Powershell issues with Get-ADComputer query.

Hi all,

I am trying to create a Powershell script that will rename a machine on startup if the name is still set to the default one.
We have a default name for all of our windows images on our fusion environments and they all need to have different names or the domain doesn't like it. Now im not a Server admin and have very little powershell experience but im a web dev so i know how to code but Powershell seems to have stumped me a bit.

Below is the logic i am trying to use, just querying AD to see if the name exists and if not applying the name otherwise regenerating the GUID again and trying again.

however the issue i have is that when i run the GetADComputers query it errors out saying that Name doesn't exits as a module etc. However when i just run that query on its own it works fine.

Can anyone here see what i am doing wrong? I would be very appreciative.

Import-Module ActiveDirectory Function GenerateName() { $guid = [guid]::NewGuid() $NewComputerName = "VM-OSX-" + $guid.ToString() $computers = (Get-ADComputer -filter (Name -Like "VM-OSX*") -Property Name | Select-Object Name) foreach ($computer in $computers) { if($computer -eq $NewComputerName) { echo "Break out and retry as " +$NewComputerName + " is already being used."; return -1 }

}
echo "name is all good; set it to computer and do restart."

return 1 } while(GenerateName == -1) { echo "Generating name..." } echo "Name has been set, computer will now restart..."


Kind Regards,

Courtney O'Sullivan



July 23rd, 2015 12:53am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics