将powershell命令的输出保存在变量中并在批处理脚本中使用它吗? [英] Save the output of a powershell command in a variable and use it in batch script?

查看:261
本文介绍了将powershell命令的输出保存在变量中并在批处理脚本中使用它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是保存powershell命令的输出(从批处理脚本运行),并在批处理脚本中使用它.

What I am trying to do is to save the output of a powershell command (run from a batch script) and use it in the batch script.

您能告诉我该怎么办吗?

Can you please advise me what to do?

电源外壳的命令是:

[System.Net.Dns]::GetHostByName((hostname)).HostName

我想在批处理脚本中使用输出.

I want to use the output in the batch script.

如果我可以从cmd而不是从powershell获取完整的计算机名称/主机名/完全限定的域名(FQDN),那就更好了.但是完整的计算机名称不是ComputerName和UserDNSDomain变量的串联.

It will be even better if I can get the full computer name/hostname/fully qualified domain name (FQDN) from cmd and not from powershell. But the full computer name is not the concatenation of the ComputerName and the UserDNSDomain variables.

推荐答案

for /f "tokens=*" %%i in ('powershell /command "[System.Net.Dns]::GetHostByName((hostname)).HostName"') do set return=%%i
echo %return%

这篇关于将powershell命令的输出保存在变量中并在批处理脚本中使用它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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