Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 390
fanciulla Avatar asked Aug 14 '14 10:08

fanciulla


2 Answers

for /f "tokens=*" %%i in ('powershell /command "[System.Net.Dns]::GetHostByName((hostname)).HostName"') do set return=%%i
echo %return%
like image 190
Stephan Avatar answered Oct 11 '22 00:10

Stephan


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%'
like image 45
Frode F. Avatar answered Oct 10 '22 23:10

Frode F.