gpt4 book ai didi

powershell - 在自定义对象中返回信息的功能

转载 作者:行者123 更新时间:2023-12-02 23:30:40 26 4
gpt4 key购买 nike

我有一个遍历计算机上所有HDD的功能,并返回有关那些驱动器及其映射到阵列中物理驱动器的信息。

我希望此函数在自定义对象中返回信息。

这是函数:

##--------------------------------------------------------------------------
## FUNCTION.......: Get-HDDInfo
## PURPOSE........:
## REQUIREMENTS...:
## NOTES..........:
##--------------------------------------------------------------------------
Function Get-HDDInfo {
[CmdletBinding()]
Param([Parameter(Mandatory = $True,
ValueFromPipeLine = $True,
Position = 0)]
[String[]]$ComputerName
)#END: Param
$W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName)
$Array = @()

$W32_DD | foreach {
$query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" `
+ $_.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition"
$Array += $_.Name
$Array += $_.Model
<#
$obj = New-Object PSObject
$obj.PSObject.typenames.insert(0,'JoeIT.Custom.SystemInfo')
$obj | Add-Member -MemberType NoteProperty -Name `
"PDCaption" -Value $_.Name
$obj | Add-Member -MemberType NoteProperty -Name `
"PDModel" -Value $_.Model
$Array += $obj
#>
Get-WmiObject -Query $query | foreach {

$Array += $_.Name
$Array += $_.Description
$Array += $_.PrimaryPartition

#$obj = New-Object PSObject
<#
$obj.PSObject.typenames.insert(0,'JoeIT.Custom.SystemInfo')
$obj | Add-Member -MemberType NoteProperty -Name `
"DPName" -Value $_.Name
$obj | Add-Member -MemberType NoteProperty -Name `
"DPDescription" -Value $_.Description
$obj | Add-Member -MemberType NoteProperty -Name `
"DPPrimary" -Value $_.PrimaryPartition
#>
$query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" `
+ $_.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk"

Get-WmiObject -Query $query2 | ForEach {
$Array+= $_.Name
$Used = [math]::round($_.Size/1024/1024/1024,0)
$Free = [math]::round($_.FreeSpace/1024/1024/1024,0)
$Array += [String]$Used +"GB"
$Array += [String]$Free +"GB"
#Return $Array;
#$Array = $Null
}

<#
$Array += $obj
$obj = $Null
#>
}#END: Get-WmiObject -Query
}#END: $W32_DD | foreach
##----------------------------------------------------------------------
## Store results in custom Object
##----------------------------------------------------------------------
Return $Array
}#END: Function Get-HDDInfo

被注释掉的东西是由于我试图将信息放入自定义对象中。也许我只是有点精疲力尽,但是我似乎无法使这项工作正确。如您所见,注释掉的代码试图覆盖命名属性-我知道我在编写它时就知道了,但是由于某种原因,我希望它仍然可以工作;)

也许我不应该休息一天不工作三周,但是我的大脑不能让我解决这个问题。

我想要的是能够执行以下操作:
$test = (get-hddinfo $SVR01)
$test.PhysicalDrive1
$test.Partition1
$test.DriveLetter1
$test.TotalSize1
$test.FreeSpace1

这将查询一台名为SVR01的计算机,并写出第一个物理HDD,该驱动器的第一个逻辑分区,分配的驱动器号,磁盘的总大小以及磁盘上的可用空间。

然后我可以做类似的事情
$test.PhysicalDrive2
$(same code here for the second physical drive)

我到底在做什么错?

最佳答案

试试这个:

[CmdletBinding()]
Param([Parameter(Mandatory = $True,
ValueFromPipeLine = $True,
Position = 0)]
[String[]]$ComputerName
)
$W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName)

$a = new-object System.Object
$sc3 = 1
$sc2 = 1
$sc1 = 1
$W32_DD | foreach {
$query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" `
+ $_.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition"

$a | Add-Member -type NoteProperty -name DiskDriveName$sc1 -value $_.Name
$a | Add-Member -type NoteProperty -name DiskDriveModel$sc1 -value $_.Model

Get-WmiObject -Query $query | foreach {

$a | Add-Member -type NoteProperty -name PartitionName$sc2 -value $_.Name
$a | Add-Member -type NoteProperty -name PartitionDescription$sc2 -value $_.Description
$a | Add-Member -type NoteProperty -name PrimaryPartition$sc2 -value $_.PrimaryPartition

$query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" `
+ $_.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk"


Get-WmiObject -Query $query2 | ForEach {

$a | Add-Member -type NoteProperty -name LogicalDiskName$sc3 -value $_.Name

$Used = [math]::round($_.Size/1024/1024/1024,0)
$Free = [math]::round($_.FreeSpace/1024/1024/1024,0)

$a | Add-Member -type NoteProperty -name UsedSpace$sc3 -value $([String]$Used +"GB")
$a | Add-Member -type NoteProperty -name FreeSpace$sc3 -value $([String]$Free +"GB")

$sc3++
}
$sc2++
}
$sc1++
}

Return $a

关于powershell - 在自定义对象中返回信息的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11212240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com