0

该脚本工作正常,将切换服务器上的打印机并确定打印机是否共享 (TRUE)。但是如果服务器上的最后一台打印机碰巧没有被共享,则会出现问题,然后将共享更改为(False)。这会歪曲电子表格中的结果,说明服务器不是打印服务器,尽管它是。我需要了解如何创建一个循环,说明如果我们在服务器上找到一台共享打印机,则停止搜索并将服务器放置在标记为打印服务器的电子表格中。

我想输入类似 $Printers = gwmi Win32_Printer -computername $StrComputer if ($printers.shared -eq "True){# then stop searching that machine and place it in the speadsheet as a print server} Here is my code 的内容:

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

    # Populate Printer Sheet 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=$($StrComputer)$))"
        $ds.SearchScope = "SubTree"
        $r = $ds.FindOne()
        $r.Path
        $Sheet1.Cells.Item($intRow, 5) = $r.Path

            $intRow = $intRow + 1}
    }
4

1 回答 1

1

改变这个:

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

有了这个:

$isprinterserver = $false
$printers | % { if ($_.shared -eq $true) { $isprinterserver = $true; break} }
$Sheet1.Cells.Item($intRow, 4) = $isprinterserver
于 2012-11-23T06:11:47.363 回答