[UPDATE] PowerShell – exécution à distance avec WMI

Hello,

Aujourd’hui j’en profite pour mettre à jour le post ‘PowerShell – exécution à distance avec WMI‘.

Voici le nouveau script, avec un peu plus de logging, et plus généralisé que la première version.

function Execute-ProgramRemotely
{
    <#
        .SYNOPSIS
        This function will execute a program on a remote computer
        .DESCRIPTION
        This function uses WMI to create a process on a remote computer to run a program
        Modify the $tempPath and $installString variable to suit your needs
        .PARAMETER computername
        The computer name to query. Just one.
        .PARAMETER source
        The path of the folder containing all required executable files and dependencies that will be copied to the target computer before execution
        .PARAMETER command
        The executable or command to execute on the target computer, once the source files are copied
         .EXAMPLE
        Execute-ProgramRemotely -Computername 'mycomputer01' -source 'd:\sources\program\7zip' -command 'setup.exe /S'
    #>
 
    param(
    [Parameter(Mandatory=$true,Position=1)]
    [string]$computername,
    [Parameter(Mandatory=$true,Position=2)]
    [string]$source,
    [Parameter(Mandatory=$true,Position=3)]
    [string]$command) #end param

    # Check if the computer is up and running
    try {
        Test-Connection $computername -ErrorAction Stop | Out-Null
 
        # Define temp folder path
        $tempPath = "\\" + $computername + "\c$\temp\Execute-Program" 
               
        # Copy the setup file to the target computer
        Copy-item $source $tempPath -Recurse -Force -ErrorAction Stop
        Write-Verbose -Message "Copied $source in $tempPath" -Verbose
 
        # Define the installString then start the process on the remote computer
        #Start the process on the remote machine
        $installString = "$tempPath\$command"
        Write-Verbose -Message "Install string : $installString" -Verbose
        $process = Invoke-WMIMethod win32_process -Name create -ComputerName $computername -ArgumentList $installString
         
        #Get the process ID for the newly created process
        $id = $process.processid
        Write-Verbose -Message "Created process $process with $id" -Verbose

        #Create a DO WHILE loop to monitor for process exit
        $a = ""
 
        do {
            $a = Get-Process -ComputerName $computername | Where{$_.ID -match "$ID"}
            Start-Sleep -Seconds 2
            Write-Verbose -Message "Process $id still ongoing" -Verbose
        } 
        While ($a -ne $null)
        
        Write-Verbose -Message "Process $id finished, now cleaning temp files" -Verbose

        #Clean temp folder
        Remove-Item -Path $tempPath -Recurse -Force
        Write-Verbose -Message "Deleted $tempPath" -Verbose
    }
    catch {
        Write-Warning -Message "$($_.Exception.Message)"
        BREAK
    }
}

Hth ! 🙂

Laisser un commentaire