However, the file could be in use because the app was running. And the app only loads the file on startup. So we came up with this, which worked quite well. Note that we use WMI (see my other posts on it!) to grab the application info then a WMI method to terminate the app.
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 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 | |
} | |
} |
No comments:
Post a Comment