如何将单个PowerShell输出字符串放入cmd变量? [英] How to put a single PowerShell output string into a cmd variable?

查看:69
本文介绍了如何将单个PowerShell输出字符串放入cmd变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PowerShell脚本,可以输出一个字符串值.我有一个cmd批处理脚本,该脚本需要执行PowerShell脚本并将单个PowerShell输出值放入批处理脚本中的变量中.我找到了导出到文件或读取文件的各种方法,但这不是我想要的.谢谢!

I have a PowerShell script that outputs a single string value. I have a cmd batch script that needs to execute the PowerShell script and place that single PowerShell output value into a variable in the batch script. I'm finding all sorts of methods for exporting to a file or reading a file but that's not what I want. Thanks!

(编辑)这是我尝试使用它的位置(以响应发布脚本):

(edit) Here's where I'm trying to use it (in response to posting the script):

@echo off
REM The next line puts the .ps1 output into the variable
REM and, obviously, this does not work
set pass_word=<C:\temp\PullPassword.ps1
tabcmd login -s "http://myserver.net" -u mylogon -p %pass_word%

(编辑)我在 中看到了OP的答案?批处理脚本中的值 ,还查看了foxdrive的答案,因此我开始使用FOR ... DO语句.我以为我对cmd相当满意,不知道为什么我的东西不起作用:

( edit ) I saw the answer by the OP here Getting Powershell variable value in batch script and also looked at foxdrive's answer so I started playing with the FOR...DO statement. I thought I was pretty good with cmd and couldn't figure out why what I had wasn't working:

for /f "delims=" %%a in ('powershell . "C:\temp\PullPassword.ps1"') do set val=%%a
echo  %a% 

当我在另一篇文章中查看foxdrive的完整答案时,我很震惊:%a%是错误的,我需要%val%!哦,真可惜!下面的作品:

When I was looking at foxdrive's full answer in the other post it struck me: %a% was wrong, I needed %val%! Oh, the shame! The below works:

@echo off
set mypath=C:\temp\PullPassword.ps1
for /f "delims=" %%a in ('powershell . "C:\temp\PullPassword.ps1"') do set pass_word=%%a 
tabcmd login -s "http://myserver.net" -u mylogon -p %pass_word%

所以我将归功于此,并将foxdrive的答案标记为正确,即使这是澄清我的错误的另一篇文章.

So I'll credit where it's due and mark foxdrive's answer correct, even though it was the other post that clarified my mistake.

推荐答案

这可能会有所帮助:它希望powershell脚本将文本输出到STDOUT,这是它出现的正常位置.

This may help: it expects that the powershell script outputs the text to STDOUT which is the normal place for it to appear.

@echo off
for /f "delims=" %%a in (' powershell "script.ps1" ') do set "var=%%a"

这篇关于如何将单个PowerShell输出字符串放入cmd变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆