Wednesday, June 29, 2016

[Presenting] SQL Saturday San Antonio, here I come!

Woot!  August 13th, see you there!  Looking through the sessions, there's going to be a ton of awesome.  R services, Hekaton, Window Functions, BIML, IaaS, Query Store, Event Notifications(plug, plug) and more! 

And looking at just part of the lineup makes me happy - Amy Herold, Tim Costello, Tim Mitchell, Steve Jones, John Sterrett... holy crap, Boles is going to be there?! And Kalen Delaney?!!?!! And I'm not presenting against them?!  Squee!!






@SQLSatSanAntone and #SQLSatSA



Powershell - kill processes remotely

Simple problem: we had to replace the config files for an app that hits the database, pointing it at a CNAME. 

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.

#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
}
}