简体   繁体   English

在 CMD 中使用时如何转义密码中的特殊字符

[英]How to escape the special characters in a password when using in CMD

I have this password, ;d#se#&4~75EY(H[SM"%cznGB .我有这个密码, ;d#se#&4~75EY(H[SM"%cznGB

I need to use this inside an automatically created command line command.我需要在自动创建的命令行命令中使用它。

(using it for TFS Release pipeline, to create a virtual directory and this password belongs to a service user and saved in a global variable as secret input, and TFS puts that password string in doublequotes when creating the command). (将它用于 TFS 发布管道,创建虚拟目录,此密码属于服务用户并保存在全局变量中作为秘密输入,TFS 在创建命令时将该密码字符串放在双引号中)。

Having a hard time escaping the scpecial characters in it.很难 escaping 中的特殊字符。

What I tried so far is to escape all alphanumeric characters by ^ and escaping % and " characters by doubling them. But so far, didn't have much luck:(到目前为止,我尝试的是通过^和 escaping %"字符将它们加倍来转义所有字母数字字符。但到目前为止,运气不佳:(

The list of failed attempts are these.失败的尝试列表是这些。

;d#se#&4~75EY^(H[SM""%%cznGB

;d#se#&4~75EY(H[SM\"%%cznGB

^;d^#se^#^&4^~75EY^(H^[SM""%%cznGB

^;d^#se^#^&4^~75EY^(H^[SM\"^%cznGB

^;d^#se^#^&4^~75EY^(H^[SM\"%%cznGB

^;d^#se^#^&4^~75EY^(H^[SM""^%%cznGB

;d#se#&4~75EY(H[SM"^^^%%cznGB

;d#se#&4~75EY(H[SM"^^^^%%cznGB

;d#se#&4~75EY(H[SM^""^%%cznGB

^;d^#se^#^&4^~75EY^(H^[SM^""^%%cznGB

^;d^#se^#^&4^~75EY^(H^[SM^""%%cznGB

;d#se#&4~75EY(H[SM""%%cznGB

Can you please give me a hand finding the correct escaped string to use it in a batch or in CMD as an inline command?您能否帮我找到正确的转义字符串以批量使用或在 CMD 中作为内联命令使用?

Before anyone downvotes this question because I posted a password, I edited many parts of it before posting.在有人因为我发布了密码而对这个问题投反对票之前,我在发布之前编辑了它的许多部分。 Just saying:)只是说:)

UPDATE:更新:

As I mentioned in the question, TFS creates a command text to set the virtual folder access credentials to IIS, for a specific web application.正如我在问题中提到的,TFS 为特定的 web 应用程序创建了一个命令文本以将虚拟文件夹访问凭据设置为 IIS。 The log shows the command text like this.日志显示这样的命令文本。

"C:\WINDOWS\system32\inetsrv\appcmd.exe" set vdir /vdir.name:"appfolder/Files" -physicalPath:"\\servername\virtualfolder" -userName:"sa_user@domain.com" -password:";d#se#&4~75EY(H[SM"%cznGB"

(I entered the password as it is, for this output) (我输入了密码,对于这个输出)

How interesting this all character escaping thing works, I continued serching.这个全字符 escaping 的工作多么有趣,我继续搜索。 and read everything I could find, The result is that all that should be escaped with in double quotes.并阅读我能找到的所有内容,结果是所有应该用双引号转义的内容。 are the double quotes themselves...是双引号本身...

(correct me if there's something I am missing, that was correct for my case) (如果我遗漏了什么,请纠正我,这对我的情况是正确的)

The resulting string that should be used in the TFS variable (and I tested it, it works:) is this:应该在 TFS 变量中使用的结果字符串(我测试过,它有效:) 是这样的:

;d#se#&4~75EY(H[SM""%cznGB

The only thing I added to the original password was another double quote near the double quote.我添加到原始密码中的唯一内容是双引号附近的另一个双引号。

That answer certainly helped me.这个答案肯定对我有帮助。 No wonder why experementing by yourselves preserves better than reading tons of articles:/难怪为什么自己体验比阅读大量文章更好:/

https://stackoverflow.com/a/15262019/2443719 https://stackoverflow.com/a/15262019/2443719

Thanks to everyone who commented and left an answer to this thread.感谢所有对此主题发表评论并留下答案的人。

rem // enable delayed expansion to expand other poison characters safely
@Echo off & setlocal EnableExtensions EnableDelayedExpansion
rem // escape % character by doubling
Set "PW=;d#se#&4~75EY(H[SM"%%cznGB"
"C:\WINDOWS\system32\inetsrv\appcmd.exe"  set vdir /vdir.name:"appfolder/Files" -physicalPath:"\\servername\virtualfolder" -userName:"sa_user@domain.com" -password:"!PW!"
Endlocal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM