Q&A on a problem I'm having with [bool] query

Clear-Host

# Modules

Import-Module ActiveDirectory -Force

Import-Module .\Module\e-mail_Attributes_Template.psm1 -Force

Import-Module .\Module\send_function.psm1 -Force

# Variables

$Computers = Import-Csv -Path ‘.\computers.csv’ -Delimiter “,” -Header ‘Computername’, ‘LocalITRef’, ‘BU’, ‘Division’

# $ErrorActionPreference = ‘SilentlyContinue’

# $Computer.Computername = “WW7KEMBA01MBE”

ForEach ($Computer in $Computers){

$SystemSearch = [bool](Get-ADComputer $Computer.Computername)

if ($SystemSearch -eq $false) {

Write-Host $Computer.Computername ‘- Doesnt exist in Active Directory’ -ForegroundColor Red

} elseif ($SystemSearch -eq $true) {

Write-Host $Computer.Computername ‘- Has been found Active Directory’ -ForegroundColor Green

$SystemProperties = (Get-ADComputer $Computer.Computername -Properties *)

# Attribute Change for “LocalITRef”

if(-NOT [string]::IsNullOrEmpty($SystemProperties.comment)){

# Replace if exists

Write-Host “Existing comment found > Replacing comment attribute” -ForegroundColor Cyan

Set-ADComputer -Identity $Computer.Computername -replace @{Comment = $Computer.LocalITRef}

}else{

# Add if empty

Write-Host “Empty comment found > Adding comment attribute” -ForegroundColor Cyan

Set-ADComputer -Identity $Computer.Computername -Add @{Comment = $Computer.LocalITRef}

}

# Attribute Change for “BU”

if(-NOT [string]::IsNullOrEmpty($SystemProperties.company)){

# Replace if exists

Write-Host “Existing comment found > Replacing comment attribute” -ForegroundColor Cyan

Set-ADComputer -Identity $Computer.Computername -replace @{company= $Computer.BU}

}else{

# Add if empty

Write-Host “Empty comment found > Adding comment attribute” -ForegroundColor Cyan

Set-ADComputer -Identity $Computer.Computername -Add @{company = $Computer.BU}

}

# Attribute Change for “Division”

if(-NOT [string]::IsNullOrEmpty($SystemProperties.division)){

# Replace if exists

Write-Host “Existing comment found > Replacing comment attribute” -ForegroundColor Cyan

Set-ADComputer -Identity $Computer.Computername -replace @{division = $Computer.Division}

}else{

# Add if empty

Write-Host “Empty comment found > Adding comment attribute” -ForegroundColor Cyan

Set-ADComputer -Identity $Computer.Computername -Add @{division = $Computer.Division}

}

$Email_Attributes_Template

$Send_Mail

} else {

Write-Host $Computer.Computername ‘Has caused an unexpected error’ -ForegroundColor Yellow

}

}

My Question would be, why doesn't it read the "bool" value when a computer isn't found.

It’s so annoying now having all the answers.

I should probably go to “switch” - try { }catch{ }Final{ }

Here are three basic options:

$computer = 'Computer123'

#Boolean check
[bool]([adsisearcher]"samaccountname=$computer$").FindOne()

#Returns an object or it is NULL.  Typically what I use
if ( Get-ADComputer -Filter {Name -eq $computer} ) { 
    "Found it" 
} 
else {
    "Not found"
}

#Try\Catch 
try { 
    Get-ADComputer -Identity $computer -ErrorAction Stop 
    "Found it"
} 
catch {
    "Not found"
}

I typically always use the second method, which is doing a search and returning object(s) back or NULL. This is normally how I implement it:

$computer = 'Computer123'

$adComputer = Get-ADComputer -Filter {Name -eq $computer}

if ( $adComputer ) { 
    "Found it" 
} 
else {
    "Not found"
}