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

[玩转系统] 更新 PowerShell Core Windows 兼容性

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

更新 PowerShell Core Windows 兼容性


我想分享一下我在 PowerShell Core 世界中生活的经历的快速更新。 Microsoft 正在努力让这一切变得更容易,其中之一就是提供一种访问可在 PowerShell Core 中运行的 Windows PowerShell 模块的方法。它通过 WindowsCompatibility 模块中的命令来完成此操作。然而,我遇到了一个烦恼,你可能也会看到我认为我应该分享的,以及我的工作。

预期行为

PowerShell Core 的行为方式与 Windows PowerShell 中的行为方式非常相似。在 Windows 系统中,您可以映射新的 PSDrive,如果您使用字母,PowerShell 会自动定义一个简单的函数,可以轻松更改为该路径。

[玩转系统] 更新 PowerShell Core Windows 兼容性

我可以创建一个新的 PSDrive 并对其进行更改,不会出现错误。现在我将导入 WindowsCompatibility 模块。

问题行为

该模块的工作方式之一是通过远程兼容性会话提供对 Windows PowerShell 命令的访问。尽管您可以使用Initialize-WinSession 有意创建此类会话,但何时创建此类会话可能并不总是很清楚。

[玩转系统] 更新 PowerShell Core Windows 兼容性

我确信命令将透明地使用此会话。但有一点问题。看看我现在尝试更改为 S: 驱动器时会发生什么。

[玩转系统] 更新 PowerShell Core Windows 兼容性

PowerShell 会抱怨,但实际上会更改位置。深入研究该错误,我发现 Set-Location 命令是通过 WinCompatibility 会话运行的,该会话对 S: 驱动器一无所知。

[玩转系统] 更新 PowerShell Core Windows 兼容性

我发现,如果我在会话中创建驱动器,那么恼人的错误就可以解决。

脚本化解决方案

考虑到这一点,我想出了一个脚本化的解决方案。我的 PowerShell 配置文件的一部分是创建许多 PSDrive 快捷方式。一旦定义了这些,我就继续导入 WindowsCompatibilityModule

Import-Module WindowsCompatibility
Add-WindowsPSModulePath
Initialize-WinSession

我知道我需要 WinCompat 会话。然后我调用这个函数。

#requires -version 6.1
#requires -module WindowsCompatibility

Function Update-WinCompatibiltySession {
    [cmdletbinding(SupportsShouldProcess)]
    [alias("uwc")]

    Param([switch]$Passthru)

    Try {
        $s = Get-PSSession -name "wincompat-localhost-$env:username" -ErrorAction Stop
        $local = Get-PSDrive
        foreach ($psdrive in $local) {
            if ($PSCmdlet.ShouldProcess($pdrive.name, "Adding")) {
                Invoke-Command {
                    Try {
                        Get-PSDrive -Name $using:psdrive.name -ErrorAction Stop | Out-Null
                    }
                    Catch {
                        $newParams = @{
                            name       = $using:psdrive.name
                            PSProvider = $using:psdrive.provider
                            Root       = $using:psdrive.root
                        }
                        #mapping the drive using the credential. This may not be necessary.
                        if ($using:psdrive.credential.username) {
                            $newParams.Add("Credential", $using:psdrive.credential)
                        }
                        $new = New-PSDrive @newParams
                        if ($using:Passthru) {
                            $new
                        }
                    }
                } -session $s
            }
        } #Whatif
    }
    Catch {
        Write-Warning "Did not detect a WinCompat remoting session. You might need to run Initialize-WinSession."
    }
}

该函数获取本地会话中的所有 PSDrive,并将其添加到远程会话中(如果尚不存在)。是的,我意识到有几种方法可以实现这一目标。但现在,我的错误已经消失了。

[玩转系统] 更新 PowerShell Core Windows 兼容性

如果稍后添加另一个驱动器,我需要重新运行该功能。

[玩转系统] 更新 PowerShell Core Windows 兼容性

我在 GitHub 上有一个关于此问题的未决问题,但目前,这种安排似乎可以解决该问题。

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

取消回复欢迎 发表评论:

关灯