当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 升级到 PowerShell 5.1 的两步指南

作者:精品下载站 日期:2024-12-14 12:37:32 浏览:17 分类:玩电脑

升级到 PowerShell 5.1 的两步指南


如果您决定安装 PowerShell 5.1,我已经创建了一个技术指南,其中包含一些脚本来为您自动执行此过程。 免责声明:本文中提到的所有代码均按原样。有些可能适用,有些可能不适用于您的环境,但是,如果您什么都没有,那么首先,它将作为您开始使用的模板。

如果您需要帮助查找正在运行的 PowerShell 版本,我建议您查看如何检查您的 PowerShell 版本(所有方法!)。

步骤#1:发现

执行企业范围的 PowerShell 升级的第一步是发现。目前的风景如何?如果不先了解某件事,你就无法改变它,令人惊讶的是,许多组织根本不知道他们拥有什么!

现有操作系统

第一个任务是了解您当前想要升级到 PowerShell 5.1 的操作系统。尽管在某些系统上,您可能已经决定安装 PowerShell 5.1,但自己并不知道。 PowerShell 5.1 作为 Windows 10 周年更新和 Windows Server 2016 RTM 版本中的捆绑工具发布,因此如果您拥有这些操作系统或更新版本中的任何一个操作系统,那么您就可以开始使用了。

对于其他人,请务必收集一份清单。没有像 SCCM 这样的系统管理工具?没问题!使用PowerShell来帮助PowerShell!如果您有 Active Directory,这里有一个简短的说明,可以让您鸟瞰其中的内容。

PS> Get-ADComputer -Filter "OperatingSystem -like 'Windows*'" -Properties OperatingSystem | group operatingsystem | sort name
Count Name                      Group
----- ----                      -----
922 Windows 10 Enterprise     XXXXXXXXXXXXXXXXXX
714 Windows 7 Enterprise      XXXXXXXXXXXXXXXXXX
23 Windows 7 Professional     XXXXXXXXXXXXXXXXXX
4 Windows 7 Ultimate          XXXXXXXXXXXXXXXXXX
2 Windows 8 Consumer Pre...   XXXXXXXXXXXXXXXXXX
1 Windows 8 Enterprise        XXXXXXXXXXXXXXXXXX

你的环境是什么样的?您有旧的 Windows XP 或 Server 2003 计算机吗?如果是这样,那你就不走运了。您要做的最好的事情是 PowerShell v2,但如果您的环境中仍然有其中一些,那么 PowerShell 是您最不用担心的!对于任何具有 Windows 7 SP1/Windows Server 2008R2 SP1 或更高版本的操作系统,都可以安装 PowerShell 5.1。

建立先决条件目标列表

在适用的操作系统上,PowerShell 5.1 在运行之前需要满足一些先决条件和要求。

  • 至少安装了 .NET Framework 4.5.2。这不是安装要求,安装仍将继续,但某些功能将无法使用,所以就这样做吧!
  • 不得安装 PowerShell 5.1 预览版
  • 在安装了 PowerShell v3 的 Windows 7 或 Server 2008R2 计算机上,您必须保存 PSModulePath 的值,或者先升级到 v4。
  • 在 Windows 7 或 Server 2008R2 计算机上,如果使用 DSC,请务必启用 WinRM。
  • 安装需要重新启动,因此请务必安排此时间

因为我很懒,而且我想让你也一样,所以我编写了一个小脚本,让你开始弄清楚你面前的先决条件是什么。对于 WinRM 要求,您可以在其中包含我的脚本中的代码,以及您正在接触的每台计算机。不过,请注意,这种方法要求每台计算机都启用 PowerShell 远程处理。

## This could be Active Directory, a text file, a SQL database, whatever$computers = Get-AdComputer -Filter *foreach ($c in $computers.Name) {	try {		$session = New-PSSession -ComputerName $c		$icmParams = @{			Session = $session		}		$output = @{        	ComputerName = $c		}    	## In case they're running Powerhell v3		$icmParams.ScriptBlock = { $env:PSModulePath; [Environment]::GetEnvironmentVariable("PSModulePath", "Machine") }		$output.PSModulePath = (Invoke-Command @icmParams) -split ';' | Select-Object -Unique | Sort-Object    	## Grab the existing version    	$icmParams.ScriptBlock = { $PSVersionTable.BuildVersion.ToString() }    	$output.PSModulePath = Invoke-Command @icmParams		## Check .NET Framework 4.5.2		if (Get-ChildItem -Path "$c\c$\windows\Microsoft.NET\Framework" -Directory | Where-Object {$_.Name -match '^v4.5.2.*' }) {			$output.DotNetGood = $true		} else {			$output.DotNetGood = $false		}		[pscustomobject]$output	} catch {	} finally {		Remove-PSSession -Session $session -ErrorAction Ignore	}

确保现有脚本将使用 PowerShell 5.1 运行

尽管 PowerShell 团队的成员自己告诉我所有脚本都应该适用于 PowerShell 5.1,但情况并非总是如此。对于运行关键业务流程的任何代码,我始终建议在另一台计算机的同一操作系统上安装 5.1,并以这种方式运行所有测试,以确保万无一失。

步骤#2:部署

一旦您满足了所有先决条件并准备好开始推出 PowerShell 5.1,下一步就是执行它!推出 5.1 与其他软件部署类似。如何进行此操作取决于您已有的工具。例如,如果您有 Active Directory 并且想要使用 GPO,您可以走这条路,SCCM 也会以同样的方式工作。我在这里尝试与产品无关,所以这当然是我如何使用 PowerShell 做到这一点!

首先,我将在本地计算机上创建一个名为 C:\PowerShellDeployment 的文件夹。接下来,我会将所需的适当版本下载到该文件夹中,并获取 psexec 的副本。接下来,由于 PowerShell 无法自我升级,我们需要依靠优秀的 VBScript。这是我很久以前创建的一个小脚本,可以帮助您入门。我将其保存到 C:\PowerShellDeployment\installPs.vbs

Set oShell = WScript.CreateObject("WScript.Shell")
Set oFso = CreateObject("Scripting.FileSystemObject")
strWorkingDir = oFso.GetParentFolderName(wscript.ScriptFullName)

'Change this to whatever file name it is
psInstallerPath = strWorkingDir & "\Windows6.1-KB2819745-x86-MultiPkg.msu"
Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set swbemServices = swbemLocator.ConnectServer(".", "Root\CIMV2")
if oFSO.GetFileVersion("c:\windows\system32\windowsPowerShell\v1.0\PowerShell.exe") = "6.0.6002.18111"
then
	Set colArchs = swbemServices.ExecQuery("SELECT SystemType FROM Win32_ComputerSystem",,48)
	For Each objArch in colArchs
    	if
        	InStr(objArch.SystemType,"x64-based PC") > 0
        Then
        	oShell.Run "wusa.exe " & psInstallerPath & " /quiet /norestart",0,True
		Else
        	Wscript.Quit(10)
		End If
	Next
End if

将 VBS 保存在本地计算机上后,创建一个像这样的 PowerShell 脚本并将其保存在某个地方。

## Again, doesn't have to be AD$computers = Get-AdComputer -Filter *foreach ($Computer in $Computers) {    if (Test-Connection -Computername $Computer -Quiet -Count 1) {        $folderPath = 'C:\PowerShellDeployment'        Copy-Item -Path $folderPath -Destination "$Computer\c$"        psexec $Computer cscript "$folderPath\installPs.vbs"        Remove-Item "$Computer\c$\PowerShellDeployment" -Recurse -Force        Restart-Computer -Computername $Computer -Force}

执行上述脚本,它应该检查计算机是否在线,将PowerShell安装程序和VBS复制到远程计算机,执行它,自行清理并重新启动计算机。与往常一样,将其安排在维护窗口中!安装 PowerShell 需要重新启动,而这将毫无悔意地完成它。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯