I'm trying to run this script with remote powershell sessions on multiple computers, but it's not working

46 Views Asked by At

I have a very simple script that I would like to run on multiple computers with remote powershell. I'm currently I'm using it on my company's GUI based remote powershell solution for a single computer connection at a time, but they have no way of doing mass sessions with the application.

When I run what I have now, it just displays the Write-Host commands and no errors pop up. When I check the computer to see if the software has been updated, it was not updated. I'm not sure where it's going wrong. Here is what I have:

$computers = "C:\Examplepath\computernames.txt\"

$variable1 = "This first variable contains organization/registration keys for the software"
$variable2 = "This second variable contains the info to uninstall the existing version of the application for msiexec"
$installPath = "This contains the path of the application installer on the computer"

foreach ($computer in $computers) {
    Write-Host "Executing on $computer..."

    $session = New-PSSession -ComputerName $computer

    Invoke-Command -Session $session -ScriptBlock {
        param ($variable1, $variable2, installPath)

        Write-Host "Uninstalling existing software version..."
        $uninstallCommand = "msiexec.exe"
        $uninstallArguments = "variable2"
        Start-Process -FilePath $uninstallCommand -ArgumentList $uninstallArguments -Wait

        Write-Host "Now installing the software..."
        $installArguments = "variable1"
        Start-Process -FilePath $installPath -ArgumentList $installArguments
    } -ArgumentList $variable1, $variable2, $installPath

    Remove-PSSession -Session $session
}

Any assistance would be greatly appreciated, I feel like this is so simple but I can't seem to get it.

1

There are 1 best solutions below

1
Keith Langmead On

Two things I can see :

Firstly, your param section is missing the $installvariable variable.

Invoke-Command -Session $session -ScriptBlock {
    param ($variable1, $variable2)

but it is present in your ArgumentList

} -ArgumentList $variable1, $variable2, $installPath

Secondly, and I'm not sure whether this is, required / used to be required in older PS versions / just how it was done in all the examples I saw when I first did this and I've never changed my code, but I think you can't use the same variable names in both the -Argumentlist and param lines. So I'd write them as :

Invoke-Command -Session $session -ScriptBlock {
    param ($var1, $var2, $instPath)

    Write-Host "Uninstalling existing software version..."
    $uninstallCommand = "msiexec.exe"
    $uninstallArguments = "var2"
    Start-Process -FilePath $uninstallCommand -ArgumentList $uninstallArguments -Wait

    Write-Host "Now installing the software..."
    $installArguments = "var1"
    Start-Process -FilePath $instPath -ArgumentList $installArguments
} -ArgumentList $variable1, $variable2, $installPath

So the -ArgumentList states the variables defined in the body of the script and passed to Invoke-Command, while params defines how they are referenced within your ScriptBlock.