Skip to content

Instantly share code, notes, and snippets.

@brent20
Last active August 29, 2015 14:13
Show Gist options
  • Save brent20/ea05a4a5f00066e72873 to your computer and use it in GitHub Desktop.
Save brent20/ea05a4a5f00066e72873 to your computer and use it in GitHub Desktop.
PoweShell script to stop a remote pc's service, edit values in a remote PC's XML file, and restart the remote PC's service
#Get host from command line argument
Param (
[Parameter(Mandatory=$True)]
[ValidateNotNull()]
$remotepc
)
#Stop the service on the remote machine
Write-Host "STOPPING SERVICE...";
(get-service -ComputerName $remotepc -Name "Name of Service").Stop()
#Wait for the service to be stopped, up to 10 seconds, then continue script
(get-service -ComputerName $remotepc -Name "Name of Service").WaitForStatus('Stopped','00:00:10')
Write-Host "SERVICE STOPPED";
#Modify XML file
Write-Host "OPENING CONFIG.XML";
$path = "\\$remotepc\c$\path\to\file\config.xml"
[xml] $xml = Get-Content $path
#set values for the XML nodes you need. This uses the XPath of the value needed.
$xml.XMLRoot.Settings.Setting = "https://google.com"
$xml.XMLRoot.Settings.AnotherSetting = "email@address.com"
$xml.XMLRoot.Settings.SecretPassword = "LamePassw0rd"
#Add additional values as needed
#Save the file
$xml.save($path)
Write-Host "CONFIG.XML SAVED";
#Start the Service
(get-service -ComputerName $remotepc -Name "Name of Service").Start()
Write-Output "SERVICE RESTARTED";
#Write the remotepc's name to a new line in a text file to keep track of completions
Add-Content c:\folder\log.txt `n $remotepc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment