Powershellでネットワークプリンタを接続する方法
1 2 3 |
PS C:\> $WshNet = New-Object -ComObject WScript.Network PS C:\> $PrinterPath = "\\server.rootlinks.net\pr01" PS C:\> $WshNet.AddWindowsPrinterConnection($PrinterPath) |
デフォルトプリンタを設定
1 |
PS C:\> $WshNet.SetDefaultPrinter('pr01') |
デフォルトプリンタの設定には正確なプリンタ名が必要なので接続したあとに下記のコマンドレットを実行して確認する必要があります
1 |
PS C:\> (New-Object -ComObject WScript.Network).EnumPrinterConnections() |
参考サイト
Powershell – Connect network printers and set default
http://www.bewi.at/?p=895
またAddPrinterConnectionとAddWindowsPrinterConnectionの違いは
AddWindowsPrinterConnection – Google Groups
https://groups.google.com/forum/#!topic/microsoft.public.windows.powershell/KJhhwavcfhg
Try using WSH or WMI. I offline so I can’t test both methods but it should
do the job:$net = new-object -com wscript.network
## 1. adds a network printer to an MS-DOS printer port, such as LPT1
$net.addPrinterConnection “LPT1”, “\\Server\Print1”
– or –
## 2. add a remote windows-based printer connection
$net.addWindowsPrinterConnection “\\ServerName\PrinterName”WMI:
## add a new printer connection to a remote computer
$computer = “.”
$printer = gwmi -list | where {$_.name -eq “Win32_Printer”}
$printer.addPrinterConnection(“\\ServerName\PrinterName”)
とあります。う~~ん、なんとなくイメージは分かるような….(^^;
[Wscript.Network COM Object Member]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
PS C:\> New-Object -ComObject WScript.Network | gm TypeName: System.__ComObject#{24be5a31-edfe-11d2-b933-00104b365c9f} Name MemberType Definition ---- ---------- ---------- AddPrinterConnection Method void AddPrinterConnection (string, string, Variant, Variant, Variant) AddWindowsPrinterConnection Method void AddWindowsPrinterConnection (string, string, string) EnumNetworkDrives Method IWshCollection EnumNetworkDrives () EnumPrinterConnections Method IWshCollection EnumPrinterConnections () MapNetworkDrive Method void MapNetworkDrive (string, string, Variant, Variant, Variant) RemoveNetworkDrive Method void RemoveNetworkDrive (string, Variant, Variant) RemovePrinterConnection Method void RemovePrinterConnection (string, Variant, Variant) SetDefaultPrinter Method void SetDefaultPrinter (string) ComputerName Property string ComputerName () {get} Organization Property string Organization () {get} Site Property string Site () {get} UserDomain Property string UserDomain () {get} UserName Property string UserName () {get} UserProfile Property string UserProfile () {get} |