Skip to content

Advanced PSRP

Runspace Pooling for Efficient Remote Execution

Runspace pooling is a PSRP feature that reuses runspaces across multiple remote commands, reducing overhead and improving performance. A runspace pool manages a collection of runspaces that can be shared among remote sessions or commands, enabling efficient resource utilization for distributed tasks. Note: Runspace pools are created and managed locally, not on remote systems. Remote commands leverage existing pools rather than creating new ones.

Key Concepts

  • Runspace: An execution context for PowerShell commands.
  • Pool: A collection of runspaces that can be dynamically allocated and released.
  • Benefits: Reduces memory and CPU usage, especially for high-throughput tasks like bulk configuration changes.

Example: Creating and Using a Runspace Pool (PSRP-Compatible)

# Create a runspace pool locally (not on the remote system)
$runspacePool = [runspacefactory]::CreateRunspacePool(2, 5)
$runspacePool.Open()

# Execute remote commands using the local runspace pool
$scriptBlock = {
    $powerShell = [powershell]::Create()
    $powerShell.RunspacePool = $runspacePool
    $powerShell.AddScript({ Get-Service -Name "WinRM" })
    $result = $powerShell.Invoke()
}
Invoke-Command -ComputerName Server01 -ScriptBlock $scriptBlock

# Clean up
$runspacePool.Close()

Advanced Use Case: Parallel Remote Commands (PSRP-Compatible)

# Create a runspace pool locally (not on the remote system)
$runspacePool = [runspacefactory]::CreateRunspacePool(2, 5)
$runspacePool.Open()

# Define a script block for remote execution
$script = {
    Get-Process | Where-Object { $_.Name -like "svchost" }
}

# Use the pool to execute commands on multiple servers
$targets = @("Server01", "Server02", "Server03")
$results = @()
foreach ($server in $targets) {
    $powerShell = [powershell]::Create()
    $powerShell.RunspacePool = $runspacePool
    $powerShell.AddScript($script)
    $powerShell.AddParameter("ComputerName", $server)
    $results += $powerShell.Invoke()
}

# Clean up
$runspacePool.Close()

Remote Job Management with PSRP

Remote job management allows you to execute commands asynchronously on remote systems and monitor their progress. This is particularly useful for long-running tasks or workflows that require minimal user interaction. PSRP enables asynchronous execution via Invoke-Command without relying on local job systems. Note: PSRP's job management is distinct from local job persistence mechanisms like Start-Job.

Key Concepts

  • Jobs: Background processes that run independently of the parent session.
  • Asynchronous Execution: Commands can run without blocking the caller.
  • Job Persistence: Jobs are not persisted across sessions; they are transient.

Example: Starting and Monitoring a Remote Job (PSRP-Compatible)

# Start a remote job using PSRP's asynchronous execution (no -AsJob)
$job = Invoke-Command -ComputerName Server01 -ScriptBlock {
    Get-ComputerInfo | Select-Object -First 5
}

# Monitor job status (PSRP-specific tracking)
Get-Job -Name "RemoteSystemCheck"
Receive-Job -Job $job
Stop-Job -Job $job
Remove-Job -Job $job

Advanced Use Case: Job Persistence and Event Handling (PSRP-Compatible)

# Register an event to trigger a remote job when a service starts (PSRP-specific)
$scriptBlock = {
    Register-ObjectEvent -InputObject (Get-Service -Name "WinRM") -EventName "Start" -Action {
        $job = Start-Job -Name "ServiceStarted" -ScriptBlock {
            Write-Output "Service 'WinRM' has started."
        }
        Write-Host "Job '$job.Name' started."
    }
}

# Execute the script block on a remote system
Invoke-Command -ComputerName Server01 -ScriptBlock $scriptBlock

# Later, retrieve and manage the job
Get-Job -Name "ServiceStarted"
Receive-Job -Job "ServiceStarted"
Remove-Job -Name "ServiceStarted"

Key takeaways

  • Runspace pooling optimizes resource usage by reusing runspaces across remote commands, improving scalability for high-throughput tasks.
  • Remote job management enables asynchronous execution of commands via Invoke-Command without local job persistence, allowing administrators to monitor and manage long-running processes across distributed systems.
  • PSRP's support for these advanced techniques ensures efficient, scalable automation in enterprise environments.