Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Exit a ForEach-object loop if a value is found to be Null

$
0
0

I have a script that I'm writing that goes through a CSV from our HR dept.  They provide me with a list of people who have left but not all of them have an AD account so I need to filter it before I pass it to the Help Desk. 

If the variable $Name=Null how do I stop the script from running the rest of the commands and go to the next entry on the CSV file?

 

 

 

 

 

 

import-csv 'C:\PurgeUserRecords\From HR\LEAVERSREPORTMay2014.csv' | ForEach-Object{

$Name=Get-ADObject -Filter {Name -eq $_.DisplayName}

$DisplayName=Get-ADObject -Filter {Name -eq $Name} -Properties * | select DisplayName

$DC01=Get-ADUser -Filter {Name -eq $Name} -Server cycdc01 -Properties LastLogon | Select-Object @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}}

$DC02=Get-ADUser -Filter {Name -eq $Name} -Server cycdc02 -Properties LastLogon | Select-Object @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}}

$DC03=Get-ADUser -Filter {Name -eq $Name} -Server cycdc03 -Properties LastLogon | Select-Object @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}}

$DC04=Get-ADUser -Filter {Name -eq $Name} -Server cycdc04 -Properties LastLogon | Select-Object @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}}

$lastLogonDate=Get-ADUSer -Filter {Name -eq $Name} -Properties * | select lastLogonDate

$Manager=ADUser -Filter {Name -eq $Name} -Properties * | select Manager

}

 


Viewing all articles
Browse latest Browse all 6937

Latest Images

Trending Articles



Latest Images