从批处理中调用PowerShell,并检索脚本中设置的临时环境变量的新值?

发布于 2024-09-13 11:47:33 字数 445 浏览 0 评论 0原文

我希望标题简洁,但以防万一:

我正在从批处理文件调用 PowerShell 脚本。我希望 PowerShell 脚本设置环境变量的值,并在 PowerShell 脚本完成时在批处理文件中提供该新值。

我知道可以在 PowerShell 中使用 $env 设置环境变量,但当 PowerShell 脚本终止时该值不会保留。我想这可能是因为 PowerShell 在单独的进程中执行。

我知道我可以返回退出代码并使用 %ErrorLevel%,但这只会给我数字,并且会发生冲突,因为 1 表示 PowerShell 异常而不是有用的数字。

现在,需要注意的是:我不希望环境变量持续存在。也就是说,我不希望它为用户或系统定义,因此我希望它在批处理文件退出后立即不可用。最终,我只是想将结果从 PowerShell 脚本传回调用批处理文件。

这可能吗?

预先感谢:)

尼克

I hope the title is concise, but just in case:

I am calling a PowerShell script from a batch file. I want the PowerShell script to set the value of an environment variable, and for that new value to be available in the batch file when the PowerShell script finishes.

I know that it is possible to set an environment variable using $env in PowerShell, but the value does not persist when the PowerShell script terminates. I imagine this is probably because PowerShell gets executed in a separate process.

I am aware that I can return an exit code and use %ErrorLevel%, but that will only give me numbers, and there will be a conflict, since 1 indicates a PowerShell exception rather than a useful number.

Now, here's the caveat: I don't want the environment variable to persist. That is, I don't want it to be defined for the user or system, and therefore I want it to be unavailable as soon as the batch file exits. Ultimately, I simply want to communicate results back from a PowerShell script to the calling batch file.

Is this possible?

Thanks in advance :)

Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妥活 2024-09-20 11:47:33

为了让 Keith 的使用 stdout 的想法发挥作用,您可以从批处理脚本中调用 powershell,如下所示:

FOR /F "usebackq delims=" %v IN (`powershell -noprofile "& { get-date }"`) DO set "d=%v"

有点尴尬,但它有效:

C:\>FOR /F "usebackq delims=" %v IN (`powershell -noprofile "& { get-date }"`) DO set "d=%v"
C:\>set d
d=August 5, 2010 11:04:36 AM

To get Keith's idea of using stdout to work, you can invoke powershell from your batch script like this:

FOR /F "usebackq delims=" %v IN (`powershell -noprofile "& { get-date }"`) DO set "d=%v"

A little awkward, but it works:

C:\>FOR /F "usebackq delims=" %v IN (`powershell -noprofile "& { get-date }"`) DO set "d=%v"
C:\>set d
d=August 5, 2010 11:04:36 AM
情徒 2024-09-20 11:47:33

我知道回答这个问题有点晚了,但我想尝试一下,以防有人需要更详细的解决方案。那么,就这样吧。

我创建了一个批处理函数,它将为您执行 ps 脚本并返回一个值,如下所示:

:: A function that would execute powershell script and return a value from it.
:: <PassPSCMD> pass the powreshell command, notice that you need to add any returning value witth Write-Host
:: <RetValue> the returned value
:RunPS <PassPSCMD> <RetValue>
  Powershell Set-ExecutionPolicy RemoteSigned -Force
  for /F "usebackq tokens=1" %%i in (`Powershell %1`) do set returnValue=%%i
  set "%2=%returnValue%"
Goto:eof
:: End of :RunPS function

现在,作为使用它的示例:

set psCmd="&{ Write-Host 'You got it';}"
call :RunPS %psCmd% RetValue
echo %RetValue%

这将显示在控制台屏幕上您明白了

作为一个更复杂的示例,我将添加:

让我们假设我们要检查虚拟机是Up还是Down,即它是打开还是关闭,所以我们可以执行以下操作:

 :CheckMachineUpOrDown <returnResult> <passedMachineName>
   set userName=vCenterAdministratorAccount
   set passWord=vCenterAdminPW
   set vCenterName=vcenter.somedmain.whatever
   set psCmd="&{Add-PSSnapin VMware.VimAutomation.Core; Connect-VIServer -server %%vCenterName%% -User %userName% -Password %passWord%; $vmServer = Get-VM %2;Write-Host ($vmServer.PowerState -eq 'PoweredOn')}"

   call :RunPS %psCmd% RetValue
   if "%RetValue%" EQU "True" (set "%1=Up") else (set "%1=Down")
 Goto:eof

:: A function that would execute powershell script and return a value from it.
:: <PassPSCMD> pass the powreshell command, notice that you need to add any returning value witth Write-Host
:: <RetValue> the returned value
:RunPS <PassPSCMD> <RetValue>
  Powershell Set-ExecutionPolicy RemoteSigned -Force
  for /F "usebackq tokens=1" %%i in (`Powershell %1`) do set returnValue=%%i
  set "%2=%returnValue%"
  Goto:eof
:: End of :RunPS function

现在,如何使用 :CheckMachineUpOrDown 函数?

只需按照以下示例操作即可:

set Workstation=MyVMName
call :CheckMachineUpOrDown VMStatus %Workstation%
echo %VMStatus%

如果虚拟机已打开,则显示“Up”;如果计算机关闭,则显示“Down”。

希望这有帮助。

谢谢

I know it's a little bit late to answer this question, but I would like to give it a try just in case any one needs more detailed solution. So, here it goes.

I created a batch function that would execute ps script for you and return a value, something like this:

:: A function that would execute powershell script and return a value from it.
:: <PassPSCMD> pass the powreshell command, notice that you need to add any returning value witth Write-Host
:: <RetValue> the returned value
:RunPS <PassPSCMD> <RetValue>
  Powershell Set-ExecutionPolicy RemoteSigned -Force
  for /F "usebackq tokens=1" %%i in (`Powershell %1`) do set returnValue=%%i
  set "%2=%returnValue%"
Goto:eof
:: End of :RunPS function

Now, as an example to use it:

set psCmd="&{ Write-Host 'You got it';}"
call :RunPS %psCmd% RetValue
echo %RetValue%

This will display on console screen You got it

As a more complicated example, I would add:

Let's assume that we want to check if a VM is Up or Down, meaning if it's powered on or off, so we can do the following:

 :CheckMachineUpOrDown <returnResult> <passedMachineName>
   set userName=vCenterAdministratorAccount
   set passWord=vCenterAdminPW
   set vCenterName=vcenter.somedmain.whatever
   set psCmd="&{Add-PSSnapin VMware.VimAutomation.Core; Connect-VIServer -server %%vCenterName%% -User %userName% -Password %passWord%; $vmServer = Get-VM %2;Write-Host ($vmServer.PowerState -eq 'PoweredOn')}"

   call :RunPS %psCmd% RetValue
   if "%RetValue%" EQU "True" (set "%1=Up") else (set "%1=Down")
 Goto:eof

:: A function that would execute powershell script and return a value from it.
:: <PassPSCMD> pass the powreshell command, notice that you need to add any returning value witth Write-Host
:: <RetValue> the returned value
:RunPS <PassPSCMD> <RetValue>
  Powershell Set-ExecutionPolicy RemoteSigned -Force
  for /F "usebackq tokens=1" %%i in (`Powershell %1`) do set returnValue=%%i
  set "%2=%returnValue%"
  Goto:eof
:: End of :RunPS function

Now, how to use :CheckMachineUpOrDown function?

just follow this example:

set Workstation=MyVMName
call :CheckMachineUpOrDown VMStatus %Workstation%
echo %VMStatus%

This will display Up if the VM is Powered On or Down if the machine is Off.

Hope this is helpful.

Thanks

百变从容 2024-09-20 11:47:33

从 PowerShell 捕获结果的最直接方法是在 PowerShell 中使用 stdout。例如,这会将日期保存到 cmd.exe 中的 d 环境变量

set d = powershell -noprofile "& { get-date }"

The most straight forward way to capture results from PowerShell is to use stdout in PowerShell. For example, this saves the date to the d env var in cmd.exe

set d = powershell -noprofile "& { get-date }"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文