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

[玩转系统] PowerShell 7 远程清理

作者:精品下载站 日期:2024-12-14 07:57:41 浏览:10 分类:玩电脑

PowerShell 7 远程清理


现在 PowerShell 7 已经发布并希望安装在您的 Windows 10 桌面上,您就可以开始了,对吧?我想说你们可能都准备好了。但是,如果像我一样,您正在运行 PowerShell 7 beta 或还安装了 PowerShell Core,则可能需要进行一些整理工作。我认为我要讨论的内容不会产生任何严重影响。但我喜欢保持一个整洁的系统,我敢打赌你们中的许多人也是如此。

适合所有人的 PSSession 配置

在我的 Windows 10 桌面上,我一直在运行 PowerShell Core、PowerShell 6.x 版本以及 PowerShell 7 beta 和候选版本。我对每个都启用了 WSMan 远程处理。随着时间的推移,这给我留下了多个 PSSessionConfiguration 设置。

[玩转系统] PowerShell 7 远程清理

在 PowerShell 7 中,您看不到 PowerShell 5.1 会话。我怀疑将这些简单地留在这里会有任何问题,但对我来说感觉很混乱。此时,我已经卸载了 PowerShell Core 6.x,因此我绝对不需要相关会话。深入挖掘一下,我可以发现这些会议所指向的内容。

[玩转系统] PowerShell 7 远程清理

我的第一个想法是其中一些文件已被删除。我想我可以使用 PowerShell 来确定哪些会话仍然具有有效的插件文件。

[玩转系统] PowerShell 7 远程清理

这让我很惊讶。但这很容易被验证。

[玩转系统] PowerShell 7 远程清理

尽管 PowerShell v6.x 版本早已被卸载,但这些位置仍然存在。沿着旧版 PowerShell 7 位。

版本清理

当您安装 PowerShell 7 最终版本并启用远程处理时,您将获得标记为 7 和 7.0.0 的文件夹。每个文件夹中的插件文件都是相同的。据我目前所知,这是设计使然。有一个通用 PowerShell 7 文件夹,然后是一个特定于版本的文件夹 7.0.0。据推测,在将来的某个时候,我会安装类似 PowerShell 7.1.0 的东西,它将覆盖 7.0.0 文件夹。我正在尝试了解更多相关信息,但目前这并不影响我的清理任务。

我的清理工作将包括删除过时的文件和文件夹以及取消注册过时的 PSSessionConfigurations。

#requires -version 7.0

[cmdletbinding(SupportsShouldProcess)]
Param()

#clean up folders
Get-Childitem -path $env:windir\system32\powershell -directory |
Where-object {$_.name -notmatch "^7(\.0\.0)?$"} | Remove-Item -Force -Recurse

#clean up sessions
Get-PSSessionConfiguration | Where-object {$_.name -notmatch "^Powershell\.7(\.0\.0)?$"} | 
Unregister-PSSessionConfiguration -Force -NoServiceRestart

Write-Warning "You will need to restart the WinRM service for these changes to take effect"

该脚本文件使用正则表达式模式来保留当前的 PowerShell 7.0.0 文件和会话。我还添加了对 -WhatIf 的支持。

[玩转系统] PowerShell 7 远程清理

我想我已经准备好了。但显然不是。

[玩转系统] PowerShell 7 远程清理

我怀疑我应该在删除文件之前删除会话配置。所以我修改了我的剧本。

[cmdletbinding(SupportsShouldProcess)]
Param()

#clean up sessions
$sessions = Get-PSSessionConfiguration | Where-Object {$_.name -notmatch "^Powershell\.7(\.0\.0)?$"}
foreach ($session in $sessions) { 
    Try {
        Unregister-PSSessionConfiguration -Name $session.name -Force -NoServiceRestart -ErrorAction Stop
    }
    Catch {
        #Fall back to this if the cmdlet fails
        winrm delete http://schemas.microsoft.com/wbem/wsman/1/config/plugin?Name=$($session.name)
    }
}

#clean up folders
Get-Childitem -path $env:windir\system32\powershell -directory |
Where-object {$_.name -notmatch "^7(\.0\.0)?$"} | Remove-Item -Force -Recurse

在此版本中,如果 Unregister-PSSessionConfiguration 失败,我将退回到 winrm 命令行工具。但是,当我再次尝试时,仍然会出现错误。事实上,我似乎完全把事情搞砸了。但没关系。这样我才能学到更多。你们中的任何一个人都可能遇到同样垃圾的环境。

深层发掘

我应该做的是在删除文件之前备份它们。或者创建一个系统还原点。你应该记住这一点。就我而言,我需要采取替代路线,因为即使重新启动 WinRM 服务后,我的 WSMan 配置仍在寻找插件配置。这些都存储在注册表中。

[玩转系统] PowerShell 7 远程清理

我假设如果删除过时的注册表项,就可以克服 Get-PSSessionConfiguration 错误。

$regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Plugin"
#get the PowerShell plugins
$ps = Get-Childitem -path $regPath | Where {$_.PSChildName -match "^PowerShell"}
#filter the ones to remove
$remove = $ps | where-object {$_.pschildname -notmatch "^Powershell\.7(\.0\.0)?$"}
$remove | Remove-Item -Recurse -Force #-WhatIf

我使用 -WhatIf 进行了测试,以验证我是否删除了正确的条目。删除这些条目后,我终于有了我想要的 PSSessionConfigurations。

[玩转系统] PowerShell 7 远程清理

概括

您不必自动执行此类清理。但如果您这样做,似乎您应该取消注册 PSSessionConfiguration,然后删除任何剩余的文件或注册表项。就我个人而言,我期待有一天使用 SSH 来满足我所有的 PowerShell 远程处理需求。但就目前而言,WSMan 仍然发挥着作用,可能需要时不时地关注一下。

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

取消回复欢迎 发表评论:

关灯