Powershell 5.0/Windows 10 Get-Command 空引用异常

Powershell 5.0/Windows 10 Get-Command Null Reference Exception

我全新安装了 Win10 和 Powershell 5.0。我有 运行 update-help 并且大部分工作正常;但是,Get-Command returns 一个空引用异常,无论我处于常规模式还是提升模式。我还尝试使用 -noprofile 从 运行 使用 运行ning Powershell,但在用户模式和提升模式下我仍然遇到相同的错误。以下是一些附加信息:

PS Z:\> Get-WmiObject Win32_OperatingSystem | Select-Object -Property Version, ServicePackMajorVersion, ServicePackMinorVersion

Version    ServicePackMajorVersion ServicePackMinorVersion
-------    ----------------------- -----------------------
10.0.10586                       0                       0

PS Z:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.0.10586.122
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.122
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PS Z:\> get-command
>> TerminatingError(Get-Command): "Object reference not set to an instance of an object."
get-command : Object reference not set to an instance of an object.
At line:1 char:1
+ get-command
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand
get-command : Object reference not set to an instance of an object.
At line:1 char:1
+ get-command
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand

PS Z:\> $error[0] | format-list * -force


PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at Microsoft.PowerShell.Commands.GetCommandCommand.IsCommandMatch(CommandInfo& current, Boolean& isDuplicate)
                           at Microsoft.PowerShell.Commands.GetCommandCommand.AccumulateMatchingCommands(IEnumerable`1 commandNames)
                           at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject          :
CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}



PS Z:\> $error[0].line
PS Z:\> $error[0].exception | format-list * -force


Message        : Object reference not set to an instance of an object.
Data           : {}
InnerException :
TargetSite     : Boolean IsCommandMatch(System.Management.Automation.CommandInfo ByRef, Boolean ByRef)
StackTrace     :    at Microsoft.PowerShell.Commands.GetCommandCommand.IsCommandMatch(CommandInfo& current, Boolean& isDuplicate)
                    at Microsoft.PowerShell.Commands.GetCommandCommand.AccumulateMatchingCommands(IEnumerable`1 commandNames)
                    at System.Management.Automation.CommandProcessor.ProcessRecord()
HelpLink       :
Source         : System.Management.Automation
HResult        : -2147467261

在用户模式和提升模式下 运行ning update-help 之后,我在 Powershell 中做的下一件事是安装 SQL Power Doc 所以我不确定这是否导致了问题,因为我不知道记得在安装前使用 Get-Command。

在安装过程中,我发出了这些命令:

Set-ExecutionPolicy RemoteSigned -Force

New-Item -type directory -path "$([Environment]::GetFolderPath([Environment+SpecialFolder]::MyDocuments))\WindowsPowerShell\Modules"

Set-Location "$([Environment]::GetFolderPath([Environment+SpecialFolder]::MyDocuments))\WindowsPowerShell"

当我 运行 在 Get-Command 上提供帮助时 - 它可以正常工作:

PS Z:\> help get-command

NAME
    Get-Command

SYNOPSIS
    Gets all commands.


SYNTAX
    Get-Command [[-ArgumentList] [<Object[]>]] [-All] [-FullyQualifiedModule [<ModuleSpecification[]>]] [-ListImported] [-Module [<String[]>]] [-Noun [<String[]>]] [-ParameterName [<String[]>]] [-ParameterType [<PSTypeName[]>]] [-ShowCommandInfo] [-Syntax] [-TotalCount
    [<Int32>]] [-Verb [<String[]>]] [<CommonParameters>]

    Get-Command [[-Name] [<String[]>]] [[-ArgumentList] [<Object[]>]] [-All] [-CommandType {Alias | Function | Filter | Cmdlet | ExternalScript | Application | Script | Workflow | Configuration | All}] [-FullyQualifiedModule [<ModuleSpecification[]>]] [-ListImported]
    [-Module [<String[]>]] [-ParameterName [<String[]>]] [-ParameterType [<PSTypeName[]>]] [-ShowCommandInfo] [-Syntax] [-TotalCount [<Int32>]] [<CommonParameters>]


DESCRIPTION
    The Get-Command cmdlet gets all commands that are installed on the computer, including cmdlets, aliases, functions, workflows, filters, scripts, and applications. Get-Command gets the commands from Windows PowerShell modules and snap-ins and commands that were
    imported from other sessions. To get only commands that have been imported into the current session, use the ListImported parameter.

    Without parameters, a Get-Command command gets all of the cmdlets, functions, workflows and aliases installed on the computer. A Get-Command * command gets all types of commands, including all of the non-Windows-PowerShell files in the Path environment variable
    ($env:path), which it lists in the "Application" command type.

    A Get-Command command that uses the exact name of the command (without wildcard characters) automatically imports the module that contains the command so you can use the command immediately. To enable, disable, and configure automatic importing of modules, use the
    $PSModuleAutoLoadingPreference preference variable. For more information, see about_Preference_Variables (http://go.microsoft.com/fwlink/?LinkID=113248).

    Get-Command gets its data directly from the command code, unlike Get-Help, which gets its information from help topics.

    In Windows PowerShell 2.0, Get-Command gets only commands in current session. It does not get commands from modules that are installed, but not imported. To limit Get-Command in Windows PowerShell 3.0 and later to commands in the current session, use the
    ListImported parameter.

    Starting in Windows PowerShell 5.0, results of the Get-Command cmdlet display a Version column by default; a new Version property has been added to the CommandInfo class.


RELATED LINKS
    Online Version: http://go.microsoft.com/fwlink/p/?linkid=289583
    Export-PSSession
    Get-Help
    Get-Member
    Get-PSDrive
    Import-PSSession
    about_Command_Precedence

REMARKS
    To see the examples, type: "get-help Get-Command -examples".
    For more information, type: "get-help Get-Command -detailed".
    For technical information, type: "get-help Get-Command -full".
    For online help, type: "get-help Get-Command -online"

有人看到明显的东西吗?感谢任何帮助。

暂时重命名您的 Modules 目录可能是值得的,然后重新 运行 Get-Command 看看是否修复了它。

(根据问题上面的评论,这似乎是解决方案。)

我刚遇到这个问题,因为我遇到了同样的问题。它认为它与 SQL Powerdoc 解决方案附带的模块 RDS-Manager 有关。 RDS.Manager 来自 TechNet 图库: http://gallery.technet.microsoft.com/ScriptCenter/e8c3af96-db10-45b0-88e3-328f087a8700/

我已经从我的模块文件夹中删除了整个文件夹,一切都恢复正常了。添加了 RDS 模块,NullReference Exception 又出现了。