带有$的powershell发送键无法正确工作

jpfvwuh4  于 2023-02-08  发布在  Shell
关注(0)|答案(2)|浏览(140)

powershell发送键与$不工作正确这里是代码
当我把密码和$ it类型作为变量放回时

$enter_password = Read-Host "Enter Your Password" -AsSecureString

$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($enter_password))

$wshell = New-Object -ComObject wscript.shell;

Sleep 3

$wshell.SendKeys($password) 

[System.Windows.Forms.SendKeys]::SendWait("$password")


密码1q2w3e$R%T^Y
我得到1q2w3e$R
我该怎么补救呢?

pbwdgjma

pbwdgjma1#

尝试转义特殊字符:

$enter_password = Read-Host "Enter Your Password" -AsSecureString

$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($enter_password))

$wshell = New-Object -ComObject wscript.shell;
$wshell.Run("notepad")
$wshell.AppActivate("notepad")
Sleep 3
$password = $password.Replace('%', '{%}').Replace('^','{^}')

$wshell.SendKeys("$password") 
write-host $password
jjjwad0x

jjjwad0x2#

如果输入包含方括号,请使用以下命令清除文本

$cleanString = ""
$string.toCharArray() | foreach {
    if ($_ -in $string){
    $cleanString = $cleanString + "{" + $_ + "}"
}
else{
    $cleanString = $cleanString + $_
    }
}

如果任何字符是关键字,请用括号将其转义

相关问题