Skip to content

Instantly share code, notes, and snippets.

@mbourgon
Created June 29, 2016 15:47
Show Gist options
  • Save mbourgon/58e41449b7501ebca81a986ec5e4d339 to your computer and use it in GitHub Desktop.
Save mbourgon/58e41449b7501ebca81a986ec5e4d339 to your computer and use it in GitHub Desktop.
Kill app on remote computer using powershell, then replace files
#get the list of servers to tackle
foreach ($Server in Get-Content "C:\UpdateUsers.txt")
#now do each server
{
#making a \\server and server variable
$server2 = $Server -replace ("\\","")
#Which server is it updating
Write-host "Working on $Server2"
#Killing the App
#get the app info and assign that object to the variable. Then we can use methods on it.
#could also use gwmi -query "select * from win32_process where name = 'outcustomapp.exe'" -computername $server2
$processes = Get-WmiObject -Class Win32_Process -ComputerName $server2 -Filter "name='ourcustomapp.exe'"
#using the WMI methods to terminate and get the PID. We iterate since people can have multiple instances running
foreach ($process in $processes) {
$returnval = $process.terminate()
$processid = $process.handle
if($returnval.returnvalue -eq 0) {
write-host "The process $ProcessName `($processid`) terminated successfully"
}
else {
write-host "The process $ProcessName `($processid`) termination has some problems"
}
}
#set variables to replace the files, both current and backup
$oldfile = $Server + "\" + "c"+"$"+"\ourcustomapp\config.xml"
$newfile = $Server + "\" + "c"+"$"+"\ourcustomapp\config_backup.xml"
$oldfile2 = $Server + "\" + "c"+"$"+"\ourcustomapp\config2.xml"
$newfile2 = $Server + "\" + "c"+"$"+"\ourcustomapp\config2_backup.xml"
#Backup / Update file; check if file is there. If so, modify.
if (test-path $oldfile) {
copy-item $oldfile $newfile
(get-content $oldfile) -ireplace ('oldservername','newshinycname') | out-file $oldfile
}
#Backup / Update file; check if file is there. If so, modify.
if (test-path $oldfile2) {
copy-item $oldfile2 $newfile2
(get-content $oldfile2) -ireplace ('oldservername','newshinycname') | out-file $oldfile2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment