列出以前加载的所有PowerShell变量

64jmpszr  于 2023-03-02  发布在  Shell
关注(0)|答案(3)|浏览(128)

是否有PowerShell命令可列出所有先前加载的变量?
我正在Visual Studio中运行一些PowerShell脚本,希望列出当前PowerShell会话中可用的所有变量。
我试过这个命令:

ls variable:*;

但它返回以下内容:

System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
wi3ka0sx

wi3ka0sx1#

ls variable:*应该可以工作,或者Get-Variable。如果这些导致了错误的输出,那是由于一个实现很差的主机,而不是powershell本身。如果你打开标准的控制台主机(运行powershell.exe),你会看到这些工作正常。
如果您需要绕过坏主机,那么将所有内容转储到显式字符串可能会更好:

Get-Variable | Out-String

Get-Variable |%{ "Name : {0}`r`nValue: {1}`r`n" -f $_.Name,$_.Value }
dgsult0t

dgsult0t2#

有趣的是,您可以只输入variable,这也可以工作!
我发现这个是因为我很好奇ls variable:*在做什么。Get-Help ls告诉我们它是PowerShell的Get-ChildItem的别名,我知道它会列出一个对象的所有子对象。所以,我只尝试了variable,瞧!
基于thisthisls variable:*所做的似乎是告诉它使用variable列表上的*(all/any)通配符进行某种范围/名称空间查找,在本例中,通配符似乎是无关紧要的(ls variable:* == ls variable: == variable)。

x0fgdtte

x0fgdtte3#

我经常东游西荡,在交互式环境中测试概念,但有时会忘记变量名或我定义的所有内容。
下面是我编写的一个用于 Package Get-Variable的函数,它自动排除shell启动时定义的任何全局变量:

function Get-UserVariable()
{
    [CmdletBinding()]
    param(
        [Parameter(Position = 0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)][String[]]$Name,
        [Parameter()][Switch]$ValueOnly,
        [Parameter()][String[]]$Include,
        [Parameter()][String[]]$Exclude,
        [Parameter()][String]$Scope
    )
    $varNames = $global:PSDefaultVariables + @("PSDefaultVariables")
    $gvParams = @{'Scope' = "1"}
    if ($PSBoundParameters.ContainsKey('Name'))
    {
        $gvParams['Name'] = $Name
    }
    if ($PSBoundParameters.ContainsKey('ValueOnly'))
    {
        $gvParams['ValueOnly'] = $ValueOnly
    }
    if ($PSBoundParameters.ContainsKey('Include'))
    {
        $gvParams['Include'] = $Include
    }
    if ($PSBoundParameters.ContainsKey('Exclude'))
    {
        # This is where the magic happens, folks
        $gvParams['Exclude'] = ($Exclude + $varNames) | Sort | Get-Unique
    }
    else
    {
        $gvParams['Exclude'] = $varNames
    }
    if ($PSBoundParameters.ContainsKey('Scope'))
    {
        $gvParams['Scope'] = $Scope
    }

    gv @gvParams
<#
.SYNOPSIS
Works just like Get-Variable, but automatically excludes the names of default globals.

.DESCRIPTION
Works just like Get-Variable, but automatically excludes the names of default globals, usually captured in the user's profile.ps1 file. Insert the line:

$PSDefaultVariables = (Get-Variable).name |% { PSEscape($_) } 

...wherever you want to (either before, or after, any other stuff in profile, depending on whether you want it to be excluded by running this command.)

.PARAMETER Name
(Optional) Refer to help for Get-Variable.

.PARAMETER ValueOnly
(Optional) Refer to help for Get-Variable.

.PARAMETER Include
(Optional) Refer to help for Get-Variable.

.PARAMETER Exclude
(Optional) Refer to help for Get-Variable; any names provided here will be added to the existing list stored in $PSDefaultVariables (sorted / unique'd to eliminate duplicates.)

.PARAMETER Scope
(Optional) Refer to help for Get-Variable. The only asterisk here is that the default value is "1" just to get us out of this function's own scope, but you can override with whatever value you need. 

.OUTPUTS
Refer to help for Get-Variable.

.EXAMPLE
PS> $foo = 1,2,3
PS> Get-UserVariable

Name                           Value
----                           -----
foo                            {1, 2, 3}
#>
}
Set-Alias -Name guv -Value Get-UserVariable

将下面一行放在profile.ps1文件的某个地方,或者放在最顶部,或者放在最底部,这取决于您是否希望自动排除每次运行交互式环境时定义的任何其他变量:

# Remember the names of all variables set up to this point (used by Get-UserVariable function)
$PSDefaultVariables = (Get-Variable).name

希望这是有帮助的!

相关问题