Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-05-25
9
Medium Priority
?
487 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 1500 total points
ID: 39196900
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 41

Expert Comment

by:footech
ID: 39197345
@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_
ID: 39197886
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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 2

Author Comment

by:Mandy_
ID: 39198831
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 39198842
>>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_
ID: 39199201
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
ID: 39199216
>>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_
ID: 39199342
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_
ID: 39200764
this has been solved so far
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
The main intent of this article is to make you aware of ‘Exchange fail to mount’ error, its effects, causes, and solution.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

671 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