将打印机端口作为 WMI 对象删除时出现问题
这个
param(
[Parameter(Mandatory=$true)]
[string] $printerName
)
$printer=gwmi win32_Printer -filter "name='$printerName'"
$printer.Delete()
Write-Host $printer.portname
$port=gwmi win32_tcpipprinterport -filter "name='$($printer.portname)'" -EnableAllPrivileges
Write-host $port
$port.Delete()
失败并出现以下情况:
Exception calling "Delete" with "0" argument(s): "Generic failure "
At line:14 char:1
+ $port.Delete()
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
但是,当在 10 秒之间添加睡眠时$printer.Delete
,$port=gwmi...
它就可以工作了!
关于它可能是什么以及除了睡眠之外如何解决它的任何建议?