Mandy_
asked on
Powershell - Could anybody analyze my script?
Hi folks,
I open this topic again for attention from other specialists. It is always good to get a second opinion.
Could anybody pay attention so far for my script? First you see the pic of my CSV
with all values. I'm a real rookie with powershell so i've to test everything and spent
a lot of time for it. Thanks god i found this community with nice specialists around.
I'm adding the setup of different custom attributes with the elseif command. Maybe
it should be better to use a hash-table for it.
like this form:
pls also see:
my other thread
Sorry that i post this topic. I dont wanna be the top thread starter here. :)
Hereby I would like also involve other powershell specilists. I'm working
for weeks on this problem and i like to finish my work asap.
Thank you for understanding.
Appreciate for your help
Mandy
I open this topic again for attention from other specialists. It is always good to get a second opinion.
Could anybody pay attention so far for my script? First you see the pic of my CSV
with all values. I'm a real rookie with powershell so i've to test everything and spent
a lot of time for it. Thanks god i found this community with nice specialists around.
I'm adding the setup of different custom attributes with the elseif command. Maybe
it should be better to use a hash-table for it.
like this form:
$compattrib3 = @{
"contoso" = "D"
"contoso2" = "F"
"All other" = "Q"
}
$compattrib12 = @{
"contoso" = "ZE"
"contoso2" = "KU"
"All other" = "ER"
}
pls also see:
my other thread
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
#}
#}
##########################
### here should set 2nd smtp and custom adress
##########################
########################
# Set custom attributes below without hash-table not working could you pls check
##########################
If ($user.company -eq "EMC")
{
Set-Mailbox -CustomAttribute5 'E' -CustomAttribute11 'EM' -CustomAttribute13 'EMC' -Identity $User.userid
}
ElseIf ($user.company -eq "CAG")
{
Set-Mailbox -CustomAttribute5 'C' -CustomAttribute11 'CA' -CustomAttribute13 'CAG' - Identity $User.userid
}
ElseIf ($user.company -ne "CAG" -or $user.company -ne "EMC")
{
Set-Mailbox -CustomAttribute5 'K' -CustomAttribute13 'ucom' -Identity $User.userid
}
ElseIf ($user.company -eq "CHT")
{
Set-Mailbox -CustomAttribute11 'CHT' -Identity $User.userid
}
ElseIf ($user.company -eq "CFT*")
{
Set-Mailbox -CustomAttribute11 'LFT' -Identity $User.userid
}
ElseIf ($user.company -eq "CXD*")
{
Set-Mailbox -CustomAttribute10 'CXD' -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"
}
}
}
Sorry that i post this topic. I dont wanna be the top thread starter here. :)
Hereby I would like also involve other powershell specilists. I'm working
for weeks on this problem and i like to finish my work asap.
Thank you for understanding.
Appreciate for your help
Mandy
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Mandy,
what else do u need?
can u address my questions?
what else do u need?
can u address my questions?
ASKER
Thank you for your fast response.
1. Custom attributes - I have 245 3- or 4-lettercodes
how can i write "all other" with the same custom attributes 5 = T 11=GU and 13=casu
2. Set mailadress (2nd smtp, custom) in line 15 of the script above
3. What about to handle Exchange 2003 account with the same activities as above?
thanks
mandy
1. Custom attributes - I have 245 3- or 4-lettercodes
how can i write "all other" with the same custom attributes 5 = T 11=GU and 13=casu
2. Set mailadress (2nd smtp, custom) in line 15 of the script above
3. What about to handle Exchange 2003 account with the same activities as above?
thanks
mandy
>>1. Custom attributes - I have 245 3- or 4-lettercodes
i don't understand.
can u give an example?
>>2. Set mailadress (2nd smtp, custom) in line 15 of the script above
where to take the mail address from?
i don't understand.
can u give an example?
>>2. Set mailadress (2nd smtp, custom) in line 15 of the script above
where to take the mail address from?
ASKER
hi,
i mean 245 company codes. Can i do this with the default parameter to
set all other company with the same customer attributes? pls see the last line
2. Mail these are only 2nd smtp like user.id@emc.com for every user nothing more
and a custom mail also for every user like MRS@userid thats all
i mean 245 company codes. Can i do this with the default parameter to
set all other company with the same customer attributes? pls see the last line
2. Mail these are only 2nd smtp like user.id@emc.com for every user nothing more
and a custom mail also for every user like MRS@userid thats all
switch($user.company){
"EMC" {Set-Mailbox -Identity $User.userid -CustomAttribute5 'E' -CustomAttribute11 'EM' -CustomAttribute13 'EMC'}
"CAG" {Set-Mailbox -Identity $User.userid -CustomAttribute5 'C' -CustomAttribute11 'CA' -CustomAttribute13 'CAG'}
"CHT" {Set-Mailbox -Identity $User.userid -CustomAttribute11 'CHT' -CustomAttribute5 'K' -CustomAttribute13 'ucom'}
"CFT*" {Set-Mailbox -Identity $User.userid -CustomAttribute11 'LFT' -CustomAttribute5 'K' -CustomAttribute13 'ucom'}
"CXD*" {Set-Mailbox -Identity $User.userid -CustomAttribute11 'CXD' -CustomAttribute5 'K' -CustomAttribute13 'ucom'}
default {"-CustomAttribute11 'CXD' -CustomAttribute5 'K' -CustomAttribute13 'ucom'}
}
like this:
switch($user.company){
"EMC" {Set-Mailbox -Identity $User.userid -CustomAttribute5 'E' -CustomAttribute11 'EM' -CustomAttribute13 'EMC'}
"CAG" {Set-Mailbox -Identity $User.userid -CustomAttribute5 'C' -CustomAttribute11 'CA' -CustomAttribute13 'CAG'}
"CHT" {Set-Mailbox -Identity $User.userid -CustomAttribute11 'CHT' -CustomAttribute5 'K' -CustomAttribute13 'ucom'}
"CFT*" {Set-Mailbox -Identity $User.userid -CustomAttribute11 'LFT' -CustomAttribute5 'K' -CustomAttribute13 'ucom'}
"CXD*" {Set-Mailbox -Identity $User.userid -CustomAttribute11 'CXD' -CustomAttribute5 'K' -CustomAttribute13 'ucom'}
default {Set-Mailbox -Identity $User.userid -CustomAttribute11 'CXD' -CustomAttribute5 'K' -CustomAttribute13 'ucom'}
}
ASKER
yes thats nice that should be work. Currently only missing to set the mails as i decri be
above.
above.
to add second smtp address:
$user = Get-Mailbox -Identity $User.userid
$user.EmailAddresses+=<2nd smtp address>
Set-Mailbox $user -EmailAddresses $user.EmailAddresses
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi,
i insert the mail and the custom attribute part. the debugger not showing any errors
but the script not running. Nothing happen. :(
Could you pls check? thanks in advance mandy
i insert the mail and the custom attribute part. the debugger not showing any errors
but the script not running. Nothing happen. :(
Could you pls check? thanks in advance mandy
Import-Module ActiveDirectory
ForEach ($User in Import-Csv "c:\export.csv"){ #$user}
switch($user){
{$_.Version -eq "MSXC2010" -and $_.action -eq "new"} {
$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
Start-Sleep -Second 5
#set mailbox
$user = Get-Mailbox -Identity $User.userid
$user.EmailAddresses = "$user.userid@emc.com"
$custom.adress = "$User.userid@mrs"
Set-Mailbox -EmailAddresses $user.Emailadresses,MRS:$custom.adress
#Set-Mailbox $user -EmailAddresses $user.EmailAddresses
write-host -ForegroundColor green "Mailbox of $($user.UserId) $($user.emailadresses) has been enabled"
switch($user.company){
"LCA*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'T' -CustomAttribute10 'LC' -CustomAttribute14 'vcom'}
"LCG*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'K' -CustomAttribute10 'LCAG' -CustomAttribute14 'vcom'}
"LCY*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'LCY' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"LCT*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'LCT' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"LCP*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'LCP' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"LTC*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'LTC' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"LVT*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'LVT' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"LCN*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'LCN' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"BTL*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'BTL' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"BTM*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'BTM' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"BTS*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'BTS' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"DTN*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'DTN' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
"KPA*" {Set-Mailbox -Identity $User.userid -CustomAttribute10 'KPA' -CustomAttribute4 'R' -CustomAttribute14 'vcom'}
default {Set-Mailbox -Identity $User.userid -CustomAttribute10 'EMC' -CustomAttribute4 'R' -CustomAttribute13 'vcom'}
}
$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 yellow "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 $_.Produkt -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"
}
}
}
ASKER
Anybody here could take a look to my script above? Thank you so much
1. This cases are doubled, once with action and once without:
2. To use wildcards in simple switch conditions, we need to tell switch that:
Also note that you are changing the context of $user from the CSV to a mailbox object - this is prone to errors.
I would change the switch to something more obvious anyway:
{$_.Version -eq "MSXC2010" -and $_.action -eq "new"} {}
{$_.Version -eq "MSXC2010" -and $_.action -eq "delete"} {}
It doesn't matter, as the switch will evaluate all conditions, unless a break or continue is encountered.2. To use wildcards in simple switch conditions, we need to tell switch that:
switch -wildcard ($user.company){
"LCA*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'T' -CustomAttribute10 'LC' -CustomAttribute14 'vcom'}
"LCG*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'K' -CustomAttribute10 'LCAG' -CustomAttribute14 'vcom'}
"LCY*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'LCY' -CustomAttribute14 'vcom'}
"LCT*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'LCT' -CustomAttribute14 'vcom'}
"LCP*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'LCP' -CustomAttribute14 'vcom'}
"LTC*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'LTC' -CustomAttribute14 'vcom'}
"LVT*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'LVT' -CustomAttribute14 'vcom'}
"LCN*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'LCN' -CustomAttribute14 'vcom'}
"BTL*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'BTL' -CustomAttribute14 'vcom'}
"BTM*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'BTM' -CustomAttribute14 'vcom'}
"BTS*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'BTS' -CustomAttribute14 'vcom'}
"DTN*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'DTN' -CustomAttribute14 'vcom'}
"KPA*" {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'KPA' -CustomAttribute14 'vcom'}
default {Set-Mailbox -Identity $User.userid -CustomAttribute4 'R' -CustomAttribute10 'EMC' -CustomAttribute14 'vcom'}
}
Note that the default case changed attribute 13 instead of 14.Also note that you are changing the context of $user from the CSV to a mailbox object - this is prone to errors.
I would change the switch to something more obvious anyway:
$attr = @{attr4 = 'R'; attr10 = $user.company.Substring(0,3)}
switch -wildcard ($user.company){
"LCA*" {$attr = @{attr4 = 'T'; attr10 = 'LC' }; break}
"LCG*" {$attr = @{attr4 = 'K'; attr10 = 'LCAG'}; break}
"LCY*" {break}
"LCT*" {break}
"LCP*" {break}
"LTC*" {break}
"LVT*" {break}
"LCN*" {break}
"BTL*" {break}
"BTM*" {break}
"BTS*" {break}
"DTN*" {break}
"KPA*" {break}
default {$attr.attr10 = 'EMC'}
}
Set-Mailbox -Identity $User.userid -CustomAttribute4 $attr.attr4 -CustomAttribute10 $attr.attr10 -CustomAttribute14 $attr.attr14
ASKER
Thank you. In which form you would to insert the creation of secondary mail and
custome mailadress?
$user = Get-Mailbox -Identity $User.userid
$user.EmailAddresses = "$user.userid@emc.com"
$custom.adress = "$User.userid@mrs"
Set-Mailbox -EmailAddresses $user.Emailadresses,MRS:$c ustom.adre ss
Youn mean this will have a conflict?
custome mailadress?
$user = Get-Mailbox -Identity $User.userid
$user.EmailAddresses = "$user.userid@emc.com"
$custom.adress = "$User.userid@mrs"
Set-Mailbox -EmailAddresses $user.Emailadresses,MRS:$c
Youn mean this will have a conflict?
I would rather go and use a different var:
$mbox = Get-Mailbox -Identity $User.userid
$mbox.EmailAddresses = $user.userid + "@emc.com"
$custom.adress = $User.userid+ "@mrs"
Set-Mailbox -Identity $User.userid -EmailAddresses $mbox.Emailadresses,MRS:$custom.adress
ASKER
thank you i will change and check again
ASKER
hi,
so far everything is working fine. The only problem left to set the mail addresstype to MRS
i did it this way but not functional
or this also not working
so far everything is working fine. The only problem left to set the mail addresstype to MRS
i did it this way but not functional
Set-Mailbox -Identity $User.userid -EmailAddresses $mbox.Emailaddresses +="MRS:$user.userid@MRS" -EmailAddressPolicyEnabled $False
or this also not working
{$Temp = Get-Mailbox -identity $User.userid}; $Temp.EmailAddresses +="MRS:" + $mbox.mrs ;Set-Mailbox $temp -EmailAddressPolicyEnabled $False
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much for your help
ASKER