专有名称显示本地计算机而不是列表中的服务器

时间:2012-11-22 04:02:02

标签: powershell powershell-v2.0

我对此代码的结果有疑问。出于某种原因,脚本的Distriguished Name部分给出了我的工作站的值,而不是数组中服务器的专有名称......

    Function WMILookup {
    foreach ($StrComputer in $colComputers){
    $GenItems1 = gwmi Win32_OperatingSystem -Comp $StrComputer
    $Printers = gwmi Win32_Printer -computername $StrComputer


    # Populate General Sheet(1) with information
foreach ($objItem in $GenItems1){
    $Sheet1.Cells.Item($intRow, 1) = $StrComputer
    $Sheet1.Cells.Item($intRow, 2) = $objItem.Caption
    $Sheet1.Cells.Item($intRow, 3) = $objItem.CSDVersion
    }

foreach ($objItem in $Printers){
    $Sheet1.Cells.Item($intRow, 4) = $objItem.Shared
    }


$de = New-Object System.DirectoryServices.DirectoryEntry
$ds = New-Object System.DirectoryServices.DirectorySearcher
$ds.SearchRoot = $de
    $ds.Filter = "(&(objectCategory=computer)(objectClass=computer)  (samAccountName=$($env:Computername)$))"
$ds.SearchScope = "SubTree"
$r = $ds.FindOne()
$r.Path
$Sheet1.Cells.Item($intRow, 5) = $r.Path

    $intRow = $intRow + 1}
    }

1 个答案:

答案 0 :(得分:0)

它会告诉你你的问题:

$ds.Filter = "(&(objectCategory=computer)(objectClass=computer)  (samAccountName=$($env:Computername)$))"

$env:Computername是执行该功能的计算机名称。

也许你想要这个:

$ds.Filter = "(&(objectCategory=computer)(objectClass=computer)  (samAccountName=$($StrComputer)$))"