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.
$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.