以下脚本在最后一行失败Get-WmiObject : Invalid namespace
:
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential ("domain\user", $password )
Write-Host "Entering PS Session..."
Enter-PSSession -Computer hyperVServer -Credential $cred
Start-Sleep -s 5
$server = "servername"
$query = "SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" + $server + "'"
$VM = get-wmiobject -query $query -namespace "root\virtualization" -computername "."
但是,当我将其一一输入到控制台时,它可以毫无问题地运行。
由于一些时间问题,我添加了Start-Sleep
...会话需要几秒钟才能真正打开。任何想法为什么该行仅在作为脚本运行时才会失败?