Start-ProcessOnComputer.ps1


Description

Purpose

Starts a process on a remote computer.

Detailed Description

The Start-ProcessOnComputer function starts a process on a remote computer. It uses the Invoke-Command cmdlet to run a script block on the remote machine, and within that script block, it uses the Start-Process cmdlet to start the process.

Back to Top

Usage

Example 1

Start-ProcessOnComputer -ComputerName "Server01" -Name "notepad.exe"

Starts the notepad.exe process on the computer named Server01.

Back to Top

Notes

No additional notes.

Back to Top


Script

<#
.SYNOPSIS
Starts a process on a remote computer.

.DESCRIPTION
The Start-ProcessOnComputer function starts a process on a remote computer. 
It uses the Invoke-Command cmdlet to run a script block on the remote machine, 
and within that script block, it uses the Start-Process cmdlet to start the process.

.PARAMETER ComputerName
The name of the computer on which to start the process.

.PARAMETER Name
The name of the process to start.

.PARAMETER Credential
The credentials to use to start the process. If not provided, the current user's credentials are used.

.EXAMPLE
Start-ProcessOnComputer -ComputerName "Server01" -Name "notepad.exe"

Starts the notepad.exe process on the computer named Server01.
#>
function Start-ProcessOnComputer {
    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = "Enter the name of the computer.")]
        [string]$ComputerName,

        [Parameter(Mandatory = $true, HelpMessage = "Enter the name of the process to start.")]
        [string]$Name,

        [Parameter(HelpMessage = "Enter the credentials to use. If not provided, the current user's credentials are used.")]
        [System.Management.Automation.PSCredential]$Credential
    )

    process {
        if ($PSCmdlet.ShouldProcess("$ComputerName", "Start process $Name")) {
            try {
                Write-Verbose "Attempting to start process $Name on $ComputerName..."
                $scriptBlock = {
                    param($Name)
                    $process = Start-Process -FilePath $Name -PassThru
                    $process | Format-List * | Out-String
                }

                $invokeParams = @{
                    ComputerName = $ComputerName
                    ScriptBlock  = $scriptBlock
                    ArgumentList = $Name
                }
                if ($null -ne $Credential) {
                    $invokeParams.Credential = $Credential
                }
                $processDetails = Invoke-Command @invokeParams
                Write-Verbose "Process $Name started on $ComputerName successfully. Details: `n$processDetails"
            }
            catch {
                Write-Error -Message "Failed to start process on Server ${ComputerName}: $_"
            }
        }
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top