Writing NETGUID value to computer object attribute field for computers in group

I am learing powershell and I need to write the NETBOOTGUID value to computer object attribute field for computers in a given group.

The pwersheelscript below is working partially.

It gets succesfully the NETGUID VALUE from computers online at the time the script is ran, but is writing the SAME NETBOOTGUID value on all computers on the given group.

 

So I suspect my code is wrong somewhere in the ForEach-Object. Can anyone help? Thanks.

 

# Importing AD module

Import-Module ActiveDirectory

# Get all members of the MP-W-Test-Netguid

Get-ADGroupMember 'MP-W-Test-Netguid' |
# Loop through each object


ForEach-Object {
# Do this for each member

$guid = [guid](Get-WmiObject Win32_ComputerSystemProduct).UUID

Set-ADComputer -Identity $_.SamAccountName -Replace @{ netbootGUID = $guid.ToByteArray() }

 

 

Yup, according to the logic you have put, it will iterate for each computers in the group, in the loop it first gets the uuid from your local computer and save it to $guid variable, then sets the guid to the current computer object in the loop. This repeats for each computer but the guid is always from the local computer and is same.

How could I fix it so that it writes the correct netbootguid value to each computer? Thanks!

I suggest you to read the documentation of Get-CimInstance cmdlet and use it instead of WMI cmdlet.

Get-Help Get-CimInstance -Online

Hint: Get-CimInstance do have a -ComputerName parameter.

Thanks

Get-WMIObject has a ComputerName parameter as well.

# get function / cmdlet details
(Get-Command -Name Get-WmiObject).Parameters.Keys

# Results
Class
Recurse
Property
Filter
...
EnableAllPrivileges
Authority
Credential
...
ComputerName
Namespace
...

Get-help -Name Get-WmiObject -Full
Get-help -Name Get-WmiObject -Online
Get-help -Name Get-WmiObject -Examples

# Results
Get-WmiObject -Class Win32_Process
Get-WmiObject -Class Win32_Service -ComputerName 127.0.0.1
Get-WmiObject -Namespace "root/default" -List
Get-WmiObject -Query "select * from win32_service where name='WinRM'" -ComputerName Server01, Server02 | Format-List -Property PSComputerName, Name, 
(Get-WmiObject -Class Win32_Service -Filter "name='WinRM'" -ComputerName Server01).StopService()
Get-WmiObject -Class Win32_Bios | Format-List -Property
Get-WmiObject Win32_Service -Credential FABRIKAM\administrator -Computer Fabrikam

Though Get-CIM* is the new hotness and what is the focus going forward.

For your script, you have to tell PowerShell or any language what thingy to work on. You are not doing that.

# Importing AD module
Import-Module ActiveDirectory

# Get all members of the MP-W-Test-Netguid
Get-ADGroupMember 'MP-W-Test-Netguid' |
ForEach-Object {
    "`n***Processing GUID setting on $($PSItem.Name)***`n"
    ($guid = [guid](Get-WmiObject -ComputerName $($PSItem.Name) -Class Win32_ComputerSystemProduct).UUID)
    Set-ADComputer -Identity $PSItem.Name -Replace @{ netbootGUID = $guid.ToByteArray() }
}

Sorry for the late reply.

 

Thanks, this fixed it.

 

Now I want to add code to it that will sdave to a csv file the names of all computers for which their NetBootGuid attribute were not upgraded becaue the computers were shutdown or for any other reason they could not be reached opn the network at the time the script was ran.

 

So far I have this, but is not working, any help will be appreaciated, THANKS !

 

Importing AD module

Import-Module ActiveDirectory

Get all members of the MP-W-Test-Netguid

Get-ADGroupMember ‘MP-W-Test-Netguid’ |

ForEach-Object {

$erroractionpreference = “silentlyContinue”

Try
{
“`nProcessing GUID setting on $($PSItem.Name)`n”

($guid = [guid](Get-WmiObject -ComputerName $($PSItem.Name) -Class Win32_ComputerSystemProduct).UUID)
}
Catch
{

Import-CSV -LiteralPath C:\ERRORS\Nontbootguid-computers.csv

}

Set-ADComputer -Identity $PSItem.Name -Replace @{ netbootGUID = $guid.ToByteArray() }

}

Import-Csv is to import a csv file and is clear from the name. You would use Export-Csv instead with -Append parameter.

But I highly recommend you to read through the docs for any cmdlet you approach before using it. This will save you time.

Finally, please read through below post on how to format code when posting in the forums.

https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/

The powershell script below is working partially.

It NOW gets succesfully the NETGUID VALUE from computers online at the time the script is ran, and it NOW WRITES IT succesfully in the netbootguid attribute field.

However, I am having a hard time figuring out how to write to a csv file the names of computers which netbootguids were not written because the computers were inaccesible at the time the script ran. Here is the code. Can you help ? Thanks

Here is the code:

# Importing AD module

Import-Module ActiveDirectory

# Get all members of the MP-W-Test-Netguid
Get-ADGroupMember 'MP-W-Test-Netguid' |


ForEach-Object {

# $erroractionpreference = "silentlyContinue"

Try
{
"`n***Processing GUID setting on $($PSItem.Name)***`n"

($guid = [guid](Get-WmiObject -ComputerName $($PSItem.Name) -Class Win32_ComputerSystemProduct).UUID)
$computerName1 = $($PSItem.Name) 
}

Catch
{

Export-Csv -Path C:\ERRORS\Netbootguid-computers.csv -InputObject $computerName1 -Append


# $computerName1 | Export-Csv -Path C:\ERRORS\Netbootguid-computers.csv -InputObject $PSItem.Name -Append

# Export-Csv -Path C:\ERRORS\Netbootguid-computers.csv [-NoTypeInformation] -Append

}


Set-ADComputer -Identity $PSItem.Name -Replace @{ netbootGUID = $guid.ToByteArray() }


}
Try {
    "`n***Processing GUID setting on $($PSItem.Name)***`n"
    $Guid = [guid](Get-WmiObject -ComputerName $PSItem.Name -Class Win32_ComputerSystemProduct -ErrorAction Stop).UUID
}

Catch {
    [PSCustomObject]@{ComputerName = $PSItem.Name} | Export-Csv -Path C:\ERRORS\Netbootguid-computers.csv -Append
}

you don’t need to wrap everything in parenthesis. See more about PSCustomObject here.

Thanks,

THe Catch block is not writing to file. Ensure that the list computers processed included computers that are unaccesible (off)

With -ErrorAction Stop, it should jump to catch block for failed attempts.

Thanks !

I also made it work this way:

 

Get-ADGroupMember 'MP-W-Test-Netguid' |
ForEach-Object {
# if($pc = Get-CimInstance Win32_ComputerSystemProduct -ComputerName $_.Name -ErrorAction SilentlyContinue){

if($pc = Get-WmiObject Win32_ComputerSystemProduct -ComputerName $_.Name){

$guid = ([guid]$pc.UUID).ToByteArray()
Set-ADComputer $_.Name -Replace @{ netbootGUID = $guid }
}
else{
$_.Name
}
} |
Out-File C:\ERRORS\NONetbootguid-computers.csv