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