PowerShell - 远程升级脚本

问题描述

我正在尝试在一堆 Windows 7 机器上升级 Powershell,以便我可以进行其他远程安装等。我正在使用 Invoke-Expression 但我发誓这在没有它的情况下工作过一次。似乎没有任何一个等待选项。当我在本地运行 Invoke-Expression 时,它确实有效。我也试过启动过程。有没有更好的方法来获得关于它为什么没有运行的反馈?调试非常缓慢,因为它一直在猜测,这既是由于缺乏反馈,也是由于在远程机器上实际安装后台时很难分辨。脚本正在被复制。我试过不使用 Remove-item 以防我删除它太快。 $cred 是管理员。我不确定是否需要执行政策。

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)