Last active
August 29, 2015 14:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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