3
votes

I am trying to Set-ADComputer on all machines matching the filter that is added to the $servers array. But it's not working. I guess it has something to do with passing an object to a string, but I can't get my head around it. Anyone's got a golden tip?

#Get gateway
$gateway = "MGMT01"
$gatewayObject = Get-ADComputer -Identity $gateway

#Get servers
$servers=@(Get-ADComputer -Filter {OperatingSystem -like "Windows Server*"}   -Properties Name | select name | ft -HideTableHeaders)

#Create list of servers
Out-File -FilePath c:\adcomputers.txt -InputObject $servers

#Set WAC delegation
ForEach ($server in $servers)
{
$nodeObject = Get-ADComputer -Identity $server
Set-ADComputer -Identity $nodeObject -PrincipalsAllowedToDelegateToAccount $gatewayObject
}

Errors:

Get-ADComputer : Cannot bind parameter 'Identity'. Cannot convert the "Microsoft.PowerShell.Commands.Internal.Format.FormatEndData" value of type "Microsoft.PowerShell.C ommands.Internal.Format.FormatEndData" to type "Microsoft.ActiveDirectory.Management.ADComputer".

At C:\Users\SA.****\Desktop\inventorize-honolulu-incl-sso.ps1:7 char:40 + $nodeObject = Get-ADComputer -Identity $server + ~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Set-ADComputer : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.

At C:\Users\SA.****\Desktop\inventorize-honolulu-incl-sso.ps1:8 char:26 + Set-ADComputer -Identity $nodeObject -PrincipalsAllowedToDelegateToAc ... + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Set-ADComputer], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADComputer

3

3 Answers

1
votes

To output your list of servers to a text file, all you need is this:

Get-ADComputer -Filter {OperatingSystem -like "Windows Server*"} |
  Select-Object -ExpandProperty Name |
  Out-File "c:\adcomputers.txt"
1
votes

Bill_Stewart had the right idea, it just didn't fit in exactly with the way you're doing things.

It's the ft -HideTableHeaders that's messing up your array. Use select -ExpandProperty instead:

$servers=@(Get-ADComputer -Filter {OperatingSystem -like "Windows Server*"}   -Properties Name | select -ExpandProperty name)

That will give you an array of plain strings, as you seem to want.

0
votes

Your Get-ADComputer line is an expression problem, you are missing () in {}. Your example works fine after fixing that.

$servers=@(Get-ADComputer -Filter {(OperatingSystem -like "Windows Server*")} -Properties Name | select name | ft -HideTableHeaders)