我发现我做 .GetType()
和 | Get-Member
很多,有时我会忘记它是 .GetType
我尝试 .Get-Type
呸!错误!),所以我一直在编写一个函数来尝试快速收集该信息。事实证明,这在控制台上工作时非常有用(我确保将核心命令放在每个输出之前,这样我就永远不会忘记与实际命令的连接,因此更多的是技术摘要,让我与语言保持联系)。
我很好奇是否有额外的复合命令来提取有用的通用信息,我们可以使用这些信息来报告给定对象的结构(即使某些需要更复杂的命令,我们也可以以非常紧凑的摘要格式快速获取这些信息)关于给定对象的有用见解)?
• $a = @(1,2,"x") ; obj $a
.这将返回 71 个方法(System.String 和 System.Int32)类型,因此我已将重复项删除到 50 个(很高兴快速查看可用的内容,但也许还可以以某种方式提及该数组中包含的不同类型?) .
• 某些输入当然会破坏功能,但即使是像这样的 ScriptBlock 示例也能正常工作 obj {$a; $x}
.你甚至可以做 obj "".GetType()
之类的事情查看那里的方法和属性。
• 使用.Module
在 GetType()
可能是多余的,因为通常输出 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}
$sw = [Diagnostics.Stopwatch]::StartNew()
$sw.Stop()
Write-Host "Execution time" $sw.Elapsed.Milliseconds "ms"
关于PowerShell 分析变量和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60237433/