programing

이전에 로드된 모든 PowerShell 변수 나열

yellowcard 2023. 8. 23. 21:43
반응형

이전에 로드된 모든 PowerShell 변수 나열

이전에 로드된 모든 변수를 나열하는 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

ls variable:*작동해야 합니다.Get-Variable이러한 결과로 인해 출력이 저하되는 경우에는 PowerShell 자체가 아닌 제대로 구현되지 않은 호스트가 원인입니다.표준 콘솔 호스트(powershell.exe 실행)를 열면 이러한 호스트가 정상적으로 작동함을 알 수 있습니다.

잘못된 호스트를 해결해야 하는 경우 모든 것을 명시적 문자열로 덤프하는 것이 좋습니다.

Get-Variable | Out-String

또는

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

흥미롭게도, 당신은 그냥 입력할 수 있습니다.variable그리고 그것도 효과가 있습니다!

저는 무엇이 궁금했기 때문에 이것을 알게 되었습니다.ls variable:*하고 있었습니다.Get-Help ls는 PowerShell의 Get-ChildItem의 별칭으로, 개체의 모든 하위 항목을 나열할 것으로 알고 있습니다.그래서 저는 그냥.variable그리고 voila!

이것과 이것을 바탕으로 볼 때, 무엇이ls variable:*하고 있는 것은 그것이 그것을 사용하여 일종의 범위/범위 조회를 하도록 지시하는 것입니다.*의 와일드카드variable리스트, 이 경우, 외부적으로 보이는 (ls variable:*==ls variable:==variable).

저는 종종 상호작용적인 쉘에서 개념을 시험하면서 빈둥거리지만, 때때로 제가 정의한 변수 이름이나 무엇이든 잊어버립니다.

여기 제가 포장하기 위해 작성한 기능이 있습니다.Get-Variable셸이 시작될 때 정의된 전역을 자동으로 제외합니다.

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 

이것이 도움이 되기를 바랍니다!

언급URL : https://stackoverflow.com/questions/12465989/list-all-previously-loaded-powershell-variables

반응형