Powershell Remove-WmiObject:指定的参数超出有效值范围。参数名称:路径
Powershell Remove-WmiObject: Specified argument was out of the range of valid values. Parameter name: path
$binaryPath = "aValidPath"
$user = "aUser"
$passwrd = "aPassword"
$serviceName = "aService"
if (Get-Service $serviceName -ErrorAction SilentlyContinue)
{
# using WMI to remove Windows service because PowerShell does not have CmdLet for this
$serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name='$serviceName'"
$serviceToRemove | Stop-Service
Remove-WmiObject $serviceToRemove
}
$secured = ConvertTo-SecureString $passwrd -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($user, $secured)
New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic -credential $creds
每次我尝试执行这段代码时,我都会得到:
Remove-WmiObject: Specified argument was out of the range of valid values.
Parameter name: path
我也想不通为什么。
如果将鼠标悬停在“$serviceToRemove”上,您将获得:
\*redacted*\root\cimv2:Win32_Service.Name="*redacted*"
PSComputerName:"*redacted*"
RunspaceId:*redacted*
PSShowComputerName:$false
$binaryPath 绝对准确,因为“新服务”部分使用得很好。尽管是该错误的原因,但删除不会删除该服务。并且 $serviceToRemove.delete() 不存在。
我能找到的每个示例网站都说使用这个:
Remove-WmiObject $serviceToRemove
或
$serviceToRemove.delete()
我修复此脚本的方法是改用此语法
Remove-WmiObject -path $serviceToRemove
我最近将所有内容都更新到了最新的 Powershell 版本,并为此使用了最新的“Visual Studio 代码”。注意:我使用老式 ISE 时收到了相同的错误消息。
$binaryPath = "aValidPath"
$user = "aUser"
$passwrd = "aPassword"
$serviceName = "aService"
if (Get-Service $serviceName -ErrorAction SilentlyContinue)
{
# using WMI to remove Windows service because PowerShell does not have CmdLet for this
$serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name='$serviceName'"
$serviceToRemove | Stop-Service
Remove-WmiObject $serviceToRemove
}
$secured = ConvertTo-SecureString $passwrd -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($user, $secured)
New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic -credential $creds
每次我尝试执行这段代码时,我都会得到:
Remove-WmiObject: Specified argument was out of the range of valid values.
Parameter name: path
我也想不通为什么。
如果将鼠标悬停在“$serviceToRemove”上,您将获得:
\*redacted*\root\cimv2:Win32_Service.Name="*redacted*"
PSComputerName:"*redacted*"
RunspaceId:*redacted*
PSShowComputerName:$false
$binaryPath 绝对准确,因为“新服务”部分使用得很好。尽管是该错误的原因,但删除不会删除该服务。并且 $serviceToRemove.delete() 不存在。
我能找到的每个示例网站都说使用这个:
Remove-WmiObject $serviceToRemove
或
$serviceToRemove.delete()
我修复此脚本的方法是改用此语法
Remove-WmiObject -path $serviceToRemove
我最近将所有内容都更新到了最新的 Powershell 版本,并为此使用了最新的“Visual Studio 代码”。注意:我使用老式 ISE 时收到了相同的错误消息。