Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Powershell to run Psexec command

First post so please don't hurt me. I've searched around but can't seem to find a way to do what I want. I've made a script that copies a folder I have to numerous computers at \$computer\c$. In these folders is a batch file that runs an .exe. What I want to do is have Powershell pull from the same computers.txt that I used to copy the folder and then use psexec to run the batch file. I could do this all manually but scripting it seems to be a problem, here's what I thought would work but apparently not.

    $computers = gc "C:\scripts\computers.txt"

foreach ($computer in $computers) {
if (test-Connection -Cn $computer -quiet) {
    cd C:\pstools
    psexec \\%computer cmd
    C:\Folder\install.bat"
} else {
    "$computer is not online"
}

}

like image 474
noz3r0 Avatar asked Apr 16 '14 20:04

noz3r0


1 Answers

I realize this question is from 2014, and this answer will not directly address the question the user asked. But for people who come across this question these days, I want to throw out there that you don't need* to use PSExec if you're using PowerShell. Since you're already in PowerShell, just use Invoke-Command.

Syntax would be

Invoke-Command -ComputerName $Computer -ScriptBlock { C:\Folder\install.bat }

It's really that easy.

*Requires PowerShell remoting to be enabled on the target server.

like image 138
Benjamin Hubbard Avatar answered Oct 15 '22 21:10

Benjamin Hubbard