Search code examples
batch-filepsexecwmic

PSEXEC Passing commands and using IF loop


When I run my batch file to go through a huge pile of IPs and get some info IF if can connect to the remote cmd, it does not work.

If it cannot connect the echo does not work, and if it does connect, it opens the remote command shell and does nothing until I exit back to the local command shell.

cd \pstools

psexec.exe @C:\users\chargraves\desktop\inv.txt cmd /c

if errorlevel 0 (
wmic csproduct get version >> \\path\results.txt &
)

if errorlevel 1(
echo "Not accessible"
)

I also started trying powershell. But as soon as it gets into a remote command shell, it just hangs. IT doesn't input the wmic code..."

$computers = Get-Content C:\users\chargraves\Desktop\inv.txt    
foreach ($computer in $computers) {
        & "c:\pstools\psexec.exe" \\$computer "cmd"
        & "wmic csproduct get version >> \\share\results.txt"
    }

Solution

  • I was able to get this to works successfully and is much less complicated. Thank you.

    Batch 1

    cd \pstools
    psexec.exe @iplist.txt -c batch2.bat
    

    Batch 2

    wmic command 1 >> results.txt
    wmic command 2 >> results.txt
    wmic command 3 >> results.txt