无法找到Microsoft.Powershell.LocalAccounts模块(或运行Get-LocalUser)

6
运行脚本时,我有一行代码来验证我们应用程序的“服务账户”(也称为本地用户账户)是否存在:
$svcAccountName = "TheAccountName"
$svcAccount = Get-LocalUser -Name $svcAccountName

服务器(Windows Server 2008 R2)拒绝使用Get-LocalUser命令,显示以下信息:
Get-LocalUser : The term 'Get-LocalUser' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-LocalUser 
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
因此,我尝试导入LocalAccounts模块:
Import-Module Microsoft.Powershell.LocalAccounts

我得到了以下信息:

Import-Module:指定的模块“LocalAccounts”未加载,因为在任何模块目录中都找不到有效的模块文件。
在第1行第1个字符处:
+ Import-Module
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (LocalAccounts:String)[Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

根据变量$PSVersionTable,服务器正在运行PSVersion 4.0。

为什么没有加载LocalAccounts模块和Get-LocalUser命令? 如何解决这个问题?


1
目前在这里查看一个Win2012 R2服务器,它也具有PSVersion 4.0,但该模块也不存在。 - Booga Roo
1
相关 - Ansgar Wiechers
@BenH 请把你的评论发布为答案。在我看来,它最好地回答了这个问题。 - Scott Baker
5个回答

5
自 Windows 10 开始,包括 Server 2016 和 2019 在内的操作系统中,Windows Management Framework (其中包含本地账户管理 PowerShell 模块)现在已经是“in-box”(内置)。请访问 https://learn.microsoft.com/en-us/powershell/wmf/overview#wmf-availability-across-windows-operating-systems 以获取有关其他 Windows 版本的 WMF 信息。
这意味着像 Get-LocalUserNew-LocalUser 这样的功能现在可以直接使用,而无需安装或导入任何模块。
请注意,从 WMF 5.1 开始,许多函数的参数名称已更改。此外,现在必须以安全字符串的形式提供密码,可以使用 ConvertTo-SecureString 函数生成安全字符串。
以下示例检查用户是否存在,并在不存在时创建该用户:
$UserName = "AdaLovelace"
$SecurePassword = ConvertTo-SecureString "ThisIsAPassword!" –asplaintext –force
if (Get-LocalUser($UserName))
{
    Echo "User exists"
}
else
{
    New-LocalUser $UserName -Password $SecurePassword -UserMayNotChangePassword -AccountNeverExpires
}

6
对于任何晚来到这个帖子的人来说,重要的是要注意,仅在64位powershell会话中才能加载收件箱版本。 - Tim
@Tim 太有帮助了!在阅读你的注释之前,我无法弄清楚为什么包含对Get-LocalUser cmdlet 的调用的脚本可以在一个帐户中成功执行,但在另一个帐户中却不能。'其他'帐户中的用户正在使用32位PS的快捷方式。 - BentChainRing
@Tim,这是最好的回复之一。Powershell的32位(x86)和64位版本也让我困扰。问题出在32位上,但在64位上(运行在Windows Server 2019上)没有出现该问题。 - M.N.

2

1
这个命令在Server 2016和Win10 1607+中可用。在早期的操作系统上,您需要使用net.exeWMIADSI或使用这些方法之一的模块或安装WMF 5.1。
编辑:微软高级外部专家Sean Kearney编写了一个名为localaccount的模块。该模块是基于GitHub repostiory的代码构建的,通过ADSI复制了新模块的功能。该模块适用于旧版本的PowerShell。请注意,这与内置模块不同。

1
第一段不正确。我正在使用Windows Server 2012 R2和PS 5.1 - Get-LocalUser正常工作。 - Jelphy
@Jelphy:由第三方编写的模块与官方模块不同(即使它模仿了原始模块的功能)。 - bluuf
@bluuf,我认为Jelphy并不是在提到Sean Kearney的模块。安装WMF 5.1会在低版本的操作系统上安装官方的Microsoft.PowerShell.LocalAccounts模块。我在2012 R2和Win7上进行了测试。 - BenH

0

0

你需要先从另一台电脑上获取.PSM1文件

#Install LocalAccount Module
Install-Module -Name localaccount

#Save Module to the PowerShell Modules folder
Save-Module -Name localaccount -Path "C:\Program Files (x86)\WindowsPowerShell\Modules"

如果您将其添加到Windows 2008 R2上的此位置,则应安装该模块,但如果未安装,请查看此链接: https://msdn.microsoft.com/en-us/library/dd878350(v=vs.85).aspx

我不确定为什么它不在Windows 2008 R2上,但LocalAccounts模块于2015年3月21日推出。这早于Windows 2016和Windows 10。

Install-Module -Name localaccount -RequiredVersion 1.1

你可以随时联系创建者 "Sean P. Kearney"。

希望这能帮到你。这是我做的方法。


谢谢 - 但为什么它失踪了? - Scott Baker
添加了更多信息,但它可以在Windows 8.1和Server 2012上运行。 - Zach Olinske

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接