我有一个接收密码来解密文件的脚本。某些密码具有单个反勾 ` 字符,并且脚本失败并出现密码错误错误。在使用硬编码密码进行测试时,我发现此密码失败:
$Passwd = "StringWith`Character"
但是使用这个密码是有效的:
$Passwd = "StringWith``Character"
我的问题是:什么 PowerShell 代码会添加额外的反勾号?我试过:
$Passwd = "StringWith`Character" # <-- entered by user
write-output "Passwd = $Passwd"
$new_pw = $Passwd.replace('`','``')
write-output "New_pw = $new_pw"
但是我得到的输出是:
Passwd = StringWithCharacter
New_pw = StringWithCharacter
令人困惑的是,这两个变量都没有显示有反勾号。这让我难住了。有什么推荐吗?
原来的变量赋值是问题所在。我应该使用单引号而不是双引号。此代码:
$Passwd = 'StringWith`Character' # <-- entered by user
$new_pw = $Passwd.replace('`','``')
write-output "Passwd = $Passwd"
write-output "New_pw = $new_pw"
产生此输出:
Passwd = StringWith`Character
New_pw = StringWith``Character