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

时间:2017-05-15 22:33:26

标签: powershell module windows-server-2008-r2

运行脚本时,我有一行来验证我们的应用程序的“服务帐户”(也称为本地用户帐户)是否存在:

$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命令? 我该如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

此cmdlet随Server 2016和Win10 1607+一起提供。在早期的操作系统中,您需要使用net.exeWMIADSI或使用其中一种方法的模块或安装WMF 5.1。

编辑:MS PFE Sean Kearney编写了一个名为localaccount的模块。这是根据GitHub repostiory的代码构建的,它通过ADSI复制新模块的功能。适用于旧版本的PowerShell。请注意,这与内置模块不同。

答案 1 :(得分:1)

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

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

祝你好运! :)

答案 2 :(得分:1)

从Windows 10(包括Server 2016和2019)开始,Windows管理框架(包括本地帐户管理Powershell模块)现已“随盒发货”。有关其他版本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
}

答案 3 :(得分: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"

希望这会对你有所帮助。这就是我做到的。