- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经创建了一个具有各种属性的自定义 PSObject,并且我正在尝试为其中一些属性重载 ToString。我在 TimeSpan 属性之一上取得了成功,但在 WMI 对象上却没有。
# Get WMI info
$wmiOS = Get-WmiObject -Class Win32_OperatingSystem
$wmiSystem = Get-WmiObject -Class Win32_ComputerSystem
$wmiDrives = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType='3'"
$ipv4Address = (Test-Connection -ComputerName $env:COMPUTERNAME -Count 1).IPV4Address.IPAddressToString
$uptime = (Get-Date) - ($wmiOS.ConvertToDateTime($wmiOS.LastBootUpTime))
# Setup object
$result = New-Object -TypeName psobject -Property @{
Hostname = $($wmiSystem.Name)
Domain = $($wmiSystem.Domain)
Username = $($wmiSystem.Username)
OperatingSystem = $($wmiOS.Caption)
OSArchitecture = $($wmiOS.OSArchitecture)
PSVersion = "$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)"
IPv4Address = $ipv4Address
Uptime = $uptime
DriveInfo = $wmiDrives
}
# Overload DriveInfo ToString
$result.DriveInfo = $result.DriveInfo | Add-Member -MemberType ScriptMethod -Name ToString -Value {
"$($this.DeviceID) $($this.FreeSpace) GB;"
} -Force -PassThru
# Overload Uptime ToString
$result.Uptime = $result.Uptime | Add-Member -MemberType ScriptMethod -Name ToString -Value {
"$($this.Days) days, $($this.Hours) hours, $($this.Minutes) minutes, $($this.Seconds) seconds"
} -Force -PassThru
# Overload result ToString
$result = $result | Add-Member -MemberType ScriptMethod -Name ToString -Value {
$temp = @"
Hostname: $($this.Hostname)
Domain: $($this.Domain)
Console User: $($this.Username)
Operating System: $($this.OperatingSystem)
OS Architecture: $($this.OSArchitecture)
PS Version: $($this.PSVersion)
IPv4 Address: $($this.IPv4Address)
Uptime: $($this.Uptime.ToString())
Free Space: $($this.DriveInfo)
"@
$temp
} -Force -PassThru
Domain: domain.local
Console User: domain\username
Operating System: Microsoft Windows 10 Pro
OS Architecture: 64-bit
PS Version: 5.1
IPv4 Address: 1.1.1.1
Uptime: 2 days, 1 hours, 31 minutes, 49 seconds
Free Space: C: 58721181696; E: 631496687616;
2 days, 1 hours, 19 minutes, 34 seconds
System.Object[]
最佳答案
你可以尝试替换:
$result.DriveInfo = $result.DriveInfo | Add-Member -MemberType ScriptMethod -Name ToString -Value {"$($this.DeviceID) $($this.FreeSpace) GB;"} -Force -PassThru
$result.DriveInfo | Add-Member -MemberType ScriptMethod -Name ToString -Value {"$($this.DeviceID) $($this.FreeSpace) GB;"} -Force -PassThru
Add-Member
应用于每个驱动器,您不需要影响结果,因为您更改了列表中的每个对象。之后,您可以使用以下方法进行测试:
$n = 0
$result.DriveInfo[$n].ToString()
关于powershell - 在嵌套 PSObject 属性上重载 ToString 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52048745/
使用以下示例: $test = '{ }' | ConvertFrom-Json 如何检测 $test 为空? 不起作用: $test -eq $null -not $test 这确实有效,但感觉不对
假设我有一个像这样的 psobject : $o=New-Object PSObject -Property @{"value"=0} Add-Member -MemberType ScriptMet
在执行 Get-ADObject 查询后,我得到了两个 PSObject(用于 2 个单独的 OU),我们称它们为 $query1 和 $query2,它们包含类型的成员对象 Selected.Mic
我创建一个这样的数组: $Array = @() $Item = New-Object PSObject $Item | Add-Member -Type NoteProperty -Name ite
下面的这个模块返回一个空对象。有什么建议可以解决吗? New-Module -ScriptBlock { $resourcesDirectory="AA" ..... $com
提供我有以下 JSON { "firstName": "Frank", "lastName": "Smith", "age": "25", "address":
在 PowerShell v3.0 中引入了 PSCustomObject。它类似于 PSObject,但更好。除了其他改进(例如保留属性顺序)之外,还简化了从哈希表创建对象的过程: [PSCusto
我一直在通过向 PSObjects 动态添加方法来实现临时类型 我希望能够使用“-eq”、“-lt”和“-gt”运算符比较我的对象的两个实例(我假设这需要我实现 IComparible 和 IEqua
此 powershell 脚本在 csv 文件中最多占用 8 行,并通过复制列将它们组合成一行,然后保存在结果文件中(第一个结果文件保存得很好)。如果 csv 中有 16 行,则意味着保存第二个结果文
我正在尝试在PowerShell中创建一个具有DataGrid且随源更新而更新的UI,所以我正在使用ObservableCollection。但是,当我将其设置为ItemsSource时,DataGr
我借用了一些PowerShell代码来比较哈希表,它返回一个带有哈希条目的名称和差异指示的自定义对象。我想输出返回的差额。 物体: function result( [string]$side )
假设我有这样的 JSON: { "a" : { "b" : 1, "c" : 2 } } 现在 ConvertTo-Json将愉快地创造
我经常使用以下内容: New-Object psobject -Property @{a=1; b=2; c=3; d=4} 我想让它尽可能短,甚至 1 个字符? 使用上述属性创建新 psobject
当显式转换我的新对象时(参见第一行代码),我新创建的 EntityReference 被存储而没有被包裹在 PSObject 中。 ,所以序列化它工作得很好: $entityRef1 = [Micro
当我使用 Get-Volume 时 $Volume = Get-Volume $Volume 我得到结果 DriveLetter FriendlyName FileSystemType DriveTy
这是我的代码: $a = @() for ($i = 0; $i -lt 5; $i++) { $item = New-Object PSObject $item | Add-Member -type
我最近开始在我的一些 PowerShell 脚本中使用自定义对象。在网上环顾四周时,我注意到有两种方法可以创建这些对象 $Obj1 = New-Object System.Object $Obj2 =
给定一个从 json (foo.json) 创建的自定义 powershell 对象 (bar) 您将如何按字母顺序对对象进行排序? foo.json { "bbb": {"zebras": "f
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
这是一个简单的问题,比任何事情都更具理论性。 Powershell 中的自定义对象和 PSObject 之间的真正区别是什么?您为什么以及何时创建它们? 谢谢! 最佳答案 PSCustomObject
我是一名优秀的程序员,十分优秀!