Create an Active Directory user with PowerShell (Updated)

Create an Active Directory user with PowerShell.

to run this script add a period (.) and drag the scrip into the PowerShell window.

Example:

[PS] C:\Documents and Settings\Administrator\Desktop>.C:\Documents and Settings\Administrator\Desktop\newuser.ps1

 

‘*********************************

 

# New User Creation Script
# 3-2018
# Don Wilwol
#
# *IMPORTANT**
# Create a subdirectory from the directory where this script will be run from
# Make sure the sub diectory is named DeptGroups
# In DeptGroups add the csv files with the Dept group names in them
# Each file **MUST** have a header of “Group”
# This can be any user classification that gets a particular set of groups
# if its by location then use the location instead of departments
#
# If you want to create an onprem mailbox The script must be run from the Exchange Shell
Write-Host “`n`n`n Script was weritten by Don Wilwol” -ForegroundColor Green
write-host “—– Enjoy —–” -ForegroundColor Green
write-host “—–”
###################################################################################################

Import-Module activedirectory
$FormatEnumerationLimit =-1
$date = ( get-date ).ToString(‘MM-dd-yyyy.hh-mm’)

######################################################################################
################### These must be configured for your environment ####################
######################################################################################
$domain = “@ex2016.com”
$RoutingDomain = ‘@tenantname.mail.onmicrosoft.com’

$OrganizationalUnit = ‘OU=TestUsers,DC=Ex2016,DC=com’
$HDr = “H:”

################### Function Lines ####################

# Show message box popup and return the button clicked by the user.
# Show input box popup and return the value entered by the user.
function Read-InputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText)
{
Add-Type -AssemblyName Microsoft.VisualBasic
return [Microsoft.VisualBasic.Interaction]::InputBox($Message, $WindowTitle, $DefaultText)
}

#############################################
#################Input boxes#################

$Firstname = Read-InputBoxDialog -Message “First Name” -WindowTitle “First name” -DefaultText “FirstName”
if ($Firstname -eq “”) { write-host “You Cancel”
return
}
elseif ($Firstname -eq “FirstName”) { write-host “You Cancel”
Return
}
else { Write-Host “You entered $FirstName” }

$Lastname = Read-InputBoxDialog -Message “Last Name” -WindowTitle “Last name” -DefaultText “LastName”
if ($Lastname -eq “”) { write-host “You Cancel”
return }
elseif ($Lastname -like “LastName”) { write-host “You Cancel”
Return }
else { Write-Host “You entered $Lastname” }
#$Lastname = Read-Host “LastName: ”

write-host “—–”
write-host “—–”
write-host “Group Files available”
write-host “—–”
write-host ” ******* DO NOT include .csv ******”
write-host “—–”
write-host “—–”

get-childitem “./DeptGroups” | foreach{
write-host “`n ” $_.name -ForegroundColor Red }
write-host “`n`n—–”
write-host “—–”

$Dept = Read-InputBoxDialog -Message “Depatment or Classification File `nThey are Listed IN RED `nin the command Window`nDo Not include .csv” -WindowTitle “Departments” -DefaultText “IT”
if ($Dept -eq “”) { write-host “You Cancel”
return }

$name = $Firstname + ” ” + $Lastname
$name2 = $Firstname + ” ” + $Lastname
$username = $Firstname[0] + $Lastname
$username2 = $Firstname[0] + $Lastname
$emailAdd = “$Firstname.$Lastname” + $domain

#############################################
################# Password ##################

$password = Read-InputBoxDialog -Message “Password” -WindowTitle “Password” -DefaultText “P@ssw0rd” -AsSecureString
#$password = Read-Host “Enter password: ” -AsSecureString
#$password = ‘P@ssw0rd’
######################################################################################
############################# Create folder for Logs & create log file Path##########
If (test-path “./UserCreatelogs”){
}
Else
{mkdir “./UserCreatelogs”}
$file = “./UserCreatelogs\$username-$date.txt”

#####Check if names exist#######################
$ErrorActionPreference = “SilentlyContinue”
$a = get-aduser $username -properties * -erroraction ‘silentlycontinue’

If ($a.name -like “$name”){
write-host “`n Name’s In use. Will Use secondary `n”
$MI = Read-InputBoxDialog -Message “We’ll Need a Middle Intial” -WindowTitle “Middle Initial” -DefaultText “Z”
#$MI = Read-Host “Middle Initial: ”
$name = $Firstname + ‘.’ + $MI+ ‘.’ + $Lastname
Write-host “`n Name will be $name `n”
$username = $Firstname[0] + $MI + $Lastname
$ErrorActionPreference = “SilentlyContinue”
$b = get-aduser $username -properties * -erroraction ‘silentlycontinue’
If ($b.name -like “$name”){
“$name2 and $Name is in use, Script will fail. Username can not be found” | out-file “./UserCreatelogs\$date-FailureLog-$Name.txt” -append
write-host ” $name2 and $Name is in use, *** Script failed.*** Username can not be found. See Log File” -ForegroundColor RED
write-host “—–”
write-host “—–”
write-host ” —–*******——*******———`n`n`n`n`n`n`n`n” -ForegroundColor RED
return
}
}
if ($a.samaccountname -like “$username”){
write-host “`n SamaccountName In use. Will use secondary `n”
$username = $Firstname[0] + $MI + $Lastname
write-host “`n SamaccountName will be $username`n ”
$ErrorActionPreference = “SilentlyContinue”
$b = get-aduser $username -properties * -erroraction ‘silentlycontinue’
If ($a.samaccountname -like “$username”){
“$name2 and $Name is in use, Script will fail. Username can not be found” | out-file “./UserCreatelogs\$date-FailureLog-$Name.txt” -append
write-host ” $name2 and $Name is in use, *** Script failed.*** Username can not be found. See Log File” -ForegroundColor RED
write-host “—–”
write-host “—–”
write-host ” —–*******——*******———`n`n`n`n`n`n`n`n” -ForegroundColor RED
return
}
}
if ($a.EmailAddress -like “$emailAdd”){
write-host “`n Email Add In use. Will use secondary`n ”
$emailAdd = “$name$domain”
write-host “`n Email will be $emailAdd `n”

}

##### Write out to file #######################

“The Date is $date ” | out-file $file -append
$who = whoamI
“Run by $who `n” | out-file $file -append
“`n——————————- `n” | out-file $file -append
“`nTemp password is $Password `n” | out-file $file -append

“`n——————————- `n” | out-file $file -append

##################Create the user#######################
Write-Host “`n Working on $name `n”
NEW-ADUser $name -GivenName $Firstname -Surname $Lastname -enable $True -SamAccountName $username -Path $OrganizationalUnit -UserPrincipalName $emailAdd -accountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -ChangePasswordAtLogon $false -OtherAttributes @{‘mail’=$emailAdd}

##### Set additional proxyaddress #######################
set-aduser -identity $username -Add @{‘proxyAddresses’ = $proxyAddresses | % { “smtp:$username$domain” }}

 

############ Setting other attributes #############################
$Desc = Read-InputBoxDialog -Message “Description” -WindowTitle “Description” -DefaultText “Regular User”
set-aduser -identity $username -description $Desc

$HO = Read-InputBoxDialog -Message “Home Office” -WindowTitle “Home Office” -DefaultText “Hoosick Falls”
set-aduser -identity $username -Office $HO

$HD = Read-InputBoxDialog -Message “Home Drive” -WindowTitle “Home Drive” -DefaultText ‘\\Servername\$MyShare\%username%’
set-aduser -identity $username -HomeDirectory $HD -Homedrive $HDr

$PH = Read-InputBoxDialog -Message “Phone” -WindowTitle “Phone” -DefaultText ‘518-686-XXXX’
set-aduser -identity $username -HomePhone $PH

 

############Disable the next two sections for a pure cloud mailbox user
############Disable these lines if not maiking a mailbox on-prem. Rem out for Office 365
$db = $NULL
#$db = get-mailboxdatabase | select name
#write-host $db.name
#$MDB = Read-InputBoxDialog -Message “Choose a Mailboxdatabase” -WindowTitle “Choose a Mailboxdatabase `n Choices are shown in the EM\C window” -DefaultText “MBDB1″
#Enable-Mailbox -Identity $username -database $MDB
#”`n`n Enabling on prem Mailbox for $username on database $MDB” | out-file $file -append

############ Enable these line for Making an O365 remote user
#(Read-InputBoxDialog -Message “Enable Office 365 Mailbox” -WindowTitle “Enable Office 365 Mailbox” -DefaultText “Script will wait for you to make sure the O365 Mailbox is ready”).WaitForExit()
#”`n`n enabling remoteMailBox for $emailAdd With an address of $username$RoutingDomain ” | out-file $file -append
#enable-remoteMailBox $emailAdd -RemoteRoutingAddress $username$RoutingDomain -PrimarySMTPAddress $emailAdd

Write-host “`n`n—————-” | out-file $file -append
Import-CSV “./DeptGroups\$Dept.csv” | % {
Write-host “`n`n Adding $Name to $_.Group”
“Adding $Name to $_.Group” | out-file $file -append
Add-ADGroupMember -Identity $_.Group -Member $UserName
}
“`n——————————- `n” | out-file $file -append

“`n`n” | Out-File $file -Append
“`n`n Running Get-aduser $username” | Out-File $file -Append
Get-aduser $username -properties * | out-file $file -append

Write-Host “`n`n`n Script is complete. Log file can be seen at $file`n`n`n`n`n`n`n`n” -ForegroundColor Green
write-host “—–”
write-host “—–”
write-host “—–”
write-host “—–“

Advertisements

Getting the Status of all Azure VM’s

I wanted to make sure all my VM’s were shut off in my lab when I’ve completed. Since I’ve converted everything to the resource manager, I couldn’t just use “Get-AzureVM –status” so I came up with this.

 

cls
Get-AzureRmVM -ResourceGroupName dw-ResourceGroup | foreach{
$vm = Get-AzureRmVM -ResourceGroupName dw-ResourceGroup -name $_.name -status
write-host ".."
write-host ".."
write-host "Server  Name – " $_.name
$vm.Statuses.displaystatus[1]}
write-host ".."

 

You want to see “VM deallocated”

 

image

A script to get a count of the number of users per server per DAG

Shared from Ron Williams @ https://r0nwilliams.wordpress.com/

A script to get a count of the number of users per server per DAG:

##Script start

$AllData = New-Object System.Collections.ArrayList
$Dags = Get-DatabaseAvailabilityGroup
foreach ($Dag in $Dags)
{
foreach ($Server in $Dag.Servers)
{
$Count = (get-mailbox -Server $Server -ResultSize unlimited).count
$Object = New-Object PSCustomObject -Property @{
ServerName = $Server
Users =       $Count
DAGName = $Dag.Name
}
[void]$AllData.Add($Object)
}
}
$AllData

 

 

Looks like:

t

White list Office 365 for Individual users from a Spreadsheet

This script will ask what the users UPN is, then set the addresses in the spreadsheet as white listed.

##Start Script

$r = Read-Host "What is the users email address"
$allU = @()
$file = get-content c:\temp\user.csv
Foreach ($usr in $file){
$allU += $usr
}
Set-MailboxJunkEmailConfiguration -Identity $r -TrustedSendersAndDomains $allU -Enabled $true

##End Script

 

You can add a block list by adding the –BlockedSendersAndDomains  or changing to it.

 

Set-MailboxJunkEmailConfiguration -Identity <MailboxIdParameter> [-BlockedSendersAndDomains <MultiValuedProperty>] [-Confirm [<SwitchParameter>]] [-ContactsTrusted <$true | $false>] [-DomainController <Fqdn>] [-Enabled <$true | $false>] [-IgnoreDefaultScope <SwitchParameter>] [-TrustedListsOnly <$true | $false>] [-TrustedRecipientsAndDomains <MultiValuedProperty>] [-TrustedSendersAndDomains <MultiValuedProperty>] [-WhatIf [<SwitchParameter>]]

 

The csv is just a list of email addresses, no header.

 

image

How to connect Powershell to an Office 365 Tenant

Step 1: Install required software

These steps are only required once on each computer you will be running powershell from, not every time you connect.

Run this to connect:

$LiveCred = Get-Credential$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection

Import-PSSession $Session

Then to remove it:

Remove-PSSession $Session

If you close the Windows PowerShell window without disconnecting from the server-side session, your connection will remain open for 15 minutes. Your account can have only three connections to the server-side session at one time.

 

Hope it helps

dw

Ping with PowerShell

Ping a single address

Get-WmiObject -Class Win32_PingStatus -Filter "Address='SomeServer'"

Ping a subnet

1..254| ForEach-Object -Process {Get-WmiObject -Class Win32_PingStatus -Filter ("Address='192.168.1." + $_ + "'") -ComputerName .} | Select-Object -Property Address,ResponseTime,StatusCode

Finding Exchange 2010 archive mailbox sizes.

With Exchange 2010 SP 1 the archive mailbox functionality is a little more usable.  This means we will need to discover ways to manage and report on these mailboxes.

So, “How do you see the size of the archive mailbox?”

Use get-mailboxstatistics mailboxname –archive.

To see all of the archive mailboxes Get-Mailbox -archive | Get-MailboxStatistics -archive |fl DisplayName, Total*

There .

I haven’t test this yet, but also found this. It’s a GUI based view of all Exchange  2010 mailboxes with mailbox sizes.

Finally, the ability to see mailbox sizes in one place!!!!!

http://gsexdev.blogspot.com/2010/03/mailbox-size-gui-exchange-2010-remote.html

Hope it Helps

dw