0

I'm trying to get Hostname, Date and Time from remote systems wherein I am not getting the desired results when I execute the server side batch file

Reference: Remote Side Batch File

@echo off
hostname.exe > __t.tmp
set /p host=<__t.tmp
del __t.tmp
set hst=%host%
set dt=%Date%
Set tm = %Time%
set RETURNVALUE= %host%, %Date%, %Time% 
ENDLOCAL&SET %~1=%RETURNVALUE%

This is returning all good if I echo the 'ReturnValue'

And Server Side Batch File (iplist.txt is the file containing IPs of remote machine)

@echo off
for /f "tokens=*" %%a in ('type iplist.txt') do (
set line=%%a
echo %line%
wmic /node:%line% process call create "cmd.exe D:\\Scripts\\RCMD_TS.bat RtVal"
echo %RtVal%
)
set %%a=
set %a=
set line=
set rt=

This returning the following:

D:\Scripts>ServerSideExecuteRemoteCommand.bat
ECHO is off.
call - Alias not found.
ECHO is off.
ECHO is off.
call - Alias not found.
ECHO is off.
ECHO is off.
call - Alias not found.
ECHO is off.
ECHO is off.
call - Alias not found.
ECHO is off.

What am I doing wrong here? Also can I get 3 values (Separate Hostname, Date and Time) instead of 1 (Return Value) I tried but it does not happen.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    Why are you using a ```for``` loop to parse each line of a text file of computer names, and then passing those to WMIC.exe? Just change ```wmic /node:%line%``` to ```wmic /node:@iplist.txt```, and WMIC.exe will use each of the computers listed in that file directly! Also, are you sure that each of those computers has access to a batch file ```D:\Scripts\RCMD_TS.bat```? – Compo Jul 28 '21 at 21:17
  • Hmm.. Didn't think of it... Nice. Will check and revert. Thanks. And Yes the client PCs have access to batch file as it resides on the client side only. Server side is just calling it. – Amit Gogna Jul 29 '21 at 06:55
  • I changed the batchfile. iplist.txt has 2 ips: 127.0.0.1 & 192.168.212.21. This is the result: Node - 127.0.0.1 Executing (Win32_Process)->Create() Method execution successful. Out Parameters: instance of __PARAMETERS { ProcessId = 11076; ReturnValue = 0; }; Node - 192.168.212.21 ERROR: Description = The RPC server is unavailable. The Batch file changed to: del /F D:\Scripts\HDT.txt wmic /node:@iplist.txt process call create "cmd.exe D:\\Scripts\\RCMD_TS.bat RtVal" echo %RtVal%>>D:\Scripts\HDT.txt set RtVal= HDT.txt is showing "Echo is Off" only – Amit Gogna Jul 29 '21 at 07:37
  • In your server-side batch file, the variables `line` and `RtVal` are assigned and read within the same block of code, namely a `for` loop, which requires [delayed expansion](https://ss64.com/nt/delayedexpansion.html). And `set %%a=` and `set %a=` after the loop is useless, because you cannot clear `for` meta-variables, which are not normal environment variables that you can set; but anyway, using `setlocal` at the top of your screipt and `endlocal` at the bottom ensures encironment changes to be eventually dismissed… – aschipfl Jul 29 '21 at 07:54

1 Answers1

0

This seems easily accomplished using PowerShell. This is not a full solution, but these might help. Of course, the network needs to be configured to support PowerShell remoting.

$HostName = [Net.DNS]::GetHostByAddress('10.24.12.35').HostName
Invoke-Command -ComputerName $HostName -ScriptBlock {Get-Date}
lit
  • 14,456
  • 10
  • 65
  • 119