简体   繁体   中英

Invoke command error, trying to get linked GPO's from specific OU from dc by script

i have a line in PS script which goes to DC and try to do this:

    $SWITCH = Invoke-Command -session $s -ScriptBlock {Get-ADOrganizationalUnit -filter *  -Property CanonicalName | Where-Object {$_.CanonicalName -eq $using:listBox2.SelectedItem}}

here are the the variables: $switch = "OU=office,DC=shahar,DC=local" $s = credentials to enter the DC with an admin account.

if i run this line on the DC without the invoke command it works. if i run other code lines with invoking commands using these credentials it works as well.

if i run this line on powershell (not as a part of script) it works.

if i replace $switch with the actual string ("DC=...") it still doesnt work. here is the error and i just cannot figure out whats the problem:

Cannot bind parameter 'Identity'. Cannot convert value "OU=Office,DC=shahar,DC=local" to type "Microsoft.ActiveDirectory.Management.ADOrganizationalUnit". Error: "Cannot convert the "OU=Office,DC=shahar,DC=local" value of type "Deserialized.Microsoft.ActiveDirectory.Management.ADOrganizationalUnit" to type "Microsoft.ActiveDirectory.Management.ADOrganizationalUnit"." + CategoryInfo: InvalidArgument: (:) [Get-ADOrganizationalUnit], ParameterBindingException + FullyQualifiedErrorId: CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADOrgani zationalUnit + PSComputerName: dc01

any help would be much appriciated!

by the way the point of this is to get from the DC the GPO's names that linked to that OU.

try this:

ipmo ActiveDirectory,GroupPolicy

$OU = 'OU=office,DC=shahar,DC=local'

(Get-ADOrganizationalUnit $OU).DistinguishedName
$LinkedGPOs = Get-ADOrganizationalUnit $OU | select -ExpandProperty LinkedGroupPolicyObjects
$LinkedGPOGUIDs = $LinkedGPOs | % {$_.Substring(4,36)}
$LinkedGPOGUIDs | % {Get-GPO -Guid $_ | select -ExpandProperty Displayname}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM