How to create a .csv output of the following PowerShell script
hello,
i have following script....it's not pretty i'll admit. right can output .txt file each wmi call output list. i'm trying output file .csv file each value in row (.csv file). i'm thinking have make changes way call each wmi. i'm fine if don't output .txt file.
i know has format-list function, i'm not sure use instead.
suggestions of can appreciated...
$result = @() foreach($server in (gc c:\apps\servers.txt)){ $computer = [adsi](”winnt://” + $server + “,computer”) $group = $computer.psbase.children.find(”administrators”) function getadmins {$members = ($group.psbase.invoke(”members”) | %{$_.gettype().invokemember(”adspath”, ‘getproperty’, $null, $_, $null)}) -replace ('winnt://domain/' + $server + '/'), '' -replace ('winnt://domain/', 'domain\') -replace ('winnt://', '') $members} function getos { gwmi -computer $server win32_operatingsystem | format-list @{expression={$_.caption};label="os name"},serialnumber,osarchitecture $ping = gwmi win32_pingstatus -filter "address='$server'" if($ping.statuscode -eq 0){$pcip=$ping.protocoladdress} write-output "ip address: " $pcip } function getsysteminfo { gwmi -computer $server win32_computersystem | format-list name,domain,manufacturer,model,systemtype} function getdiskinfo { $wmi = gwmi -computer $server win32_logicaldisk foreach($device in $wmi){ if ($device.name -eq "c:" -or $device.name -eq "d:") { write-output "drive: " $device.name write-output "size: "; "{0:n2}" -f ($device.size/1gb) + " gb" #write-output "freespace: "; "{0:n2}" -f ($device.freespace/1gb) + " gb" "" } } } function ip { $ping = gwmi win32_pingstatus -filter "address='$server'" if($ping.statuscode -eq 0){$pcip=$ping.protocoladdress} write-output $pcip } #$result += write-output "server: $server" $result += (getsysteminfo) #$result += (ip) $result += (getos) $result += write-output '... ' $result += ( getadmins ) $result += write-output '... ' $result += (getdiskinfo) $result += write-output '... ' $result += write-output '============================' $result1 = (getsysteminfo) $result2 = (getos) $result3 = ( getadmins ) $result4 = (getdiskinfo) $resultcsv = $result1+";"+$result2+";"+$result3+";"+$result4 } $result > c:\apps\result.txt $resultcsv > c:\apps\result.csv
hi,
$result = @() foreach($server in (gc c:\apps\servers.txt)){ $computer = [adsi](”winnt://” + $server + “,computer”) $group = $computer.psbase.children.find(”administrators”) $members = ($group.psbase.invoke(”members”) | %{$_.gettype().invokemember(”adspath”, ‘getproperty’, $null, $_, $null)}) -replace ('winnt://domain/' + $server + '/'), '' -replace ('winnt://domain/', 'domain\') -replace ('winnt://', '') $getos=gwmi -computer $server win32_operatingsystem | select @{expression={$_.caption};label="os name"},serialnumber,osarchitecture $ping = gwmi win32_pingstatus -filter "address='$server'" if($ping.statuscode -eq 0){$pcip=$ping.protocoladdress} $getsysteminfo=gwmi -computer $server win32_computersystem | select name,domain,manufacturer,model,systemtype} function getdiskinfo { $wmi = gwmi -computer $server win32_logicaldisk foreach($device in $wmi){ if ($device.name -eq "c:" -or $device.name -eq "d:") { $size= "{0:n2}" -f ($device.size/1gb) + " gb" "" } } } $obj = new-object psobject -property @{ name=$server os=$getos.'os name' serialnumber=$getos.serialnumber osarchitecture=$getos.osarchitecture ipaddress = $pcip systeminfo=$getsysteminfo.name domain=$getsysteminfo.domain manufacturer=$getsysteminfo.manufacturer model=$getsysteminfo.model systemtype=$getsysteminfo.systemtype } $result += $obj write-output $result $result |export-csv c:\apps\result.csv
we use format-table command out put result in row.
and export-csv out put files csv file. more details it, please refer below link:
http://technet.microsoft.com/en-us/library/dd347724.aspx
http://technet.microsoft.com/en-us/library/ee176825.aspx
best regards,
yan li
yan li
technet community support
Windows Server > Windows PowerShell
Comments
Post a Comment