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

[玩转系统] 删除过时的 PowerShell 远程配置

作者:精品下载站 日期:2024-12-14 08:04:38 浏览:16 分类:玩电脑

删除过时的 PowerShell 远程配置


我很确定我之前已经讨论过这个问题,但微软计划很快发布 PowerShell 7.2,我认为重新讨论这个主题可能会更好。这是潜在的问题。如果您安装 PowerShell 7 版本已有一段时间,并且已启用 PowerShell 远程处理,那么您很可能拥有如下所示的远程会话配置列表。

[玩转系统] 删除过时的 PowerShell 远程配置

如果您在 Windows PowerShell 中运行 Get-PSSessionConfiguration,除了 Windows PowerShell 5.1 会话之外,您还将获得这些配置。现在,拥有所有这些配置可能没有任何问题,但就我个人而言,我觉得有点混乱。我不喜欢保留不被使用的东西。是时候做点家务了。

取消注册-PSSessionConfiguration

使用 Unregister-PSSessionConfiguration 删除会话配置非常简单。指定配置名称,就没有了。该 cmdlet 还支持 -WhatIf 和 -Confirm,我始终建议您使用它们。这相当于木匠的格言“测量两次,切割一次”。但我想要一种简单的方法来转储我不使用的所有内容,除了与当前 PowerShell 版本关联的会话配置之外。在我的系统上是 7.1.5。作为一个额外的变化,我还安装了 PowerShell 7.2 的最新预览版,包括远程处理配置。我也想保留它。我知道随着时间的推移,我将需要再次执行此内务任务,因此我不妨构建一个 PowerShell 函数。

删除-ObsoleteSessionConfiguration

我的函数编写为在 Windows PowerShell 5.1 和 PowerShell 7 中运行。如果我在 Windows PowerShell 中运行,我想排除默认会话配置。我将使用正则表达式模式。

$Exclude = "microsoft.powershell|microsoft.powershell.workflow|microsoft.powershell32|microsoft.windows.servermanagerworkflows|PowerShell.$($PSVersionTable.PSVersion)"

最后一个条目经过计算,应对应于 PowerShell 7 会话配置。当我在 Windows PowerShell 中运行此命令时,这将创建一个可能永远不存在的条目,但这不会影响过滤。

 (Get-PSSessionConfiguration -verbose:$False).where({$_.name -notmatch $exclude})

我使用Where()方法来获得轻微的性能优势。顺便说一句,我有意关闭 Get-PSSessionConfiguration 的详细输出。该命令的输出显示了将要运行的函数,但有用的信息很少。我怀疑在开发过程中使用了详细输出,这是一项很好的技术,但后来详细输出从未被修改过。

然后,每个会话配置都会通过管道传输到 Unregister-PSSessionConfiguration。

 Unregister-PSSessionConfiguration -Verbose:$false -Confirm

同样,出于同样的原因,我将关闭详细输出。我还明确使用 -Confirm 参数。我希望始终收到删除会话配置的提示。

我的功能包括对 -Whatif 的支持。

[玩转系统] 删除过时的 PowerShell 远程配置

当然,当我在没有 -Whatif 的情况下运行该函数时,确认过程将接管。

[玩转系统] 删除过时的 PowerShell 远程配置

我可以选择要删除的配置以及我想要保留的配置,例如 PowerShell 预览。

[玩转系统] 删除过时的 PowerShell 远程配置

尝试一下

这是我正在使用的代码。我还鼓励您阅读 about_Functions_CmdletBindingAttribute 帮助主题。

#requires -version 5.1
#requires -RunAsAdministrator

Function Remove-ObsoleteSessionConfiguration {
    [cmdletbinding(SupportsShouldProcess,ConfirmImpact="High")]
    Param()
    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Starting $($myinvocation.mycommand)"
        #exclude these default configurations as a regex pattern
        $Exclude = "microsoft.powershell|microsoft.powershell.workflow|microsoft.powershell32|microsoft.windows.servermanagerworkflows|PowerShell.$($PSVersionTable.PSVersion)"
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Excluding $Exclude"
    } #begin

    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting non-default PowerShell Remoting configurations"
        <#
         turn off Verbose output for Get-PSSessionConfiguration and Unregister-PSSessionConfiguration.
         These command displays PowerShell code which adds no value for my purpose.
        #>
        (Get-PSSessionConfiguration -verbose:$False).where({$_.name -notmatch $exclude}) |
        Unregister-PSSessionConfiguration -Verbose:$false -Confirm
    } #process

    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)"
    } #end

} #close Remove-PSRemotingConfiguration

非常谨慎使用,并在非生产环境中对其进行彻底测试,尤其是在您创建了自己的自定义会话配置的情况下。

如果您有自定义配置,请确保您拥有所有必需的文件,以便在必要时重新创建它们。运行此函数后,您应该重新启动 WinRM 服务。

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

取消回复欢迎 发表评论:

关灯