1

I wanted to query [adsisearcher] to get me the OU info for a few servers for servers in trusted & non trusted domains.

$session = New-PSSession -ComputerName icvmm02

Invoke-Command -Session $session -ScriptBlock {
$compname= (Get-WmiObject "Win32_Computersystem" -namespace "root\CIMV2" -computername $comp).name
$searcher = [adsisearcher]"(&(ObjectCategory=computer)(Name=$compname))"
$ou= $searcher.FindOne() | Select-Object path
$serverou = $ou.path
}
$adou= (Invoke-Command -Session $session  -ScriptBlock { $serverou })
Get-PSSession | Remove-PSSession

for servers in trusted domains im passing a $cred = get credentials while creating a pssession, but when i run

$compname= (Get-WmiObject "Win32_Computersystem" -namespace "root\CIMV2" -computername $comp).name
$searcher = [adsisearcher]"(&(ObjectCategory=computer)(Name=$compname))"
$ou= $searcher.FindOne() | Select-Object path

it gives me an error as

Exception calling "FindOne" with "0" argument(s): "An operations error occurred.
"
At line:1 char:27
+     $ou= $searcher.FindOne <<<< () | Select-Object path
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
$serverou = $ou.path

Could some one please help me in sorting this out.

PowerShell
  • 1,991
  • 8
  • 35
  • 57

2 Answers2

1

I don't the cause but the ADSI interface just doesn't work in remote sessions. I can't even get the DN of the domain of a computer in my domain. I can get this to run locally but not remotely:

icm { ([adsi]"").distinguishedName }  #works

icm -Session $s -ScriptBlock { ([adsi]"").distinguishedName } #doesn't work
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Hi @shay levy thanks for your response, could you please let me know any other workaround for it?. Thanks – PowerShell Jul 21 '11 at 12:06
  • Thanks for your response, please do let me know if you find some workaround which can help me. thanks! Good Day! – PowerShell Jul 21 '11 at 12:42
  • hi @shay levy im using the quest ad commandlets to extract the OU info but im facing some error can you please help me http://stackoverflow.com/questions/6788966/unable-to-query-get-qadcomputer-info-in-remote-ps-sessions-powershell – PowerShell Jul 22 '11 at 10:55
0

Looks like there's a problem with the FindOne method call - you can find out more about the $searcher object with this;

$searcher | gm

the findOne method should be there with a list of the parameters it takes.

Though, I've just tried it on the type:

[adsisearcher] | gm | sort name

and there's no FindOne method - are you sure it's a method of adsisearcher?

Matt
  • 1,931
  • 12
  • 20