Having issues deleting a printer port as a WMI object
This
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()
Fails with the following:
Exception calling "Delete" with "0" argument(s): "Generic failure "
At line:14 char:1
+ $port.Delete()
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
However when a sleep is added for 10 seconds between $printer.Delete
and $port=gwmi...
it works!
Any suggestions on what it could be and how to fix it other than a sleep?