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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 06:08:04

问题


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?

The power shell comand is:

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

I want to use the output in the batch script.

P.S.

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.


回答1:


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



回答2:


You can do this in batch using nslookup which does the same DNS-search:

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


来源:https://stackoverflow.com/questions/25305421/save-the-output-of-a-powershell-command-in-a-variable-and-use-it-in-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!