Solved

Powershell the script stopped if first values not in the CSV.

Posted on 2013-05-25
9
458 Views
Last Modified: 2013-05-28
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
csv import
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)
}

}
}

Open in new window

Export.csv
0
Comment
Question by:Mandy_
  • 5
  • 3
9 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
Comment Utility
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)	  
		}
	}
}

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
@sedgwick - I like your use of a switch statement here.

If it's of any help to anyone looking at this question, it is part of another question at http:#a39196209, for which I have also posted code.
0
 
LVL 2

Author Comment

by:Mandy_
Comment Utility
Hi,

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

Open in new window


$compattrib3 = @{
"contoso" =  "D"
"contoso2" = "F"
"All other" = "Q"
}

$compattrib12 = @{
"contoso" = "ZE"
"contoso2" = "KU"
"All other" = "ER"
}

Open in new window



Set-Mailbox -CustomAttribute3 $compattrib3."All other" -CustomAttribute12 $compattrib12."All other" -CustomAttribute11 'EMC' -SingleItemRecoveryEnabled $true -Identity $User.userid

The 2nd think is to write the secondary smtp user.id@emc.com and a custom adress like
MRS@user.id to all user has been enabled with the script above.

 Set-Mailbox identity $user.UserId -EmailAddresses #smtp:$user.userid@dlh.de,MRS:$user.userid@mrs

The 3rd thing should be to handle Exchange 2003 user

Thanks for your help
Mandy
0
 
LVL 2

Author Comment

by:Mandy_
Comment Utility
Dear my friends.

Every other threads are closed now . The questions still not be answered are the
following:

1. Add the secondary smtp user.id@emc.com and a custom adress like
MRS@user.id to all user MBX has been enabled with the script above.

 Set-Mailbox identity $user.UserId -EmailAddresses smtp:$user.userid@dlh.de,MRS:$user.userid@mrs

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.

Appreciate for your help
Mandy
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
>>here's the script and it works at all
so it works or not?

also u didn't fill in the missing parts.
where do u want to add the secondary address?
can u be more specific?
0
 
LVL 2

Author Comment

by:Mandy_
Comment Utility
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.

Mandy
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
>>The secondary Adress should be installed for all user that running within the mbx-enable
part of the script.

can u refer to the line number?
where should the code be placed in the script?
0
 
LVL 2

Author Comment

by:Mandy_
Comment Utility
hi, pls see the script below. I market the place for setup the smtp and custom adress.

i tried to implement the custom attributes part without a hash table. The
script stops not working. Nothing happens. Could pls advice?


Thank you so much for your help
mandy



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

0
 
LVL 2

Author Closing Comment

by:Mandy_
Comment Utility
this has been solved so far
0

Featured Post

Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
In this video we show how to create an Address List in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Organization >> Ad…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now