Power Shell Scripts
Power Shell Scripts
Add-PSSnapin Quest.Activeroles.ADManagement
$ErrorActionPreference = "SilentlyContinue"
out-file -filepath C:\aduserlog.txt -encoding unicode -inputobject "Action on Account
SamAccount Date-Time"
out-file -filepath C:\aduserlog.txt -encoding unicode -append -inputobject ""
$user = $_.SamAccountName
$group1 = $_.group1
$group2 = $_.group2
$newUser.Put("givenName",$givenName)
$newUser.Put("sn",$sn)
$newUser.Put("displayName",$displayName)
$newUser.Put("edsaUPNPrefix",$UPNPrefix)
$newUser.Put("edsaUPNSuffix",$UPNSuffix)
$newUser.Put("samAccountName",$samAccount)
$newUser.Put("employeeType",$employeeType)
$newUser.Put("employeeID",$employeeID)
$newUser.Put("edsaPassword",$userpassword)
$newUser.Put("edsaAccountIsDisabled","false")
$newUser.setInfo()
1
Add-QADgroupmember -member $samAccount -identity $group1
Add-QADgroupmember -member $samAccount -identity $group2
Trap [Exception] {
Set-Variable -Name ErrMsg -Value "AC" -Scope global
Write-Host "Error occurred, ignoring it"
# Might set a variable so we can check for the error outside of Trap
$global:ErrMsg = ($_.Exception.Message.ToString()).Trim();
$dt = get-date
out-file -filepath C:\aduserlog.txt -encoding unicode -append -inputobject
"$global:ErrMsg $user $dt"
Continue
}
$dt = get-date
out-file -filepath C:\aduserlog.txt -encoding unicode -append -inputobject "Account
Created $user $dt"
}
$global:flgval = ""
}
disconnect-qadService
2
Script to Create a Group: “Creategrp.ps1”
Script to create a group by taking groups names from a csv file: “creategrpfromcsv.ps1”
$Pcontainer = $_.Container
$grpname = $_.group
$grptype = $_.type
$grpscope = $_.scope
Trap [Exception] {
Set-Variable -Name ErrMsg -Value "AC" -Scope global
Write-Host "Error occurred, ignoring it"
# Might set a variable so we can check for the error outside of Trap
$global:ErrMsg = ($_.Exception.Message.ToString()).Trim();
$dt = get-date
out-file -filepath C:\creategroups.log -encoding unicode -append -inputobject
"$global:ErrMsg $grpname $dt"
Continue
}
3
{
$dt = get-date
out-file -filepath C:\creategroups.log -encoding unicode -append -inputobject "Group
created $grpname $dt"
}
$global:flgval = ""
disconnect-qadService
$samAccount=$_.SamAccountName
$group = $_.group
disconnect-qadService
4
out-file -filepath C:\test.txt -encoding unicode -append -inputobject "Account Disabled
$user $dt"
}
$emailFrom = "[email protected]"
$emailTo = "[email protected]"
$subject = "your subject"
$body = "test"
$smtpServer = "130.21.197.94"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
Add-PSSnapin Quest.Activeroles.ADManagement
connect-QADService -proxy -service 'qualityassurance.ars.hk.hsbc'
$emailFrom = "[email protected]"
$emailTo = "[email protected]"
$subject = "Account Enabled"
$body = "Pls do not reply to this email. This is an auto-generated email."
$smtpServer = "130.21.197.94"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
5
Send-smtpMail -smtphost "130.21.197.94" -to "[email protected]" -from
"[email protected]" -subject "Account Enabled" -body "Pls do not reply to this
email. This is an auto-generated email."
$ErrorActionPreference = "SilentlyContinue"
out-file -filepath C:\resetpassword.log -encoding unicode -inputobject "Action on
Account SamAccount Date-Time"
out-file -filepath C:\resetpassword.log -encoding unicode -append -inputobject ""
$lastchars = $username2.length - 4
$lastchars = $username2.remove(0,$lastchars)
Trap [Exception] {
Set-Variable -Name ErrMsg -Value "AC" -Scope global
Write-Host "Error occurred, ignoring it"
# Might set a variable so we can check for the error outside of Trap
$global:ErrMsg = ($_.Exception.Message.ToString()).Trim();
$dt = get-date
out-file -filepath C:\resetpassword.log -encoding unicode -append -inputobject
"$global:ErrMsg $user $dt"
6
Continue
}
$dt = get-date
out-file -filepath C:\resetpassword.log -encoding unicode -append -inputobject "Password
changed $user $dt"
}
$global:flgval = ""
$ErrorActionPreference = "SilentlyContinue"
out-file -filepath C:\maphomefolder.log -encoding unicode -inputobject "Action on
Account SamAccount Date-Time"
out-file -filepath C:\maphomefolder.log -encoding unicode -append -inputobject ""
Trap [Exception] {
Set-Variable -Name ErrMsg -Value "AC" -Scope global
Write-Host "Error occurred, ignoring it"
# Might set a variable so we can check for the error outside of Trap
$global:ErrMsg = ($_.Exception.Message.ToString()).Trim();
$dt = get-date
7
out-file -filepath C:\maphomefolder.log -encoding unicode -append -inputobject
"$global:ErrMsg $empid $dt"
Continue
}
$dt = get-date
out-file -filepath C:\maphomefolder.log -encoding unicode -append -inputobject
"Homedrive changed to $drivenew $empid $dt"
}
$global:flgval = ""
$samAccount=$_.SamAccountName
$groupold = $_.groupold
$groupnew = $_.groupnew
$dt = get-date
out-file -filepath C:\moveduserlog.txt -encoding unicode -append -inputobject
"$groupnew $groupold $samAccount $dt"
disconnect-qadService
8
Script to send email: “sendemail.ps1”
$ErrorActionPreference = "SilentlyContinue"
out-file -filepath C:\unlockuser.log -encoding unicode -inputobject "Action on Account
SamAccount Date-Time"
out-file -filepath C:\unlockuser.log -encoding unicode -append -inputobject ""
Trap [Exception] {
Set-Variable -Name ErrMsg -Value "AC" -Scope global
Write-Host "Error occurred, ignoring it"
# Might set a variable so we can check for the error outside of Trap
$global:ErrMsg = ($_.Exception.Message.ToString()).Trim();
$dt = get-date
out-file -filepath C:\ unlockuser.log -encoding unicode -append -inputobject
"$global:ErrMsg $user $dt"
Continue
}
9
out-file -filepath C:\unlockuser.log -encoding unicode -append -inputobject "Account
unlocked $user $dt"
}
$global:flgval = ""
}
10