PowerShell提示輸入命令不是有效命令
阿新 • • 發佈:2018-11-16
來任務了,由於要Windows伺服器版本要升級:2008 -> 2016,所以要做很多的準備工作,比如OU, GPO。由於OU劃分很細,有幾百個OU,要在2016OU下面新建所有在2008OU下已經存在的OU。那就用PowerShell來搞搞吧!但是找了一臺2008的server,執行一下Get-ADOrganizationalUnit這個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模組。
那就匯入一下吧,執行:
PS C:\Windows\system32> Add-WindowsFeature RSAT-AD-PowerShell
然後再執行一把get-module,看一下,ActiveDirectory載入過來了
PS C:\Windows\system32> get-module -listavailable
最後執行一下命令:PS C:\Windows\system32> Get-ADOrganizationalUnit,不再報錯,而且提示需要引數,著就對了。
我在另外一個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 {}