`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{ }