Home > Mobile >  PowerShell error Method invocation failed because [System.Object[]] does not contain a method named
PowerShell error Method invocation failed because [System.Object[]] does not contain a method named

Time:01-26

EDITING QUESTION and Code--- I have hit a wall and am asking the experts for some help. I am working on a script that will link a GPO to multiple OUs and place it at a link order of a target GPO in each OU ( or plus 1 depending on the situation) for example GPO to be linked is GPO1 and it needs to be linked to OU1 and the link order needs to be based off of GPO2s link order so if GPO2 is link order 9, it will put GPO1 at position 9 moving GPO2 down to 10. this will need to happen to all sub OUs and needs to be based off of the position of GPO2 in each one of the OUs since the position is different in each OU.

I edited the code per TheMadTechnicians suggestion and it cleared the error, however it has developed another issue.

it places GPO1 in the correct place in the first OU based on GPO2s link order, however, it places it in the same link order on all sub-OUs. for example OU1 GPO2 link order is 9 and it places GPO1 in link order 9, OU2 GPO2 link order is 15 but it still places GPO1 in link order 9 when it should put it in link order 15.

 $gpoName = Read-Host -Prompt 'Enter the Group Policy Name you want to Link'
 $targetGpoName = Read-Host -Prompt 'Enter the name of the target Group Policy you want to base the Link order off of'
 $EnableLink = Read-Host -Prompt 'Enter Yes or No to enable link or not'


 # Get all OUs
 $ous = Get-ADOrganizationalUnit -SearchBase 'OU=OU,DC=DC,DC=DC,DC=DC,DC=DC,DC=DC'-Filter 'Name -like "Computers"'

 # Link the GPO to each OU
 foreach ($ou in $ous)
 {
 # Get the link order of the target GPO
 $link = (Get-ADOrganizationalUnit -SearchBase 'OU=OU,DC=DC,DC=DC,DC=DC,DC=DC,DC=DC' -Filter 'Name -like "Computers"' | Get-GPInheritance).GpoLinks | Where-Object displayname -EQ $targetGpoName | Select -ExpandProperty Order -first 1

# Link the GPO to the OU
    New-GPLink -Name $gpoName -Target $ou.DistinguishedName -LinkEnabled $EnableLink -Order $Link }

where have I gone wrong to make it do the math. ultimately, I will build the 1 option into a read-host prompt but i will tackle that once I figure out why I cant make PowerShell do simple math in this script.

any ideas and help would be greatly appreciated.

Thank you

CodePudding user response:

It looks like you're being left with an array of objects, most likely with 1 object that has 1 property. Remember that Select -Property Order leaves you with an object that has 1 property, Order. To expand the value of that property and output what the order value is you need to use Select -ExpandProperty -Property Order, then if you want to make sure it is not an array you could also add -First 1 which will ensure only 1 value is passed down the pipeline.

You should be able to change:

Select -Property Order

to:

Select -Property Order -Expand -First 1

That should solve your problem.

  • Related