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.
Two things I can see :
Firstly, your
paramsection is missing the$installvariablevariable.but it is present in your
ArgumentListSecondly, 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
-Argumentlistandparamlines. So I'd write them as :So the
-ArgumentListstates the variables defined in the body of the script and passed toInvoke-Command, whileparamsdefines how they are referenced within yourScriptBlock.