PowerShell 分析变量和对象

标签 powershell console

我发现我做 .GetType()| Get-Member很多,有时我会忘记它是 .GetType我尝试 .Get-Type呸!错误!),所以我一直在编写一个函数来尝试快速收集该信息。事实证明,这在控制台上工作时非常有用(我确保将核心命令放在每个输出之前,这样我就永远不会忘记与实际命令的连接,因此更多的是技术摘要,让我与语言保持联系)。

我很好奇是否有额外的复合命令来提取有用的通用信息,我们可以使用这些信息来报告给定对象的结构(即使某些需要更复杂的命令,我们也可以以非常紧凑的摘要格式快速获取这些信息)关于给定对象的有用见解)?

$a = @(1,2,"x") ; obj $a .这将返回 71 个方法(System.String 和 System.Int32)类型,因此我已将重复项删除到 50 个(很高兴快速查看可用的内容,但也许还可以以某种方式提及该数组中包含的不同类型?) .

• 某些输入当然会破坏功能,但即使是像这样的 ScriptBlock 示例也能正常工作 obj {$a; $x} .你甚至可以做 obj "".GetType() 之类的事情查看那里的方法和属性。

• 使用.ModuleGetType()可能是多余的,因为通常输出 CommonLanguageRuntimeLibrary ,但也许来自这些成员的其他有用信息(当然,所有信息在不同时间都有用,但我对通用摘要输出很好奇)?

• 一般来说,您使用的任何改进或其他复合命令或可用于在快速摘要 View 中破解打开对象信息的任何改进或其他复合命令都非常值得了解? :-)

更新为 -Force @Clint 建议:

function obj ($cmd) {
    if ($cmd -eq $null) { Write-Host "Object is `$null" ; return } 
    Write-Host "Contents:" -F Cyan
    $cmd
    ""
    Write-Host "(`$object).GetType()" -F Cyan -NoNewline ; Write-Host " :: [BaseType|Name|IsPublic|IsSerial|Module]"
    ($cmd).GetType() | % { "$($_.BaseType), $($_.Name), $($_.IsPublic), $($_.IsSerializable), $($_.Module)" }
    ""
    Write-Host "`$object | Get-Member -Force" -F Cyan
    $m = "" ; $p = "" ; $pp = "" ; $np = "" ; $sp = ""
    $msum = 0 ; $psum = 0 ; $ppsum = 0 ; $npsum = 0 ; $spsum = 0
    $($cmd | Get-Member -Force) | % {
        if ($_.MemberType -eq "Method") { if(!($m -like "*$($_.Name),*")) { $m += "$($_.Name), " ; $msum++ } }
        if ($_.MemberType -eq "Property") { if(!($p -like "*$($_.Name),*")) { $p += "$($_.Name), " ; $psum++ } }
        if ($_.MemberType -eq "ParameterizedProperty") { if(!($pp -like "*$($_.Name),*")) { $pp += "$($_.Name), " ; $ppsum++} }
        if ($_.MemberType -eq "NoteProperty") { if(!($np -like "*$($_.Name),*")) { $np += "$($_.Name), " ; $npsum++ } }
        if ($_.MemberType -eq "ScriptProperty") { if(!($sp -like "*$($_.Name),*")) { $sp += "$($_.Name), " ; $npsum++ } }
    }
    if($msum -ne 0) { Write-Host ":: Method [$msum] => $($m.TrimEnd(", "))" }
    if($psum -ne 0) { Write-Host ":: Property [$psum] => $($p.TrimEnd(", "))" }
    if($ppsum -ne 0) { Write-Host ":: ParameterizedProperty [$ppsum] => $($pp.TrimEnd(", "))" }
    if($npsum -ne 0) { Write-Host ":: NoteProperty [$npsum] => $($np.TrimEnd(", "))" }
    if($spsum -ne 0) { Write-Host ":: ScriptProperty [$spsum] => $($sp.TrimEnd(", "))" }
    ""
}

输出示例:
C:\> $a = @(123,"x")
C:\> def $a
Contents:
123
x

($object).GetType() :: [BaseType|Name|IsPublic|IsSerial|Module]
array, Object[], True, True, CommonLanguageRuntimeLibrary

$object | Get-Member -Force
:: Method [50] => CompareTo, Equals, GetHashCode, GetType, GetTypeCode, ToBoolean, ToByte, ToChar, ToDateTime, ToDecimal, ToDouble, ToInt16,
ToInt32, ToInt64, ToSByte, ToSingle, ToString, ToType, ToUInt16, ToUInt32, ToUInt64, Clone, Contains, CopyTo, EndsWith, GetEnumerator,
get_Chars, get_Length, IndexOf, IndexOfAny, Insert, IsNormalized, LastIndexOf, LastIndexOfAny, Normalize, PadLeft, PadRight, Remove, Replace,
Split, StartsWith, Substring, ToCharArray, ToLower, ToLowerInvariant, ToUpper, ToUpperInvariant, Trim, TrimEnd, TrimStart
:: Property [1] => Length
:: ParameterizedProperty [1] => Chars

最佳答案

你已经总结的很好了,你不妨补充一下

  • object | gm -Force # To add members that are usually hidden by default
    

    The Get-Member command uses the Force parameter to add the intrinsic members and compiler-generated members of the objects to the display. Get-Member gets these members, but it hides them by default.

    Intrinsic members (PSBase, PSAdapted, PSObject, PSTypeNames)

    Compiler-generated get_ and set_ methods



    MSDN文档
  • object.PSObject #Wraps the information pertaining to different members
    object.PSObject | Select-Object -ExpandProperty Properties #You can do the same for other members 
    
  • $object |gm -force -static #even with force the static members are not listed by default, so we need to explicitly mention static 
    
  • maybe good to somehow also mention the different Types that are contained in that array?).


    $object | ForEach-Object {$_.GetType().FullName}
    
  • OffTopic,另外,如果你想捕获函数执行所花费的时间
    $sw = [Diagnostics.Stopwatch]::StartNew()    
    $sw.Stop()
    Write-Host "Execution time" $sw.Elapsed.Milliseconds "ms"
    
  • 关于PowerShell 分析变量和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60237433/

    相关文章:

    powershell - 用户Get-ADUser列出所有属性并导出到.csv

    azure - 用于管理 Azure Batch 应用程序版本保留的 PS 脚本

    sql - 运行 SQL 文件时如何将内容打印到 postgres 输出控制台?

    c - 使控制台中字符的宽度和高度以像素为单位相等 (C)

    C# Powershell 互操作

    windows - 在 Windows 服务失败时运行 Powershell 脚本。手动运行时有效,但服务失败时无效。需要建议

    powershell - WinRM 客户端无法在指定时间内完成操作

    python - 将多行(读取为单行/输入)粘贴到 Spyder 控制台

    java - 为什么在循环中进行计算会减慢将打印写入控制台但不会减慢写入文件的速度?

    node.js - 用于控制台彩色输出的 Node 模板引擎