无法find接受参数“WScript.CreateObject”的位置参数

试图执行:

Set objWshShell = CreateObject("WScript.Shell") 

出错:

 [![PS C:\Windows\system32> Set objWshShell = WScript.CreateObject("WScript.Shell") Set-Variable : A positional parameter cannot be found that accepts argument 'WScript.CreateObject'. At line:1 char:1 + Set objWshShell = WScript.CreateObject("WScript.Shell") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) \[Set-Variable\], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetVaria bleCommand][1]][1] 

然而这工作:

 $WSH = New-Object -Com WScript.Shell $WSH.Run("explorer.exe""") 

我试图运行这需要壳和上下文:

 Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run """%PROGRAMFILES(x86)%\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe""" WScript.Sleep 3000 WshShell.AppActivate "Cisco AnyConnect Secure Mobility Client" WshShell.SendKeys "{TAB}" WshShell.SendKeys "{TAB}" WshShell.SendKeys "{ENTER}" WScript.Sleep 5000 WshShell.SendKeys "PASSWORD" WshShell.SendKeys "{ENTER}" 

在我看来,你只是遇到麻烦翻译VBS代码到PowerShell的麻烦。 有一些微妙的语法差异,例如:

 $WSH = New-Object -Com wscript.shell $WSH.run("executable") $WSH.AppActivate("name") $WSH.SendKeys("key to send") 

您将使用powershell的睡眠:

 Start-Sleep -Seconds 5