Skip to content

Instantly share code, notes, and snippets.

@mbourgon
Created April 28, 2015 17:46
Show Gist options
  • Save mbourgon/f7bffb2bea9553ddf762 to your computer and use it in GitHub Desktop.
Save mbourgon/f7bffb2bea9553ddf762 to your computer and use it in GitHub Desktop.
Powershell - Process Uptime for multiple machines, saved to a table
. C:\powershell_scripts\out-datatable.ps1
. C:\powershell_scripts\write-datatable.ps1
$server_repository = 'myrepo'
$database_repository = 'repodb'
#here we create $starttime, then it will get the values when passed through the for-each. Could just do inline, but it's a clever concept so leaving it here.
$StartTime= @{n='StartTime';e={$_.ConvertToDateTime($_.CreationDate)}}
#get list of servers we want to look at
$serverlist = invoke-sqlcmd -serverinstance $server_repository -database $database_repository `
-query "SELECT server FROM dbo.serverlist WHERE Connect = 1"
$serverlist|%{
#can't get it to select out otherwise; stupid datatables
$computer = $_.server
#use get-wmiobject to get details about "myprocess.exe", then pull in other details,
# round off the number of hours it's been running, etc., etc.
#The ManagementDateTimeConverter is needed for WMI DMTF dates, as per
# http://stackoverflow.com/questions/29838688/hours-between-two-times-within-a-select
$details_table = gwmi win32_process -cn $_.server -filter "Name='myprocess.exe' AND CreationDate IS NOT NULL" |
select @{Name = 'Servername'; Expression = {"$computer"}},$StartTime, processid, path `
, @{Name = 'HoursUptime'; Expression = {[math]::Round(([datetime](get-date) - [System.Management.ManagementDateTimeConverter]::ToDateTime($_.CreationDate)).TotalHours)}} |
out-datatable
Write-DataTable -ServerInstance $server_repository -Database $database_repository -TableName Scheduler_PID_Info -Data $details_table
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment