PowerShell提示输入命令不是有效命令

来任务了,由于要Windows服务器版本要升级:2008 -> 2016,所以要做很多的准备工作,比如OU, GPO。由于OU划分很细,有几百个OU,要在2016OU下面新建所有在2008OU下已经存在的OU。那就用PowerShell来搞搞吧!但是找了一台2008的server,运行一下Get-ADOrganizationalUnit这个PowerShell的命令,我去,竟然提示我不是有效的命令,错误详细信息见下:
PowerShell提示输入命令不是有效命令

奇怪了,之前在另外一台server上面还是可以用的,刚开始怀疑是不是PowerShell版本太低了,索性查一下:

PS C:\Users\admin> $PSVersionTable

Name                           Value                                                                                                                                                         
----                           -----                                                                                                                                                         
PSVersion                      5.1.14393.2515                                                                                                                                                
PSEdition                      Desktop                                                                                                                                                       
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                       
BuildVersion                   10.0.14393.2515                                                                                                                                               
CLRVersion                     4.0.30319.42000                                                                                                                                               
WSManStackVersion              3.0                                                                                                                                                           
PSRemotingProtocolVersion      2.3                                                                                                                                                           
SerializationVersion           1.1.0.1   

版本都5.1了,不是很老啊!OK,那再来看看有没有AD模块吧,毕竟这个命令是对AD的操作。运行: get-module -listavailable 回车,走你,结果一看。原来如此,目测没有AD模块。

PowerShell提示输入命令不是有效命令

那就导入一下吧,运行:

PS C:\Windows\system32> Add-WindowsFeature RSAT-AD-PowerShell
然后再运行一把get-module,看一下,ActiveDirectory加载过来了
PS C:\Windows\system32> get-module -listavailable

PowerShell提示输入命令不是有效命令

最后运行一下命令:PS C:\Windows\system32> Get-ADOrganizationalUnit,不再报错,而且提示需要参数,着就对了。
PowerShell提示输入命令不是有效命令

我在另外一个server上面也试了另外一种方法:
首先运行:get-module -listavailable 查看ActiveDirectory模块是否已经加载。
然后就是直接运行:import-module activedirectory
如果没有报错,就再运行:get-module -listavailable
这个时候ActiveDirectory模块就被导入了,关于AD相关的PowerShell命令你就可以开始用了。

The term 'Get-ADOrganizationalUnit' 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:5 char:38
+ $SVSOUList = Get-ADOrganizationalUnit <<<<  -SearchBase $sourceOU -Filter {Name -like "W2K8_*"} |select -ExpandProperty Name
    + CategoryInfo          : ObjectNotFound: (Get-ADOrganizationalUnit:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 ________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
PS C:\Users\admin> import-module activedirectory

________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
PS C:\Users\admin> get-module -listavailable

ModuleType Name                      ExportedCommands                                                           
---------- ----                      ----------------                                                           
Manifest   ActiveDirectory           {}                                                                         
Manifest   ADRMS                     {}                                                                         
Manifest   AppLocker                 {}                                                                         
Manifest   BestPractices             {}                                                                         
Manifest   BitsTransfer              {}                                                                         
Manifest   GroupPolicy               {}                                                                         
Manifest   PSDiagnostics             {}                                                                         
Manifest   ServerManager             {}                                                                         
Manifest   TroubleshootingPack       {}                                                                         
Manifest   Citrix.XenDesktop.Admin   {}                                                                         
Manifest   Citrix.Common.Commands    {}                                                                         
Binary     TelemetryModule           {}                                                                         

猜你喜欢

转载自blog.51cto.com/jiaszwx/2317941