Monday, November 19, 2012

Create mailboxes from a file

cls
Remove-Variable * -ErrorAction SilentlyContinue
Import-Module ActiveDirectory

#Prompt for users password which will be changed on first logo
 
 

$password = Read-Host "Enter initial password for user. It will be changed at first logon." -AsSecureString

#get users from file
$users = Get-Content "c:\temp\users.txt"
foreach ($user in $users)

{    
$split = $user.Split(",", 2)
$first = $split[0];
$last = $split[1];
$display = $first + " " + $last
$name = $first + " " + $last
$upn = $first + "." + $last + "@test.com"

#get the first letter of the first name so we can create alias
$firstletter = $first.substring(0,1)
$alias = $firstletter + $last
   
#test if user exists
$testuser = Get-ADUser -server DC01 -LDAPFilter "(sAMAccountName=$alias)"

if ($testuser -eq $Null)
{ 
write-host "creating" $alias
New-Mailbox -UserPrincipalName $upn -Alias $alias -Name $name -Password $password -OrganizationalUnit HQ -FirstName $first -LastName $last -DisplayName $display -ResetPasswordOnNextLogon $true -DomainController DC01

##database is not necessary
Start-Sleep -s 5
Add-DistributionGroupMember -Identity "TestDL" -Member $alias
}Else {
write-host $name "already exists in AD"
}
}

Script to revert to Server Core

Install-WindowsFeature -ComputerName ??? server-gui-mgmt-infra,server-gui-shell

Remove-WindowsFeature -ComputerName ??? Server-Gui-Shell,Server-Gui-Mgmt-Infra

Install Powershell Web Access

Install-WindowsFeature –Name WindowsPowerShellWebAccess -IncludeManagementTools
Install-PswaWebApplication
Add-PswaAuthorizationRule -UserName contoso\anfisher -ComputerName VMM01 -ConfigurationName Windows.Powershell



Remove-WindowsFeature –Name WindowsPowerShellWebAccess -IncludeManagementTools
Uninstall-PswaWebApplication –webApplicationName PSWA
Remove-PswaAuthorizationRule -id 2




Get-PswaAuthorizationRule

Create Mutliple Test Hyper-V VMs


$NameVM ="testserver"
$NumArray = (1..4)
foreach ($number in $numArray)

{
$srvname = $NameVM + $number
mkdir d:\foldername\$srvname
$path = "d:\foldername\" + $srvname + "\" + $srvname + ".vhdx"
write-host "Creating" $srvname
New-VM –name $srvname -NewVHDSizeBytes 20GB –MemoryStartupBytes 1024MB –NewVHDPath $path –SwitchName CorpNet01
Set-VMDvdDrive –VMName $srvname -Path D:\src\pathtoiso.ISO=
Start-VM –Name $srvname
}