1

I am trying to upgrade Powershell on a bunch of Windows 7 boxes so I can do other remote installs and such. I am using Invoke-Expression but I swear this worked once before without it. There doesn't appear to be a Wait option for any of this. It does work when I run the Invoke-Expression locally. I also tried Start-Process. Is there a better way to get feedback on why it didn't run? The debugging is painfully slow because it has been a lot of just guessing, both due to lack of feedback and due to its hard to tell on the remote machine when its actually installing the background. The script is getting copied. I've tried without the Remove-item in case I was deleting it too fast. The $cred is admin. I'm not sure Execution Policy is necessary.

foreach ($comp in $computers) {
$comp.Name 
if(test-connection -ComputerName $comp.Name -quiet ){
    
   $Destination = "\\$($comp.Name)\c$\Temp\"
    copy-item -path "\\10.1.32.161\New Client Setups\WMF_5.1_PowerShell\*" -Destination $Destination -recurse -force
    "`t Copied"
    
    $session = Enter-PSSession $comp.Name -Credential $cred
        $results = Invoke-Command -ComputerName $comp.Name -ScriptBlock {
            Set-ExecutionPolicy RemoteSigned
            $ver = $PSVersionTable.PSVersion.Major
            "`t Powershell Version : $ver"
            if ($ver -lt "5"){
                "`tNeeds upgrade"
                $argumentList = @()
                $argumentList += , "-AcceptEULA"
                $argumentList += , "-AllowRestart"
                #Invoke-Expression "& 'C:\Temp\Windows7_Server2008r2\Install-WMF5.1.ps1' + $argumentList"
                Invoke-Expression 'C:\Temp\Windows7_Server2008r2\Install-WMF5.1.ps1 -AllowRestart -AcceptEULA'
                }
        
        } 
        $results
        Remove-item -Path "$Destination*" -recurse

    Exit-PSSession    
    Remove-PSSession -session $session
Furbs
  • 49
  • 5
  • You don’t want Enter-PSSession, that’s for interactive. Perhaps you meant New-PSSession? – Doug Maurer May 25 '21 at 18:02
  • If I remember right, it may not be possible to set the execution policy to remote from a remote connection. Also, you might try using Start-Transcript and save the powershell command results to a local file to see what is happening after the fact. – Lee Exothermix May 25 '21 at 18:24
  • Enter-PSSession has been working on a basic level. They seemed to have the same result earlier. @LeeExothermix would Start Transcript record the results of the Install-WMF5 script as well? – Furbs May 25 '21 at 19:08
  • 1
    If you want the Install-WMF5 script to store results, then start-transcript -Path "$path" would need to be added inside that script. I would put start-transcript in every script where you need a record of its actions and errors. – Lee Exothermix May 25 '21 at 20:27
  • A few things jump out at me when I review this code. First, get rid of enter/exit pssession. You probably need `-EnableNetworkAccess` added to the invoke-command call (if valid for 2.0). Your version comparison should be something like 5.0.0, not just 5. You should really provide details on what output you are getting, "it don't work" is difficult to assist with. Finally, consider doing this with DSC architecture instead, there are free options for this. – Colyn1337 May 25 '21 at 21:10
  • @Colyn1337 Thanks for the tips. I have gotten rid of the pssession, I thought it was necessary for invoke-command originally. When I posted this, I got no negative outputs in ISE so I didn't know where to go. I did find an event error on the computer saying that the MS PS1 script cannot access the installers it brought with them. I think it is a security issue now. Maybe there's a better place to copy to than C:/Temp or maybe there's a way to get the second ps script to run as admin locally. – Furbs May 26 '21 at 18:43

0 Answers0