无法识别PowerShell Add-WindowsFeature

问题描述 投票:18回答:3

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

我基本上已经有了第三方代理软件,该软件可让我以本地系统的身份执行PowerShell。这使我无需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窗口,它按预期返回命令列表。

powershell powershell-3.0 windows-server-2012
3个回答
18
投票

这可能是因为PowerShell脚本是从PowerShell的32位实例启动的。 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
投票

问题最终是这些服务器上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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.