【问题标题】:Powershell SendKeys command not sending dataPowershell SendKeys 命令不发送数据
【发布时间】:2020-10-09 02:55:41
【问题描述】:

我是 PowerShell 的新手。我把我认为是一个简单的脚本放在一起。我正在使用 Cisco VPNCLI 创建一个 VPN 自动登录脚本。最后我遇到了一个问题,即没有使用命令 [System.Windows.Forms.SendKeys]::SendWait("$value{Enter}") 发送值。我希望我没有遗漏一些明显的东西。

$VPNclipath = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"
$VPN = "my.vpn.com"
$group = "GroupName"
$user = "DOMAIN\$env:username"
$encpass = Get-Content C:\Temp\VPN.txt
$password = ConvertTo-SecureString -String $encpass
$accept? = "y"


###Connnect to VPN###
Function VPNConnect()
{
Start-Process -FilePath $VPNclipath -ArgumentList "connect $VPN"
}

VPNConnect

Start-Sleep -m 2000
[System.Windows.Forms.SendKeys]::SendWait("$group{Enter}")
Start-Sleep -m 2000
[System.Windows.Forms.SendKeys]::SendWait("$user{Enter}")
Start-Sleep -m 2000
[System.Windows.Forms.SendKeys]::SendWait("$password{Enter}")
Start-Sleep -m 2000
[System.Windows.Forms.SendKeys]::SendWait("$accept?{Enter}")
Start-Sleep -m 2000

【问题讨论】:

  • 1) 如果尝试发送击键工作,我会感到惊讶。我会调查您使用的 VPN 连接工具是否支持其他命令行参数。 2) VPN 连接工具不太可能接受您尝试使用的加密密码。密码可能需要以纯文本形式传递(如果 VPN 工具允许您传递密码)。
  • PSGallery 上有一个 Powershell 模块,它添加了 cmdlet 来执行此类操作 - POSH-VPN。即使您不使用该模块,您也可以查看代码以显示您是如何执行此操作的。

标签: powershell


【解决方案1】:

如果您将 powershell 代码作为脚本运行,则需要通过加载您引用的 .NET Framework 类来启动脚本。

Add-Type -AssemblyName System.Windows.Forms

【讨论】:

  • 一直在编写脚本。我添加了 Add-Type -AssemblyName System.Windows.Forms 行,它开始发送值。
【解决方案2】:

这是更新后的脚本,添加了 Nas 建议的行。未完成,但它确实连接到 VPN。非常感谢纳斯

Add-Type -AssemblyName System.Windows.Forms
$vpnclipath = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility 
Client\vpncli.exe"
$VPN = "my.vpn.com"
$group = "1"
$user = "DOMAIN\$env:username"
$securepass = Import-Clixml -Path 'C:\Temp\VPN.xml'
$password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($securepass))
$accept? = "y"



###Close all Cisco Apps###
Stop-Process -Name vpnagent.exe -Force
Stop-Process -Name vpnui.exe -Force
Start-Sleep -m 2000



###Disconnect VPN###
Function VPNDisconnect()
{
Start-Process -FilePath $vpnclipath -ArgumentList "disconnect"
}
VPNDisconnect
Start-Sleep -m 2000

###Close all Cisco Apps###
Stop-Process -Name vpnagent.exe -Force
Stop-Process -Name vpnui.exe -Force
Start-Sleep -m 2000

###Connect to VPN###
Function VPNConnect()
{
Start-Process -FilePath $vpnclipath -ArgumentList "connect $VPN"
}

VPNConnect

Start-Sleep -m 2000
[System.Windows.Forms.SendKeys]::SendWait("$group{Enter}")
Start-Sleep -m 2000
[System.Windows.Forms.SendKeys]::SendWait("$user{Enter}")
Start-Sleep -m 2000
[System.Windows.Forms.SendKeys]::SendWait("$password{Enter}")
Start-Sleep -m 2000
[System.Windows.Forms.SendKeys]::SendWait("$accept?{Enter}")
Start-Sleep -m 2000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 2015-09-24
    • 2010-11-03
    • 2019-02-16
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多