PowerShell Add-WindowsFeature无法识别

Bea*_*STi 17 powershell powershell-3.0 windows-server-2012

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

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

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

我试图实现这一目标的一个例子是:

Import-Module ServerManager;
Get-WindowsFeature;
Run Code Online (Sandbox Code Playgroud)

输出如下:

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.
Run Code Online (Sandbox Code Playgroud)

如果我在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
Run Code Online (Sandbox Code Playgroud)

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

HAL*_*256 14

这可能是因为PowerShell脚本是从32位PowerShell实例启动的.ServerManager命令仅适用于64位版本的PowerShell.请参阅:无法通过PowerShell访问ServerManager模块

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

额外的事情要尝试:

运行Get-Command cmdlt时:

gcm | ? { $_.ModuleName -eq 'ServerManager' }
Run Code Online (Sandbox Code Playgroud)

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

尝试运行它.它将列出要加载的所有可用模块:

Get-Module -ListAvailable | ? { $_.Name -eq 'ServerManager' }
Run Code Online (Sandbox Code Playgroud)

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

Import-Module ServerManager -Force;
Get-WindowsFeature;
Run Code Online (Sandbox Code Playgroud)

  • 对于 Windows 10,我必须按照 Server Fault 上的这个答案安装“Windows 10 远程服务器管理工​​具”:https://serverfault.com/a/831836/420795 (4认同)

Bea*_*STi 2

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

Import-Module : The 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Serveranager\ServerManager.psd1' module cannot be imported because its manifest contins one or more members that are not valid. The valid manifest members are ('ModuleToProcess', 'NestedModules', 'GUID', 'Author', 'CompanyName', 'Copyright', 'ModuleVersion', 'Description', 'PowerShellVersion', 'PowerShellHostName', 'PowerShellHostVersion', 'CLRVersion', 'DotNetFrameworkVersion', 'ProcessorArchitecture', 'RequiredModules', 'TypesToProcess', 'FormatsToProcess', 'ScriptsToProcess', 'PrivateData', 'RequiredAssemblies', 'ModuleList', 'FileList', 'FunctionsToExport', 'VariablesToExport', 'AliasesToExport', 'CmdletsToExport'). Remove the members that are not valid ('HelpInfoUri', 'RootModule'), then try to importthe 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.PowerShell.Commands.ImportModuleCommand
Run Code Online (Sandbox Code Playgroud)