Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF Null

Status
Not open for further replies.

cojast08

Technical User
Apr 4, 2008
25
0
0
US
I am hoping this is an easy one for you all and I am just over looking a small mistake.

$mainOU = Read-Host -Prompt 'NorthAmerica, SouthAmerica, Africa, etc.?'
$site = Read-Host -Prompt 'Site?'
$subsite = Read-Host -Prompt 'Computers, Servers, Users, etc.?'
$dept = Read-Host -Prompt 'You can only choose one Department to scan.'

if ($subsite -eq $null){
$infoColl = @()
Get-ADComputer -Filter * -server "domain controller" -SearchBase "OU=$site, OU=$mainOU, DC=MyCompany, DC=net" | Foreach-object{Rest of code}
}
Else {
Get-ADComputer -Filter * -server "domain controller" -SearchBase "OU=$subsite, OU=$dept, OU=$site, OU=$mainOU, DC=MyCompany, DC=net" | Foreach-object{Rest of code}

}
Basically I am wanting to give an option to either scan the entire site if the subsite is left blank or if someone enters a subsite then it will only search in that OU.

If I comment out the if null then the code will work just fine for searching all site.
If I leave the "if null" code then it fails if I enter in the mainOU and site but just hit enter on subsite and dept. I assume that hitting enter would leave the variable blank and thus null. I narrowed it down to it failing at the else because it is expecting a subsite which there is not one. So it is basically ignoring the null code.
if I put something in the subsite and dept then the ELSE statement works like it is suppose to.
 
Thanks I found that putting it this way fixes the my issue

If (!($subsite)) {code}

Appears this is another way to have a blank or null varible
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top