[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

Powershell - Could anybody analyze my script?

Hi folks,

the csv
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"
}

Open in new window



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" 
	  	}
		
         	}
                  }

Open in new window



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
0
Mandy_
Asked:
Mandy_
  • 10
  • 5
  • 4
3 Solutions
 
Meir RivkinFull stack Software EngineerCommented:
i fixed the custom attribute section in your script, take a look.
in regards to set 2ns smtp address and custom address, where are they located?

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

		   		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'}
				}
				##########################
							
				$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" 
	  	}		
	}
}

Open in new window

0
 
Mandy_Author Commented:
sorry i delete the other thread. Pls let this questions. thank you
0
 
Meir RivkinFull stack Software EngineerCommented:
Mandy,

what else do u need?
can u address my questions?
0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
Mandy_Author Commented:
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
0
 
Meir RivkinFull stack Software EngineerCommented:
>>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?
0
 
Mandy_Author Commented:
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


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'}
}

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
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'}
}

Open in new window

0
 
Mandy_Author Commented:
yes thats nice that should be work. Currently only missing to set the mails as i decri be
above.
0
 
Meir RivkinFull stack Software EngineerCommented:
to add second smtp address:
  $user = Get-Mailbox -Identity $User.userid
   $user.EmailAddresses+=<2nd smtp address>
   Set-Mailbox $user -EmailAddresses $user.EmailAddresses   

Open in new window

0
 
QlemoDeveloperCommented:
In http:#a39201454, I would use the same sequence of custom attributes - that is much better to read:
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 -CustomAttribute5 'K' -CustomAttribute11 'CHT' -CustomAttribute13 'ucom'}
  "CFT*" {Set-Mailbox -Identity $User.userid -CustomAttribute5 'K' -CustomAttribute11 'LFT' -CustomAttribute13 'ucom'}
  "CXD*" {Set-Mailbox -Identity $User.userid -CustomAttribute5 'K' -CustomAttribute11 'CXD' -CustomAttribute13 'ucom'}
 default {Set-Mailbox -Identity $User.userid -CustomAttribute5 'T' -CustomAttribute11 'GU'  -CustomAttribute13 'casu'}
}

Open in new window

Note that I have also changed the default, as you initially asked for that set of values, but in your example later copied the CXD* case.
0
 
Mandy_Author Commented:
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


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" 
		}
		
	}

}

Open in new window

0
 
Mandy_Author Commented:
Anybody here could take a look to my script above?  Thank you so much
0
 
QlemoDeveloperCommented:
1. This cases are doubled, once with action and once without:
    {$_.Version -eq "MSXC2010" -and $_.action -eq "new"} {}
    {$_.Version -eq "MSXC2010" -and $_.action -eq "delete"} {}

Open in new window

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'}
}

Open in new window

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

Open in new window

0
 
Mandy_Author Commented:
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:$custom.adress


Youn mean this will have a conflict?
0
 
QlemoDeveloperCommented:
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

Open in new window

0
 
Mandy_Author Commented:
thank you i will change and check again
0
 
Mandy_Author Commented:
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

 Set-Mailbox -Identity $User.userid -EmailAddresses $mbox.Emailaddresses +="MRS:$user.userid@MRS"  -EmailAddressPolicyEnabled $False

Open in new window


or this also not working

{$Temp = Get-Mailbox -identity $User.userid}; $Temp.EmailAddresses +="MRS:" + $mbox.mrs ;Set-Mailbox $temp  -EmailAddressPolicyEnabled $False

Open in new window

0
 
QlemoDeveloperCommented:
I can't tell how to set up MRS types, but you would need to change the expression in double quotes (and the assignment is not necessary):
 Set-Mailbox -Identity $User.userid -EmailAddresses $mbox.Emailaddresses +"MRS:$($user.userid)@MRS"  -EmailAddressPolicyEnabled $False

Open in new window

That is because "$var.property" will not resolve to the value stored in that property, but just replace $var with a string representation, and than add ".property" as text.
0
 
Mandy_Author Commented:
Thank you so much for your help
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 10
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now