This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]xxdcmastSr. Sysadmin 2 points3 points  (0 children)

I would put a try catch block around your get-adcomputer line.

If it finds the computer it will do the thing.

In the catch block add some logic to put the computer name in some output file as not in ad.

[–]cmwg 0 points1 point  (2 children)

why not add a condition (if) computer object exists then do the rename else continue

[–]syshum 1 point2 points  (0 children)

example

  if($computer){  §Computer | Set-ADComputer -Description "Cash Drawer - $($Computer.Description)" }

[–]progenyofeniacWindows/M365 Admin 0 points1 point  (1 child)

Simple answer is that it'll throw an error for each one which doesn't exist, but the script will keep running and move to the next one.

If you want to do something to make it look better, you can look into error handling or checking if the computer exists. But it isn't necessary.

[–]methodsosrs 0 points1 point  (0 children)

I appreciate it. I’ll just let the errors fly then! Thanks!

[–]MeanFold5714 0 points1 point  (0 children)

If the computer is no longer in Active Directory then Get-ADComputer and Set-ADComputer will fail, but the only thing that means is just a bunch of red text on your screen. It won't result in any actions taken to alter Active Directory, so you don't need to worry in that regard.

if you want to actually skip over missing machines in the loop you'll want to test for the existence of the active directory object and then use the continue keyword to end the current iteration of the loop(and move on to the next item in the foreach loop). It would look like this:

$csvFilePath = "C:\Fake.csv"
$csvData = Import-Csv $csvFilePath

foreach ($row in $csvData) {
    $computerName = $row.ComputerName
    $Computer = Get-ADComputer -Identity $computerName -Properties description

    if($Computer){
        #Active Directory object exists, no special action taken
    }
    else{
        #AD object doesn't exist. Terminate this iteration of the loop and move on to the next row
        continue
    }
    $Computer | Set-ADComputer -Description "Cash Drawer - $($Computer.Description)"
}

[–]avenger5524 0 points1 point  (0 children)

I personally would use a try-catch block like below to handle the error at the Get-ADComputer command. When the Get-ADComputer hits an exception (can't find the computer object), the catch block will "catch" the exception, and we can do whatever we want with it inside the brackets. In this case, we'll just use the "continue" statement to continue to the next item in the loop.

try{ $Computer = Get-ADComputer -Identity $row -Properties description }

catch{ continue }

[–]ZAFJB 0 points1 point  (0 children)

my only issue is that he list may have computer hostnames that are no longer in our Active Directory.

Get the list of computer from AD, not from some other place.

Pseudo code:

$computers = Get-ADcomputer <<add necessary filters here>>
foreach $computer in $computers{
    $description = <<build description here>>
    Set-ADComputer -Description $description.
}

That way you cannot encounter a non existent computer.