使用get-wmiobject和创建psobject并导出到csv时。如果对象1有4个头,但由于信息的迭代,对象2有8个头。您只能在导出csv中看到4个标题。
我只使用此帖子中的磁盘部分,因为这是我现在唯一遇到问题的部分。
如果您对15台服务器或计算机使用此功能。让我们说你在列表中反对的第一台计算机并导出到CSV有2个驱动器。虽然第二台电脑有5个驱动器。您将永远不会看到CSV中的2个驱动器。似乎数组对象的第一次迭代设置了标题。如果在运行该函数后使用变量$ donkeypages。您仍然可以看到要比较的每台机器的完整信息。
加载到个人资料中的示例命令。
info .\listofcomputers.txt disk 1
这应该为您提供列表中所有计算机的Info-List.csv以及它们拥有的磁盘。虽然测试时确保第一台机器的驱动器数量最少,而第二台机器的驱动器数量更多。重现问题。
我希望能解决这个问题。有点像这个在墙上撞我的头。我想看看所有的驱动器。无论顺序如何。或者,如果我可以计算迭代次数,然后将其设置为第一个设置标题的条目。或者动态设置标题。我甚至考虑过为每台服务器/计算机创建一个CSV,然后将它们全部组合起来。
以下是该脚本的示例。
function Info {
Param (
[Parameter(Position=1,Mandatory=$TRUE,HelpMessage="Supply a list of servers or computers or a single computer/server")]
$cn,
[Parameter(Position=2,Mandatory=$FALSE,HelpMessage="Variables proc=Processor, mem=Memory, sys=System, time=LocalSystemTime, nic=Network Card, hba=HBAInfo, user=UserInfo, disk=Disk, freedisk=Free Disk Space, 3=PAE Switch, bios=Bios, os=Operating System, =ALL")]
$actn,
[Parameter(Position=3,Mandatory=$FALSE,HelpMessage="CSV 1 or 0. This will export the information into a csv if this is set to 1.")]
$csv
)
PROCESS {
if (($cn -like "*.csv") -or ($cn -like "*.txt")) {
$cn = Get-Content $cn
} else {
$cn = $cn
}
$donkeypages = @()
foreach ($c in $cn) {
if (Test-Connection -Cn $c -BufferSize 16 -Count 1 -ea 0 -quiet) {
$obj = new-Object PSObject
$obj | Add-Member NoteProperty Computername $c
write-host "$c is alive" -f green -b black
switch ($actn) {
"disk" {
try {
Get-WmiObject win32_diskdrive -cn $c -ErrorAction stop > $null
$DISKDRIVE = Get-WmiObject win32_diskdrive -cn $c | Select-Object model,interfacetype,partitions
FOR($i=0;$i -lt $DISKDRIVE.length; $i++) {
$obj | Add-Member NoteProperty "$i DiskModel" ($diskdrive[$i].model)
$obj | Add-Member NoteProperty "$i DiskInterfaceType" ($diskdrive[$i].interfacetype)
$obj | Add-Member NoteProperty "$i DiskPartitions" ($diskdrive[$i].partitions)
}
}
catch {
$Exception = $_
switch ($Exception){
{($Exception.Exception.Message -like "*Access is denied*")} {Write-Warning "Access denied to $env:username"; $obj | Add-Member NoteProperty "0 DiskModel"("Failed to access server with account $env:username")}
{($Exception.Exception.Message -like "*The RPC server is unavailable*")} {Write-Warning "System RPC connection not accessible"; $obj | Add-Member NoteProperty "0 DiskModel"("RPC Failure")}
}
}
try {
Get-WmiObject win32_volume -cn $c -ErrorAction stop > $null
$VOLUME = Get-WmiObject win32_volume -cn $c | Select-Object SerialNumber,Label,Name,@{Label='Capacity';EXPRESSION={"{0:N2}" -f ($_.capacity/1GB)}},@{Label='GBFreeSpace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}}
FOR($i=0;$i -lt $VOLUME.length; $i++) {
$obj | Add-Member NoteProperty "$i DiskSerialNumber" ($volume[$i].serialnumber)
$obj | Add-Member NoteProperty "$i DiskLabel" ($volume[$i].label)
$obj | Add-Member NoteProperty "$i DiskName" ($volume[$i].name)
$obj | Add-Member NoteProperty "$i DiskCapacity" ($volume[$i].capacity)
$obj | Add-Member NoteProperty "$i DiskFreeSpace" ($volume[$i].gbfreespace)
}
}
catch {
$Exception = $_
switch ($Exception) {
{($Exception.Exception.Message -like "*Invalid class*")} {Write-Warning "System does not contain the Win32_Volume class"; $obj | Add-Member NoteProperty "0 DiskSerialNumber" ("Cannot find class win32_volume")}
{($Exception.Exception.Message -like "*The RPC server is unavailable*")} {Write-Warning "System RPC connection not accessible"; $obj | Add-Member NoteProperty "0 DiskSerialNumber"("RPC Failure")}
{($Exception.Exception.Message -like "*Access is denied*")} {Write-Warning "Access denied to $env:username"; $obj | Add-Member NoteProperty "0 DiskSerialNumber"("Failed to access server with account $env:username")}
}
}
}
if ($_.systemstartupoptions) {
$obj | ForEach-Object {$_.systemstartupoptions = [string]$_.systemstartupoptions}
}
if (!$csv) {
Write-Output $obj | Format-List *
$donkeypages += $obj
} else {
$donkeypages += $obj
}
} Else {
write-warning "$c is not alive"
}
}
if ($csv) {
$donkeypages | Export-Csv -notype ".\Info-List.csv"
} else {
$global:donkeypages = $donkeypages
}
}
}