找不到接受参数“WScript.CreateObject”的位置参数

找不到接受参数“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}"

答案1

在我看来,您在将 VBS 代码转换为 powershell 时遇到了麻烦。有一些细微的语法差异,例如:

$WSH = New-Object -Com wscript.shell

$WSH.run("executable")
$WSH.AppActivate("name")
$WSH.SendKeys("key to send")

但是您将使用 powershell 的睡眠:

Start-Sleep -Seconds 5

相关内容