How to get a lengthy string environment variable in full?

自闭症网瘾萝莉.ら 提交于 2021-01-28 11:43:13

问题


The command gci env:ApiSecret | ConvertTo-Json works to return a long string, the API secret for Twitter, which is truncated without the pipe to JSON.

However, the JSON is rather spammy.

Is there a "goldilocks" way to get the lengthy string value without the extraneous details?

(Unfortunately, gci env: truncates the key)


回答1:


Get-ChildItem is for retrieving all or a subset of items from a container. Note that it outputs an object with Name and Value properties (substituting Path as another lengthy environment variable value)...

PS> gci env:Path

Name                           Value
----                           -----
Path                           C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\WINDO...

Get-Item yields the same result...

PS> gi env:Path

Name                           Value
----                           -----
Path                           C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\WINDO...

Either way, the object retrieved is a DictionaryEntry...

PS> gi env:Path | Get-Member


   TypeName: System.Collections.DictionaryEntry

Name          MemberType    Definition
----          ----------    ----------
Name          AliasProperty Name = Key
Equals        Method        bool Equals(System.Object obj)
GetHashCode   Method        int GetHashCode()
GetType       Method        type GetType()
ToString      Method        string ToString()
PSDrive       NoteProperty  PSDriveInfo PSDrive=Env
PSIsContainer NoteProperty  bool PSIsContainer=False
PSPath        NoteProperty  string PSPath=Microsoft.PowerShell.Core\Environment::path
PSProvider    NoteProperty  ProviderInfo PSProvider=Microsoft.PowerShell.Core\Environment
Key           Property      System.Object Key {get;set;}
Value         Property      System.Object Value {get;set;}

...and when you pipe that to ConvertTo-Json it will include all kinds of undesirable properties from that class.

In short, don't use ConvertTo-Json for this. Since you know the exact item you want, just retrieve it directly using variable syntax...

PS> $env:Path
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;...

Equivalent code using the .NET API would be...

PS> [Environment]::GetEnvironmentVariable('Path')
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;...

If you really wanted to use a Get-*Item cmdlet you'd just need to specify that it's the Value property you want using property syntax...

PS> (gi env:Path).Value
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;...

...or Select-Object...

PS> gi env:Path | Select-Object -ExpandProperty 'Value'
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;...

All of the above commands will output only a [String] containing the entirety of that environment variable value. I inserted trailing ellipses since showing my entire Path value is not useful here; in practice, those commands will output the entire environment variable with no truncation.




回答2:


Your statement does not strip anything away. However, for console display purpose, it truncate the output that you view in the console.

If you assign the result to a variable or pipe to a file, nothing will be truncated.

Therefore, my assumption on your question is that you want to view the result in the console without the console truncating your stuff there.

For that, you could write the results to the host yourself. Here's a simple example that do just that.

$envvars = gci env: 
$Max = ($envvars.name| Measure-Object -Property length -Maximum).Maximum + 3
$envvars | % {Write-Host $_.name.padright($Max,' ')  -ForegroundColor Cyan -NoNewline;Write-Host $_.value}

Result — As you can see, the path variable value is no longer truncated.




回答3:


The simplest way to inspect the value of environment variables in full is to use the
$env:<varName> (namespace variable notation) syntax
, which in your case means: $env:ApiSecret (if the variable name contains special characters, enclose everything after the $ in {...}; e.g., ${env:ApiSecret(1)})

That way, environment-variable values (which are invariably strings) that are longer than your terminal's (console's) width simply continue on subsequent lines.

To demonstrate:

# Simulate a long value (200 chars.)
$env:ApiSecret = 'x' * 199 + '!'

# Output the long value
$env:ApiSecret

With an 80-char. wide terminal, you'd see output as follows:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx!

If you do want to use Get-Item (or Get-ChildItem, which acts the same in this case), you have two options:

# Format-List shows each property on its own line, 
# with values wrapping across multiple lines
Get-Item env:ApiSecret | Format-List

# Format-Table -Wrap causes values to wrap as well.
Get-Item env:ApiSecret | Format-Table -Wrap


来源:https://stackoverflow.com/questions/59868975/how-to-get-a-lengthy-string-environment-variable-in-full

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!