【发布时间】:2020-02-15 09:27:01
【问题描述】:
我发现我经常使用.GetType() 和| Get-Member,有时我忘记了它是.GetType,我尝试.Get-Type duh!错误!),所以我一直在编写一个函数来尝试快速收集该信息。事实证明,这在控制台上工作时非常有用(我确保将核心命令放在每个输出之前,这样我就永远不会忘记与实际命令的连接,所以更多的技术摘要可以让我与语言保持联系)。
我很好奇是否有额外的复合命令来提取有用的通用信息,我们可以使用这些信息来报告给定对象的结构(我们可以以非常紧凑的摘要格式快速获得的东西,即使更复杂的命令是需要对给定对象进行一些有用的见解)?
•$a = @(1,2,"x") ; obj $a。这将返回 71 个方法(System.String 和 System.Int32)类型,因此我已将重复项删除到 50 个(很高兴快速查看可用的内容,但也许还可以以某种方式提及包含在该数组中的不同类型?) .
• 某些输入当然会破坏功能,但即使是这样的 ScriptBlock 示例也可以正常工作obj {$a; $x}。您甚至可以执行obj "".GetType() 之类的操作来查看其中的方法和属性。
• 在GetType() 中使用.Module 可能是多余的,因为通常输出CommonLanguageRuntimeLibrary,但可能来自这些成员的其他有用信息(当然,每件事在不同的时间都有用,但我很好奇通用摘要输出)?
• 一般而言,您使用或可能有助于在快速摘要视图中破解开放对象信息的任何改进或其他复合命令会很高兴知道吗? :-)
更新为@Clint 建议的-Force:
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
【问题讨论】:
-
是强制的 -F 吗?
-
它是
-ForegroundColor的简写,只要不与另一个开关冲突,PowerShell 允许您截断任何开关,所以我使用-F 表示ForegroundColour,使用-B 表示-BackgroundColor。节省打字! :) -
啊我明白了,你可以使用-Force然后
标签: powershell console