0

I am getting the following error when attempting to run the PS script I wrote.

Error:

New-GPLink : Cannot convert 'System.Object[]' to the type 'System.Int32' required by parameter 'Order'. Specified method is not supported. At line:17 char:93

-Target $ou.DistinguishedName -LinkEnabled $EnableLink -Order $Link }

CategoryInfo : InvalidArgument: (:) [New-GPLink], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.GroupPolicy.Commands.NewGPLinkCommand

here is the code:

 $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=DCv'-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 -Property Order

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

here is what i am trying to accomplish:

trying to 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 in OU1 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.

for example, in OU2 GPO2 is in link order 15 which means the script should place GPO1 at link order 15 in OU2 unlike OU1 where it placed it at link order 9.

what am I missing here? any help or advise would be greatly appreciated as i am stumped.

thank you

----------edited code and error below------

 $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
 $links = (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 -Property Order
  }


 # Link the GPO to the OU
 foreach ($link in $links){

 New-GPLink -Name $gpoName -Target $ou.DistinguishedName -LinkEnabled $EnableLink -Order $Link }

----New Error below------------- ----of Note - the error below repeats except the "@{Order=23}" changes and it does represent the proper link order for each OU i am trying to link GPO1 to

New-GPLink : Cannot bind parameter 'Order'. Cannot convert the "@{Order=23}" value of type "Selected.Microsoft.GroupPolicy.GpoLink" to type "System.Int32". At line:21 char:93 -Target $ou.DistinguishedName -LinkEnabled $EnableLink -Order $Link }

CategoryInfo : InvalidArgument: (:) [New-GPLink], ParameterBindingException FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.GroupPolicy.Commands.NewGPLinkCommand

NuckinFutz
  • 85
  • 5
  • 18
  • You are only getting one results which you have to force to an array using `@`. Use : $link = @(,,,,,,,,) – jdweng Jan 25 '23 at 15:16
  • @jdweng made edit and same error $link = @(Get-ADOrganizationalUni........ Error: New-GPLink : Cannot convert 'System.Object[]' to the type 'System.Int32' required by parameter 'Order'...... – NuckinFutz Jan 25 '23 at 15:31
  • $link is an array with more than one result – js2010 Jan 25 '23 at 15:34
  • 1
    Change `... |Select -Property Order` to `... |Select-Object -ExpandProperty Order` or `... |ForEach-Object Order` – Mathias R. Jessen Jan 25 '23 at 15:35
  • @MathiasR.Jessen - unfortunately that didnt work either but it did change the error (note the error repeats and the "{@order=23}" does reflect the correct position of the target GPO - Code to follow New-GPLink : Cannot bind parameter 'Order'. Cannot convert the "@{Order=23}" value of type "Selected.Microsoft.GroupPolicy.GpoLink" to type "System.Int32". will add new code to original question and full error – NuckinFutz Jan 25 '23 at 15:54
  • @js2010 - yes it is – NuckinFutz Jan 25 '23 at 15:59
  • Get the array of values : $links | Foreach $_.Value – jdweng Jan 25 '23 at 16:28
  • @jdweng, Mathias's comment shows how to properly extract the `.Order` property values. Leaving aside that there's no `.Value` property, use of `$_` _outside a script block_ is meaningless (unless it refers to the `$_` value of an _enclosing_ script block). – mklement0 Jan 25 '23 at 16:35
  • Now that the array problem has been solved, the remaining problem - neglecting to use `-ExpandProperty` instead of `-Property` with `Select-Object` when trying to extract a single property _value_ - as already noted in the comments, is explained in more detail in the linked duplicate. – mklement0 Jan 25 '23 at 16:37
  • @mklement0 thank you. i changed it to Select-Object -ExpandProperty Order and the error goes back to "New-GPLink : Cannot convert 'System.Object[]' to the type 'System.Int32' required by parameter 'Order'. Specified method is not supported." error that was in the original question. maybe I am not understanding as I am still learning. – NuckinFutz Jan 25 '23 at 16:47
  • @NuckinFutz, you need _both_ `-ExpandProperty Order` _and_ - depending on your business logic - (a) either a _loop_ over the resulting multiple values or (b) some way to select _one_ value from among the multiple ones. – mklement0 Jan 25 '23 at 16:50
  • @mklement0 ok. I am trying to loop through to get the target GPOs link order for all OUs in specified OU. since the target GPOs link or is different in each OU, then from there trying to assign the new GPO the link order number that the target GPO has in each OU since the target GPO has a different link order in each OU – NuckinFutz Jan 25 '23 at 16:54
  • Armed with the knowledge of how to avoid the two instances of type mismatch, I suggest you ask a _new_ question that focuses on the remaining problem. – mklement0 Jan 26 '23 at 13:52
  • 1
    @mklement0 somehow or another i lost the other question, not sure if issue on my end or what but it is back and i have actually figured it all out and have a working script thanks to all yalls help – NuckinFutz Feb 02 '23 at 20:55

0 Answers0