0

I am trying to deploy printers to the PCs in my organization through a powershell script. The script itself is deployed through group policy to run on machine startup. When a machine in the test OU boots it will sit at please wait indefinitely and the script never completes. The script works if you execute it manually. I won't go into the details of it but deploying printers directly through group policy is not an option.

Powershell Script

# Print File Server Path
$printsrv = "\\Servername\Deploy\"

# Check FTP Server Status
$file = ($env:computername + '.csv')
$num = 0
IF ((Test-Path ($printsrv + $file)) -eq $false) {
Do {
Start-Sleep -Seconds 5
$num = $num + 1
} Until ((Test-Path ($printsrv + $file)) -eq $true -or $num -eq 3)
} 
if ((Test-Path ($printsrv + $file)) -eq $false) {exit}

# Create Printers
$print = Import-Csv  ($printsrv + $file)
$num = 0
do {
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection($print.p_address[$num])
$num = $num + 1
} Until ($num -eq $print.Count)

#Set Default
$num = 0
Do {
$contains = $Print.Def_Bit[$num].Contains(1)
$Default = $print.P_Address[$num]
$num++
} until ($contains -eq $true -or $num -eq $print.Count) 
(New-Object -ComObject WScript.Network).SetDefaultPrinter($Default)
Remove-Item -Path ($printsrv + $file)

Csv file

#TYPE System.Data.DataRow
"P_Address","DEF_Bit"
"\\server\printer",""
"\\server\printer","True"
4

2 に答える 2

0

問題 (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection が問題でした

于 2013-03-15T20:09:10.147 に答える
0

この方法でもプリンターを追加できます (静的 wmi メソッド):

$printClass = [wmiclass]'win32_printer'
$printClass.AddPrinterConnection($printer)

「add-printer -connectionname」はお勧めしません。

于 2018-06-10T14:55:42.597 に答える