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

Can't find Microsoft.Powershell.LocalAccounts module (or run Get-LocalUser)

当 运行 一个脚本时,我有一行来验证我们应用程序的 "service account"(又名本地用户帐户)是否存在:

$svcAccountName = "TheAccountName"
$svcAccount = Get-LocalUser -Name $svcAccountName

服务器 (Windows Server 2008 R2) 对 Get-LocalUser cmdlet 犹豫不决,声明:

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 : The specified module 'LocalAccounts' was not loaded because no
valid module file was found in any module directory.
At line:1 char:1
+ Import-Module
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (LocalAccounts:String)[Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

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

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

您需要先从另一台计算机上获取 .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 上将其添加到此位置。它应该安装模块,但如果没有检查这个 link: 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"

希望对您有所帮助。我就是这样做的。

此 cmdlet 可用于 Server 2016 和 Win10 1607+。在较早的 OS 上,您将需要使用 net.exeWMIADSI 或使用其中一种方法的模块或安装 WMF 5.1。

编辑:MS PFE Sean Kearney 编写了一个名为 localaccount. Which is built from the code of this GitHub repostiory 的模块,它通过 ADSI 复制了新模块的功能。适用于旧版本的 PowerShell。请注意,这与内置模块不同。

Microsoft.Powershell.LocalAccounts 模块是 Windows 管理框架 (WMF) v5.1 的一部分,可从以下网址下载:https://www.microsoft.com/en-us/download/details.aspx?id=54616

安装后,您将能够在脚本中使用这些 cmdlet。 在这里,您还可以参考每个版本的 Powershell 中包含哪些模块:https://msdn.microsoft.com/powershell/reference/readme

祝你好运! :)

从 Windows 10(包括 Server 2016 和 2019)开始,Windows 管理框架(包括本地帐户管理 Powershell 模块)现已发布 "in-box"。有关其他版本 Windows 的 WMF 信息,请参阅 https://docs.microsoft.com/en-us/powershell/wmf/overview#wmf-availability-across-windows-operating-systems

这意味着无需安装或导入任何模块即可使用 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
}