首页 > 解决方案 > Set-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 我没有这个问题。我可以在获得广告集合后通过设置严格模式来解决此问题。有人可以向我解释这种行为吗?

我的 psversiontable 是:

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

标签: powershellpowershell-core

解决方案


activedirectory 模块在名为 WinPSCompatSession 的“远程”PSSession 中加载到本地主机。该模块实际上运行的不是 PS 7,而是 5.1。

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


推荐阅读