INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

IF Null

IF Null

(OP)
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.

RE: IF Null

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close