简体   繁体   English

将powershell命令的输出保存在变量中并在批处理脚本中使用它?

[英]Save the output of a powershell command in a variable and use it in batch script?

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. 我想要做的是保存powershell命令的输出(从批处理脚本运行)并在批处理脚本中使用它。

Can you please advise me what to do? 你能告诉我该怎么办?

The power shell comand is: 电源外壳命令是:

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

I want to use the output in the batch script. 我想在批处理脚本中使用输出。

PS PS

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. 如果我可以从cmd获取完整的计算机名/主机名/完全限定的域名(FQDN)而不是PowerShell,那将会更好。 But the full computer name is not the concatenation of the ComputerName and the UserDNSDomain variables. 但完整的计算机名称不是ComputerName和UserDNSDomain变量的串联。

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

You can do this in batch using nslookup which does the same DNS-search: 您可以使用执行相同DNS搜索的nslookup批量执行此操作:

for /f "tokens=1*" %%a in ('nslookup hostname ^| findstr /i "name"') do set return=%%b
echo Hello '%return%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM