PowerShell Add-WindowsFeature无法识别

时间:2013-12-19 16:05:29

标签: powershell powershell-v3.0 windows-server-2012

首先感谢您对此进行审核。

我基本上有一个第三方代理软件,它允许我将PowerShell作为LocalSystem执行。这使我可以在没有WinRM等的情况下轻松运行远程PowerShell命令。

我遇到的问题是,在某些服务器上,我无法执行get-WindowsFeature或Add-WindowsFeature。

我正在努力实现这一目标的一个例子是:

Import-Module ServerManager;
Get-WindowsFeature;

输出如下:

The term 'Get-WindowsFeature' 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.

如果我在PowerShell窗口中键入相同的命令,或直接调用PowerShell.exe,则返回。我试图弄清楚我们在应用程序中做得不对,但我是PowerShell中最熟悉的人。

加载这些cmdlet需要做些什么特别的事情吗?奇怪的是,Get-Module没有显示任何内容。

谢谢!


回应JBSmith:

Yessir - 看起来像2.0。以下是您提到的命令的结果

>Name                           Value                                            
>----                           -----                                            
>CLRVersion                     2.0.50727.6407                                   
>BuildVersion                   6.1.7600.16385                                   
>PSVersion                      2.0                                              
>WSManStackVersion              2.0                                              
>PSCompatibleVersions           {1.0, 2.0}                                       
>SerializationVersion           1.1.0.1                                          
>PSRemotingProtocolVersion      2.1                                              
>
>Name : AppLocker
>Name : Appx
>Name : BestPractices
>Name : BitsTransfer
>Name : BranchCache
>Name : CimCmdlets
>Name : DirectAccessClientComponents
>Name : Dism
>Name : DnsClient
>Name : International
>Name : iSCSI
>Name : IscsiTarget
>Name : ISE
>Name : Kds
>Name : Microsoft.PowerShell.Diagnostics
>Name : Microsoft.PowerShell.Host
>Name : Microsoft.PowerShell.Management
>Name : Microsoft.PowerShell.Security
>Name : Microsoft.PowerShell.Utility
>Name : Microsoft.WSMan.Management
>Name : MMAgent
>Name : MsDtc
>Name : NetAdapter
>Name : NetConnection
>Name : NetLbfo
>Name : NetQos
>Name : NetSecurity
>Name : NetSwitchTeam
>Name : NetTCPIP
>Name : NetworkConnectivityStatus
>Name : NetworkTransition
>Name : MSFT_NfsMappedIdentity
>Name : NFS
>Name : PKI
>Name : PrintManagement
>Name : PSDiagnostics
>Name : PSScheduledJob
>Name : PSWorkflow
>Name : PSWorkflowUtility
>Name : RemoteDesktop
>Name : ScheduledTasks
>Name : SecureBoot
>Name : ServerCore
>Name : ServerManager
>Name : ServerManagerTasks
>Name : SmbShare
>Name : SmbWitness
>Name : Storage
>Name : TroubleshootingPack
>Name : TrustedPlatformModule
>Name : UserAccessLogging
>Name : VpnClient
>Name : Wdac
>Name : Whea
>Name : WindowsDeveloperLicense
>Name : WindowsErrorReporting
>Name : AWSPowerShell

我也注意到了GCM | ? {$ _。ModuleName -eq'ServerManager'}在我运行时没有返回任何内容,但通过正常的PS窗口,它会按预期返回命令列表。

3 个答案:

答案 0 :(得分:11)

这可能是因为PowerShell脚本是从32位PowerShell实例启动的。 ServerManager命令仅适用于64位版本的PowerShell。请参阅:Can't access ServerManager module via PowerShell

- 编辑 - 添加到jbsmith的评论---

要尝试的额外事项:

运行Get-Command cmdlt:

gcm | ? { $_.ModuleName -eq 'ServerManager' }

它将不返回任何内容,因为尚未加载ServerManager模块。

尝试运行此代码。它将列出要加载的所有可用模块:

Get-Module -ListAvailable | ? { $_.Name -eq 'ServerManager' }

要尝试的另一件事是使用“强制”选项(重新导入模块及其成员,即使模块或其成员具有只读访问模式):

Import-Module ServerManager -Force;
Get-WindowsFeature;

答案 1 :(得分:1)

问题最终导致ServerManager的元数据在这些服务器上为3.0,但用于调用PowerShell命令的开发exe只是版本2.0。当它尝试导入模块时,返回了有关元数据的模式错误,但EXE没有将它们重定向到stdout,因此没有响应。

Import-Module : The 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ServerM
anager\ServerManager.psd1' module cannot be imported because its manifest conta
ins one or more members that are not valid. The valid manifest members are ('Mo
duleToProcess', 'NestedModules', 'GUID', 'Author', 'CompanyName', 'Copyright',
'ModuleVersion', 'Description', 'PowerShellVersion', 'PowerShellHostName', 'Pow
erShellHostVersion', 'CLRVersion', 'DotNetFrameworkVersion', 'ProcessorArchitec
ture', 'RequiredModules', 'TypesToProcess', 'FormatsToProcess', 'ScriptsToProce
ss', 'PrivateData', 'RequiredAssemblies', 'ModuleList', 'FileList', 'FunctionsT
oExport', 'VariablesToExport', 'AliasesToExport', 'CmdletsToExport'). Remove th
e members that are not valid ('HelpInfoUri', 'RootModule'), then try to import
the module again.
At line:1 char:14
+ Import-Module <<<<  ServerManager; Get-Module
    + CategoryInfo          : InvalidData: (C:\Windows\syst...verManager.psd1:
   String) [Import-Module], InvalidOperationException
    + FullyQualifiedErrorId : Modules_InvalidManifestMember,Microsoft.PowerShe
   ll.Commands.ImportModuleCommand

答案 2 :(得分:1)

在Windows Server 2016上,在作为解决方案安装ADFS时,我将C:\ Windows \ system32 \ WindowsPowerShell \ v1.0 \ Modules文件夹复制到C:\ Users \ vagrant \ Documents \ WindowsPowerShell \ Modules并成功! / p>