Last active
August 29, 2015 14:21
-
-
Save bgelens/eaf45d229876279e9da8 to your computer and use it in GitHub Desktop.
SMA Killing lingering sessions
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
#ChildRunbook to stop lingering sessions which is called inline | |
workflow Stop-LingeringSession { | |
param ( | |
[Parameter(Mandatory)] | |
[Int] $ProcessId, | |
[Parameter(Mandatory)] | |
[String] $Server, | |
[Parameter(Mandatory)] | |
[PSCredential] $Credential | |
) | |
try { | |
$Null = inlinescript { | |
$CimSession = New-CimSession -ComputerName $using:Server -Authentication CredSsp -Credential $using:Credential | |
if ($process = Get-CimInstance -ClassName win32_process -CimSession $CimSession -Filter "ProcessId = '$using:ProcessId'") { | |
$process | Invoke-CimMethod -MethodName terminate | Out-Null | |
} | |
$CimSession | Remove-CimSession | |
} | |
} | |
catch { | |
# Do nothing | |
} | |
} | |
#Runbook with inlinescript activity | |
#When runbook is finished, the associated PID is killed by Stop-LingeringSession childrunbook | |
workflow Test-Runbook { | |
[OutputType([PSCustomObject])] | |
param ( | |
[Parameter(Mandatory)] | |
[string] $MyInput | |
) | |
Write-Verbose -Message 'Running Runbook: Test-Runbook' | |
Write-Verbose -Message "MyInput: $MyInput" | |
$Creds = Get-AutomationPSCredential -Name 'TestCredentials' | |
$Server = Get-AutomationVariable -Name 'MyServer' | |
$OutputObj = [PSCustomObject] @{} | |
try { | |
[PSCustomObject]$ResultObj = inlinescript { | |
$ErrorActionPreference = 'Stop' | |
$VerbosePreference = [System.Management.Automation.ActionPreference]$Using:VerbosePreference | |
$DebugPreference = [System.Management.Automation.ActionPreference]$Using:DebugPreference | |
$InlineObj = [PSCustomObject]@{} | |
Add-Member -InputObject $InlineObj -MemberType NoteProperty -Name 'ProcessId' -Value $PID -Force | |
Add-Member -InputObject $InlineObj -MemberType NoteProperty -Name 'HostName' -Value "$env:COMPUTERNAME.$env:USERDNSDOMAIN" -Force | |
Add-Member -InputObject $InlineObj -MemberType NoteProperty -Name 'MyInput' -Value $using:MyInput -Force | |
return $InlineObj | |
} -PSComputerName $Server -PSCredential $Creds | |
if ($ResultObj.ProcessId) { | |
Stop-LingeringSession -ProcessId $ResultObj.ProcessId -Server $ResultObj.HostName -Credential $Creds | |
} | |
} | |
catch { | |
Write-Error -Message "Exception happened in runbook Test-Runbook: $($_.Message)" -ErrorAction Continue | |
Add-Member -InputObject $OutputObj -MemberType NoteProperty -Name 'error' -Value $_.message | |
} | |
Add-Member -InputObject $OutputObj -MemberType NoteProperty -Name 'MyInput' -Value $ResultObj.MyInput -Force | |
if ($ResultObj.ProcessId) { | |
Stop-LingeringSession -ProcessId $ResultObj.ProcessId -Server $ResultObj.Hostname -Credential $Creds | |
} | |
return $OutputObj | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment