Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

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
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
Mandy_
Asked:
Mandy_
  • 5
  • 3
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
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
 
footechCommented:
@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
 
Mandy_Author Commented:
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
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

 
Mandy_Author Commented:
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
 
Meir RivkinFull stack Software EngineerCommented:
>>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
 
Mandy_Author Commented:
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
 
Meir RivkinFull stack Software EngineerCommented:
>>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
 
Mandy_Author Commented:
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
 
Mandy_Author Commented:
this has been solved so far
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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