Powershell the script stopped if first values not in the CSV.
hi,
i'm really impressed about the friendly people around here. Maybe anybody could help with
this one below. The problem is that all variations of csv should run with my script. the first if
loop already stops the script if the conditions do not apply. What can i do ?
Thank you so much . mandy
CSV
In the picture above and as attachment you can see the form i'm getting the CSV.
All possible variations are listed.
1. Action=Delete,Version=MSXC2010
work to be performed: Disable MBX Exchange2010, write used database to description field
2.Action=delete,Version=MSXC
work to be performed: Disable MBX Exchange2003, write used database to description field
3.Action=new,Version=MSXC2010
work to be performed: Enable MBX Exchange 2010, for EMC use different Database
write orderID to existing descriptions, set CustomAttributes related to company
Set secondary smtp (user.id@emc.com) set custom adress (MRS@user.id)
Set primary smtp related to company and first.lastname@comp.com(other script)
4.Action=new,Version=MSXC (not in CSV Forgot)
work to be performed: Enable MBX Exchange 2003.....
5.Action=new,Product=AdAccount,Version=blank
work to be performed: Passwort reset to initial value
The Exchange 2003 steps as far as i know could not handle by powershell. I have a
tool (exchmbx.exe) could be embedded in PS. Anybody knows a solution? here is the link:
Joeware Exchange 2003 Mailbox enable
ForEach ($User in Import-Csv "c:\export.csv"){ If($user.Version -like "MSX2010") { If($user.Action -like "new") {Import-Module ActiveDirectory #user which have department EMC should not use the standard database $DBuser = (Get-ADUser -identity $user.UserID).department If ($DBuser -like "*EMC*") { $db = "DBEMC00$("{0:00}" -f (1..45 | Get-random))" Enable-Mailbox -Identity $user.UserId -Database $db } else { Enable-Mailbox -Identity $user.UserId } # set custom attributes and @MRS and secondary smtp . later# Set-Mailbox -CustomAttribute3 'Q' -CustomAttribute12 'ER' -CustomAttribute11 'EMC' -#SingleItemRecoveryEnabled $true -Identity $User.userid# Set-Mailbox identity $user.UserId -EmailAddresses #smtp:$user.userid@dlh.de,MRS:$user.userid@mrs#disable mailbox exchange 2010 If($user.Action -like "delete") { Disable-Mailbox -Identity $user.UserID write-host -ForegroundColor red -backgroundcolor blue "Mailbox of $user has been disabled" # Password reset If($user.product -like "ADAccount") { $NewPassword = $user.UserId.Insert(5,"$") $NewPassword = $newPassword.Insert(3,"e") $NewPassword = $newPassword.Remove(0, 1) $newPassword = $newPassword.Insert(0,"Z") Set-ADAccountPassword -Identity $user.UserId -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force) } # write used database to description if delete and only orderid when new If($user.Aktion -like "delete") { If($user.Aktion -like "new") { $descnew = $user.OrderID $dbnew = (Get-User -Identity $user.userid | Get-Mailbox | Select-Object Database) $desccurrent = (Get-ADUser -identity $user.userid -Properties Description).Description set-aduser -identity $user.userid -description ($desccurrent+$descnew+$dbnew)}}}
i fixed the structure of your script.
u still need to fill in the missing parts of missing actions.
i used switch keyword to isolate all use cases u described in the question.
i coudldn't understand from your script which action belongs to what use case, so u need to do it yourself.
Import-Module ActiveDirectory ForEach ($User in Import-Csv "c:\export.csv"){ switch($user){ {$_.Version -eq "MSX2010" -and $_.Action -eq "new"} { #user which have department EMC should not use the standard database $DBuser = (Get-ADUser -identity $user.UserID).department If ($DBuser -like "*EMC*") { $db = "DBEMC00$("{0:00}" -f (1..45 | Get-random))" Enable-Mailbox -Identity $user.UserId -Database $db } else { Enable-Mailbox -Identity $user.UserId } } {$_.Version -eq "MSX2010" -and $_.Action -eq "delete"} { #disable mailbox exchange 2010 Disable-Mailbox -Identity $user.UserID write-host -ForegroundColor red -backgroundcolor blue "Mailbox of $user has been disabled" } {$_.Version -eq "MSX" -and $_.Action -eq "new"} {} {$_.Version -eq "MSX" -and $_.Action -eq "delete"} {} {$_.Version -eq "" -and $_.Action -eq "new" -and $_.Product -eq "AdAccount"} { $NewPassword = $user.UserId.Insert(5,"$") $NewPassword = $newPassword.Insert(3,"e") $NewPassword = $newPassword.Remove(0, 1) $newPassword = $newPassword.Insert(0,"Z") Set-ADAccountPassword -Identity $user.UserId -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force) } }}
here's the script and it works at all. I like to fix the write-host commands to
print out only the userid nothing else. And then is to do a lot of Set
custom attribute and set mailbox. pls see behind the code.
Import-Module ActiveDirectory ForEach ($User in Import-Csv "c:\export_51.csv"){#$user} switch($user){ {$_.Version -eq "MSXC2010" -and $_.Action -eq "new"} { #user which have department EMC should not use the standard database $DBuser = (Get-ADUser -identity $user.UserID).department If ($DBuser -like "*EMC*") { $db = "DBEMC00$("{0:00}" -f (1..45 | Get-random))" Enable-Mailbox -Identity $user.UserId -Database $db } else { Enable-Mailbox -Identity $user.UserId #} #} $descnew = $user.orderID $desccurrent = (Get-ADUser -identity $user.userid -Properties Description).Description set-aduser -identity $user.userid -description ($desccurrent+"/"+$descnew) } } {$_.Version -eq "MSXC2010" -and $_.Action -eq "delete"} { $dbnew = (Get-Mailbox -Identity $user.userid | Select-Object Database) $desccurrent = (Get-ADUser -identity $user.userid -Properties Description).Description set-aduser -identity $user.userid -description ($desccurrent+"/"+$dbnew) #disable mailbox exchange 2010 Disable-Mailbox -Identity $user.UserID -confirm:$false write-host -ForegroundColor red -backgroundcolor blue "Mailbox of $_.user.UserId has been disabled" } {$_.Version -eq "MSXC2010" -and $_.Action -eq "new"} {} {$_.Version -eq "MSXC2010" -and $_.Action -eq "delete"} {} {$_.Version -eq "" -and $_.Action -eq "new" -and $_.Product -eq "AdAccount"} { $NewPassword = $user.UserId.Insert(5,"$") $NewPassword = $newPassword.Insert(3,"e") $NewPassword = $newPassword.Remove(0, 1) $newPassword = $newPassword.Insert(0,"Z") Set-ADAccountPassword -Identity $user.UserId -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force) write-host -ForegroundColor yellow "Password of $_.user.UserId has been set to $newPassword" } }}
The 3rd thing should be to handle Exchange 2003 user
Thanks for your help
Mandy
0
Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.
2. Handle the 2003 exchange Account within or separate in other scripts
the same as 2010, enable/disable MBX, PW-Reset, Add to description,
set SMTP first.lastname@company.com, set 2nd smtp.
set custom attributes.
hi,
sorry i think it's missunderstanding. It works so far. I fill the Description part
yesterday with your form of the script. Today i will include to set the custom
attributes.
The secondary Adress should be installed for all user that running within the mbx-enable
part of the script. It should be userid@emc.com for everyone passing this part "mailbox-enable" and also the custom adress like MRS@user.id .
To build the primary SMTP i have create an other script with hash.table because it
has a lot of different domains and company.
u still need to fill in the missing parts of missing actions.
i used switch keyword to isolate all use cases u described in the question.
i coudldn't understand from your script which action belongs to what use case, so u need to do it yourself.
Open in new window