英文:
How can i use a cmd variable in a powershell command?
问题
我已经创建了一个简单的 .bat 文件。在这个批处理文件中,我有一个名为 urlExample 的变量,它等于 "example.com"。
在同一个批处理文件中,我想在一个 PowerShell 命令中使用这个变量 urlExample。
具体来说,考虑以下代码:
@echo off
set urlExample="example.com";
powershell -ExecutionPolicy Bypass -Command "& {$WebClient = New-Object System.Net.WebClient;$WebClient.DownloadFile($urlExample,"C:\.....somePath")}
如何在 WebClient 命令中使用 urlExample?
附注:我不想简单地将 URL 放在 DownloadFile 的第一个参数中。我想使用批处理变量传递它。
英文:
I have created a simple .bat file. In this batch file i have a variable named urlExample which is equal to "example.com".
In the same batch file i want to use this variable urlExample in a powershell command.
Specifically, consider the following code:
@echo off
set urlExample = "example.com"
powershell -ExecutionPolicy Bypass -Command "& {$WebClient = New-Object System.Net.WebClient;$WebClient.DownloadFile($urlExample,"C:\.....somePath")
How could i achieve using the urlExample inside the WebClient command?
P.s. I don't want to simply put the url in the DownloadFile's first argument. I want to pass it with a batch variable.
Thanks in advance
答案1
得分: 3
-
在
cmd.exe
中,所有 变量也都是 环境 变量,比如你的情况下的%urlExample%
,以及 子进程,比如调用powershell.exe
,Windows PowerShell CLI,会 继承 环境变量。- 与此相反,PowerShell 还具有仅限于该会话的 shell(-only) 变量 (例如,
$urlExample
),而 环境 变量必须通过env
命名空间访问 (例如,$env:urlExample
- 参见概念性的 about_Environment_Variables 帮助主题)。
- 与此相反,PowerShell 还具有仅限于该会话的 shell(-only) 变量 (例如,
-
尽管你可以在
cmd.exe
那一侧使用字符串插值来“嵌入”cmd.exe
定义的环境变量的 值,通过将%urlExample%
嵌入-Command
参数,但更健壮的方法是让 PowerShell 访问环境变量,通过引用$env:urlExample
。
因此:
@echo off
:: 注意:名字和值周围没有空格,使用双引号
set "urlExample=example.com"
:: 注意对 $env:urlExample 的引用
:: 嵌入的双引号字符被转义为 \"
powershell -ExecutionPolicy Bypass -Command "$WebClient = New-Object System.Net.WebClient; $WebClient.DownloadFile($env:urlExample, \"C:\.....somePath\")"
注意:
-
-ExecutionPolicy Bypass
在这种情况下不是绝对必要的,因为这里没有涉及执行脚本 文件 (.ps1
) (无论是直接使用-File
还是间接使用-Command
参数)。 -
然而,考虑到在使用
-Command
时(powershell.exe
的默认参数),有效的 执行策略 也适用于不太明显的情况,比如(可能是隐式的)加载一个脚本模块(*.psm1
)和/或包含格式/类型定义数据(*.ps1xml
)的模块 - 使用-ExecutionPolicy Bypass
是一种良好的习惯,以确保可预测的执行,假设你信任你要调用的代码。 -
正如Compo指出的,确保可预测的执行环境的另一个好习惯是使用
-NoProfile
,它绕过了加载 PowerShell 的配置文件。除了防止配置文件对执行环境进行可能不必要/不想要的修改之外,绕过配置文件的加载还可以加快命令的执行速度。
英文:
<!-- language-all: sh -->
-
In
cmd.exe
, all variables are also environment variables, such as%urlExample%
in your case, and child processes - such as a call topowershell.exe
, the Windows PowerShell CLI, inherit environment variables.- By contrast, PowerShell also has shell(-only) variables (e.g.,
$urlExample
, limited to that session only), whereas environment variables must be accessed via theenv
namespace (e.g.$env:urlExample
- see the conceptual about_Environment_Variables help topic).
- By contrast, PowerShell also has shell(-only) variables (e.g.,
-
While you can use string interpolation on the
cmd.exe
side to "bake in" the values ofcmd.exe
-defined environment variables, by embedding%urlExample%
in the-Command
argument, it is more robust to let PowerShell access the environment variable, by referencing$env:urlExample
.
Therefore:
@echo off
:: Note: No spaces around "=", double-quote the name *and* the value.
set "urlExample=example.com"
:: Note the reference to $env:urlExample
:: Embedded " chars. are escaped as \"
powershell -ExecutionPolicy Bypass -Command "$WebClient = New-Object System.Net.WebClient; $WebClient.DownloadFile($env:urlExample, \"C:\.....somePath\")"
Note:
-
-ExecutionPolicy Bypass
isn't strictly needed in this case, given that no execution of a script file is (.ps1
) is involved here (whether directly with-File
or indirectly, as part of a-Command
argument). -
However, given that the effective execution policy also applies in less obvious scenarios when you use
-Command
(the default parameter ofpowershell.exe
)<sup>[1]</sup> - such as (possibly implicitly) loading a module that is either a script module (*.psm1
) and / or contains formatting / type-definition data (*.ps1xml
) - using-ExecutionPolicy Bypass
is a good habit to form to ensure predictable execution, assuming that you trust the code you're invoking. -
As Compo points out, another good habit to form to ensure a predictable execution environment is to use
-NoProfile
, which bypasses loading of PowerShell's profile files. In addition to preventing potentially unnecessary / unwanted modifications of the execution environment by the profiles, bypassing profile loading also speeds up the command.
<sup>[1] Note that pwsh
, the PowerShell (Core) CLI, now defaults to -File
.</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论