Running Scripts On Remote PCs

I often have situations where I want to run some commands on a remote PC in the environment, or even on a series of PCs.  There is a fairly straightforward method to run script remotely by reading a list of computers from a text file and supplying Admin credentials.  It took me a bit to piece these together so I thought I would share.

Let’s say we want to install a number of programs on some existing machines and we don’t have something cool like SCCM available.  I will post the script and then describe what the pieces do.

Install_Script

Shameless Self Promotion and then The Good Stuff

##Top of Script
###################################################################
# Written by Roberto Henriquez http://www.itbuki.com #
###################################################################

# This will prompt for credentials when the script is run. Generally these will be admin credentials that have admin rights on the target machines
$credential = Get-Credential

#This will read the list of target computers from a text file. One computer per line, please.
$YourFile = Get-Content ‘C:\scripts\computer_targets.txt’

# This establishes variables for a file share to run the installs from using the credentials entered
$psdrive = @{
Name = “PSDrive”
PSProvider = “FileSystem”
Root = “\\FILESERVER\FileShare”
Credential = $credential
}

# This runs through each computer list in the text file. One computer per line, please.
foreach ($computer in $YourFile)
{

# This will print the target computer name on the screen
write-host “${computer}”

# This will attempt to start the Windows Remote Management service. This service MUST be running for this to work. If you have WinRM running automatically, you can eliminate this step.
Get-Service -Name WinRM -ComputerName $computer | Set-Service -Status Running

# Here’s the magic sauce. This will run everything inside the brackets like you are sitting at a PowerShell window on the remote computer
Invoke-Command -ComputerName $computer -ScriptBlock {

# This section uninstalls software. You can use this to search for any number of products simply by changing what’s inside the single quotes after Name.
$p = gwmi -Class win32_Product -Filter “Name =’OpenOffice 4.1.5′”
$p.Uninstall()

# This section creates a drive within the remote context to copy files from
New-PSDrive @using:psdrive

# This section installs programs. The Wait switch at the end makes sure that install has finished BEFORE moving on to the next install.
Start-Process -FilePath ‘\\FILESERVER\FileShare\Some Amazing Program\SAP.msi’ -ArgumentList ‘/quiet’,’/qn’,’/norestart’ -Wait
Start-Process “msiexec.exe” -ArgumentList “/i \\FILESERVER\FileShare\DeepBlueSea\DeepBlueSea.2.8.583m.msi /quiet” -Wait

# This is an example of copying a directory and it’s contents to the target system and then running the install from the local location
Copy-Item -Path \\FILESERVER\FileShare\SomeBigProgram -Recurse -Destination c:\temp\ -container
Start-Process c:\temp\SomeBigProgram\install.bat -Wait

# This command runs an MSP file to update an existing installed program
Start-Process “msiexec.exe” -ArgumentList “/update \\FILESERVER\FileShare\BobsProgram\BobsProgram_Patch7.msp /qn” -Wait

# This shows some more switches that sometimes need to be used.
Start-Process -FilePath ‘\\FILESERVER\FileShare\Nonesense Components\BNware\Windows\BNware-Client-4.3.0-24601.exe’ -ArgumentList ‘/s’,’/v’,’/qn REBOOT=ReallySupress’ -Wait

# Very Important to make sure and remove the drive mounting before ending the script. If you do not, rerunning the script will frequently fail till the target machine is rebooted.
Remove-PSDrive psdrive
}
}

FIN

There are several ways to tackle problems, but this is the way I have had much luck with. I hope you find this useful.  Please comment and let me know how this works for you.

Roberto

The names and places have been changed to protect the innocent (and the guilty). No electrons were harmed during this explanation.