从run.exe运行Powershell命令

SwagiWagi

我正在尝试运行运行正常的命令,但是从run.exe运行

(new-object System.Net.WebClient).DownloadFile("host", "filename.ext");

尝试过:

powershell.exe -noexit -Command "(new-object System.Net.WebClient).DownloadFile(\"host\", \"filename.ext\");"

但是我得到这个错误:

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:1 char:1
+ (new-object System.Net.WebClient).DownloadFile("host ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

host和filename.ext只是占位符。

班德最大

考虑Invoke-WebRequest改用:

powershell.exe -c "Invoke-WebRequest -Uri http://server.domain.tld/path/to/file.ext -OutFile C:\path\to\save\download\file.ext"

如果您需要使用上面尝试过的旧方法,则可以正确地避免使用引号。下载调用应如下所示:

powershell.exe -noexit -Command "(new-object System.Net.WebClient).DownloadFile(`"host`", `"filename.ext`");"

上面命令的问题是,您尝试使用a\而不是a来对双引号进行转义`PowerShell中的转义字符为`当然,您始终可以通过对DownloadFile参数使用单引号来避免对字符串进行转义的需要

powershell.exe -noexit -Command "(new-object System.Net.WebClient).DownloadFile('host', 'filename.ext');"

请注意,在PowerShell中,双引号字符串可以在其中直接运行变量和子表达式,并且可以转义字符。单引号字符串会转义所有特殊字符,因此不能直接将变量或子表达式插入其中。但是,单引号和双引号字符串都支持格式字符串

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章