设置StrictMode bug powershell 7或正常行为



希望你能向我解释一下。我有一个扫描AD以查找计算机对象的脚本。我刚开始使用powershell 7。

脚本的一部分如下:


$ErrorActionPreference = "Stop"
Set-StrictMode -Version "Latest"
$adComputerCollection  = Get-ADComputer -Filter { OperatingSystem -like "*Windows Server*" }

使用powershell 7,它会给我一个错误,即以下错误:

Get-ADComputer: The 'Get-ADComputer' command was found in the module 'ActiveDirectory', but the module could not be loaded. For more information, run 'Import-Module ActiveDirectory'.

好的,所以没问题,我只是添加了导入模块,我的脚本如下:

$ErrorActionPreference = "Stop"
Set-StrictMode -Version "Latest"
Import-Module "ActiveDirectory"
$adComputerCollection  = Get-ADComputer -Filter { OperatingSystem -like "*Windows Server*" }

然后我在导入模块activeDirectory时得到以下错误:

Get-PSSession: The remote session with the name WinPSCompatSession  is not available.

这是正常行为吗。有了powershell 5.1,我就没有这个问题了。我可以在收到广告集后通过设置严格的模式来解决这个问题。有人能向我解释一下这种行为吗?

我的ps版本表是:

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Active Directory模块在名为WinPSCompatSession的本地主机的"远程"PSSession中加载。该模块实际运行的不是PS7,而是5.1。

这可能就是您看到错误的原因。

最新更新