运行脚本时,我有一行来验证我们的应用程序的“服务帐户”(也称为本地用户帐户)是否存在:
$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
命令?我怎样才能解决这个问题?
3 回答
此cmdlet随Server 2016和Win10 1607一起提供 . 在早期的操作系统上,您需要使用
net.exe
,WMI
,ADSI
或使用其中一种方法的模块或安装WMF 5.1 .编辑:MS PFE Sean Kearney编写了一个名为localaccount的模块 . 这是根据GitHub repostiory的代码构建的,它通过
ADSI
复制新模块的功能 . 适用于旧版本的PowerShell . 请注意,这与内置模块不同 .您需要先从另一台计算机上获取.PSM1文件
如果将其添加到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 .
你可以随时联系创建它的人“Sean P. Kearney”
希望这对你有所帮助 . 这就是我做到的 .
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
祝好运! :)