Remotely execute cmd.exe commands on multiple computers

If it works in cmd.exe , you can remotely execute on multiple Computers . This script invokes whatever command you can use in cmd.exe on one or more computers
you input the command you'd like to run as a screen input when you run the script, you can use all cmd.exe command like [del,ipconfig /flushdns,ipconfig /registerdns,gpupdate /force ,notepad.exe,defrag c:, wuauclt /detectnow , powercfg, net start ,net stop,copy,arp,wscript.exe ....]

#============================================================================================== # Script Name : Run Remote cmd.exe Commands

# COMMENT: # this script invokes whatever command you can use in cmd.exe on one or more computers #you input the command name as a screen input when you run the script #you can use all cmd.exe command like [del,ipconfig /flushdns,ipconfig /registerdns,gpupdate /force ,notepad.exe,defrag c:, ... #..wuauclt /detectnow,powercfg,net start ,net stop,copy,arp,wscript.exe ....] #if you can do it from cmd.exe you can do it here on multiple computers at the same time #==============================================================================================

function Run-RemoteCMD {
param(
[Parameter(Mandatory=$true,valuefrompipeline=$true)]
[string]$compname)
begin {
$command = Read-Host " Enter command to run"
[string]$cmd = "CMD.EXE /c " +$command
}

process {
$newproc = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ($cmd) -ComputerName $compname

if ($newproc.ReturnValue -eq 0 )
{ Write-Output " Command $($command) invoked Sucessfully on $($compname)" }
# if command is sucessfully invoked it doesn't mean that it did what its supposed to do
#it means that the command only sucessfully ran on the cmd.exe of the server
#syntax errors can occur due to user input

}
End{Write-Output "Script ...END"}
}
$Compname = Get-Content "C:\Scripts\Logfile.txt" | Run-RemoteCMD

Comments

Popular posts from this blog

Enterprise CA option is greyed out / unavailable

Rearm the Office 2013 installation

MP has rejected registration request due to failure in client certificate