![共有プリンターが存在する場合は削除する PowerShell スクリプト](https://rvso.com/image/789283/%E5%85%B1%E6%9C%89%E3%83%97%E3%83%AA%E3%83%B3%E3%82%BF%E3%83%BC%E3%81%8C%E5%AD%98%E5%9C%A8%E3%81%99%E3%82%8B%E5%A0%B4%E5%90%88%E3%81%AF%E5%89%8A%E9%99%A4%E3%81%99%E3%82%8B%20PowerShell%20%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88.png)
質問して申し訳ありませんが、これを解決するには助けが必要です。\server\printer のように、多くのコンピューターにプリンターがインストールされています。PowerShell スクリプトを使用してそれらを削除しようと思ったのですが、うまくいきません。次のようなことを考えていました。
$printers=Get-Printer | where { $_.Type -eq 'Connection'} | select name /* this part show me only \\server\printers */
$printers | ForEach-Object {
if ($printers) {
Remove-Printer -Name $printers -Confirm:$false
}
}
皆様、事前に感謝します。
答え1
これは、Remove-Printer コマンドレットにプリンターのリスト全体を提供しているためです。一度に 1 つのプリンターを指定する必要があります。
[...]
Remove-Printer -Name $printers -Confirm:$false
# Error here------------/\
[...]
foreach ループ内にいる場合は、$PSItem
または短縮エイリアスを使用して現在のプリンターを取得できます$_
。
$PSItem
(および$_
) は「ForEach ループ内の現在のオブジェクト」を意味します。
これは動作するはずです:
$printers=Get-Printer | where { $_.Type -eq 'Connection'} | select name /* this part show me only \\server\printers */
$printers | ForEach-Object {
if ($_) {
Remove-Printer -Name $_.Name -Confirm:$false
}
}