Skip to content

Instantly share code, notes, and snippets.

@hypebeast
Last active October 12, 2015 18:08
Show Gist options
  • Save hypebeast/4066746 to your computer and use it in GitHub Desktop.
Save hypebeast/4066746 to your computer and use it in GitHub Desktop.
Monitor a remote process with PowerShell
# Returns the path of the executing script
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
# Process name
$processName = "notepad++"
# Remote computer IP
$remoteIP = "192.168.45.101"
# Output file name
$logFileName = "process.log"
# Full file name of the log file
$scriptPath = Get-ScriptDirectory
$logFilePath = $scriptPath + "\" + $logFileName
# Loop forever
do {
$logMessage = ""
$strTime = get-date -format G
# Get the process
$Process = Get-Process -name $processName -ErrorAction SilentlyContinue -ComputerName $remoteIP
if ($Process -ne $null) {
if ($Process | select -Property Responding) {
$logMessage = $strTime + " - Process is alive"
Write-Host $logMessage
} else {
$logMessage = $strTime + " - Process is not responding"
Write-Host $logMessage
}
} else {
$logMessage = $strTime + " - Process is not running"
Write-Host $logMessage
}
# Write log message to the log file
out-file -filepath $logFilePath -inputobject $logMessage -Append -encoding ASCII -width 80
# Sleep for some time
Start-Sleep -s 10
} While ($true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment