Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Working with WMI Events on a Remote Machine

$
0
0

I have a script (see following) that connects to a remote machine, runs a process, and attempts to wait for it to finish. The problem is it waits indefinitely. The "Wait-Event" call never returns.

#requires -version 2.0

Param (
    [Parameter(Mandatory=$True,Position=1)][string]$HostName
  , [Parameter(Mandatory=$True,Position=2)][string]$PathToExecutable
  , [Parameter(Mandatory=$True,Position=3)][string]$PathForOutput
)

Set-StrictMode -Version 2

##Verify that the target machine is accessible
Write-Verbose -Message "$(Get-Date -Format G): Verifying that ${HostName} is accessible."

If ((Test-Connection -ComputerName $HostName -Count 3 -Quiet) -eq $False) {
    Throw 'The target machine, ${HostName}, is inaccessible.'
}

##Verify that the output path is accessible
Write-Verbose -Message "$(Get-Date -Format G): Verifying that ${PathForOutput} is accessible."

If ((Test-Path -LiteralPath $PathForOutput -PathType Container) -eq $False) {
    Throw 'The output path, ${PathForOutput}, could not be found.'
}

$wmi_idfr_process_trace = 'trace_probe_' + [guid]::NewGuid()
$wmi_namespace = 'root\cimv2'

##Starting the probe on the remote machine
$process_probe = Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList $PathToExecutable -ComputerName $HostName -Namespace $wmi_namespace

Write-Verbose -Message "$(get-date -Format G): Process $($process_probe.ProcessID) was initiated on ${HostName}."

Try {
    ##Verifying the probe process is running.
    Get-Process -Id $process_probe.ProcessID -ComputerName $HostName -ErrorAction Stop | Out-Null

    ##Registering the probe process trace event.
    Write-Verbose -Message "$(get-date -Format G): Registering WMI process trace event on ${HostName}."
    $wmi_qry_process_trace = "SELECT * FROM Win32_ProcessStopTrace WHERE ProcessID=$($process_probe.ProcessID)"
    Register-WmiEvent -Query $wmi_qry_process_trace -SourceIdentifier $wmi_idfr_process_trace -ComputerName $HostName -Forward
    ##Waiting for the trace event to fire.
    Write-Verbose -Message "$(get-date -Format G): Creating wait on process trace event ${wmi_idfr_process_trace}."
    $event = Wait-Event -SourceIdentifier $wmi_idfr_process_trace
    Write-Verbose -Message "$(get-date -Format G): Event registered."

    ##Copying the probe's files
    #TBA

} Catch [System.Management.Automation.ActionPreferenceStopException] {
    Throw "The process could not be found.`n`n$error[0]"
} Catch {
    Throw "An unexpected error occured.`n`n$error[0]"
}

The executable on the remote machine is starting (verified by logging into the remote machine and verifying the process and PID). I just can't seem to get a handle on when it terminates.

What am I missing?


Adam


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>