有人可以帮我获取磁盘信息吗?我有 3 个磁盘,但我无法使用添加成员获取它们的信息。
我收到错误:
"Add-Member : Cannot add a member with the name "Disks" because a member with that name already exists. If you want to overwrite the member anyway, use the Force parameter to overwrite it."
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
function Get-Inven {
param([string[]]$computername)
#Import-Module ActiveDirectory
foreach ($computer in $computername) {
$disks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computer -Filter 'DriveType=3'
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
#$comp = Get-ADComputer -Filter { cn=$computer }
$info = @{
'ComputerName'=$computer;
'OSVersion'=$os.caption;
'DnsHostName'=$comp.dnshostname
}
$obj = New-Object -TypeName PSObject -Property $info
foreach ($disk in $disks) {
$info = @{
'DriveLetter'=$disk.deviceID;
'FreeSpace'=($disk.freespace / 1MB -as [int])
}
$diskobj = New-Object -TypeName PSObject -Property $Info
$obj | Add-Member -MemberType NoteProperty -Name Disks -Value $diskobj
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果添加 -Force 参数,您仍然可以设置 Name 属性。您还应该添加 -PassThru 开关参数以将对象发送回管道:
$obj | Add-Member -MemberType NoteProperty -Name Disks -Value $diskobj -Force -PassThru
Run Code Online (Sandbox Code Playgroud)
更新:
在我看来,您可以简化该功能(无需添加成员调用):
foreach ($computer in $computername) {
$disks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computer -Filter 'DriveType=3'
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
#$comp = Get-ADComputer -Filter { cn=$computer }
$info = @{
ComputerName=$computer
OSVersion=$os.caption
DnsHostName=$comp.dnshostname
FreeSpaceMB= ($disks | foreach { "{0},{1:N0}" -f $_.Caption,($_.freespace/1MB) }) -join ';'
}
New-Object -TypeName PSObject -Property $info
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15521 次 |
最近记录: |