question

BijuThankappan-2567 avatar image
0 Votes"
BijuThankappan-2567 asked RichMatheisen-8856 edited

How to use export-csv with this Powershell script

Import-Module ActiveDirectory

$Servers = Get-ADcomputer -Filter {OperatingSystem -like "Server"} -Properties Name, OperatingSystem | Select Name
$diskReport = Foreach($Server in $Servers)
{

 #$Status = "Offline"    
 $Name = $Server.Name
 #Make sure server is online
 if(Test-Connection -ComputerName $Name -ErrorAction SilentlyContinue)
 {

     #Get only 10%       
     Get-WMIObject win32_logicaldisk -ComputerName $Name -Filter "DriveType=3" -ErrorAction SilentlyContinue
 }
 else
 {
     Write-Output $Name + "Offline/Unreachable"
 }       

}

$servers = $diskreport | Select-Object @{Label = "Server Name";Expression = {$.SystemName}},@{Label = "Drive Letter";Expression = {$.DeviceID}}


I'm looking to use export-csv on $servers so that I will get the below output:
Server Name OS Drives
xyz Windows Server 2019 C,D,E

TIA

windows-serverwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello

I thinks this can help you simplify it:

$serverlist = Get-Content .\servers.txt
Get-WmiObject Win32_Volume -Computer $serverlist |
Select-Object __SERVER, Name, Label, FreeSpace, Capacity |
Export-Csv 'C:\output.csv' -NoType

If you need particular column titles instead of the regular property names or values in a particular format use calculated properties to change that:

Get-WmiObject Win32_Volume -Computer $serverlist |
Select-Object @{n='Server Name';e={$.SERVER}}, @{n='Drive';e={$.Name}},
@{n='Disk Name';e={$.Label}},
@{n='FreeSpace';e={'{0} GB' -f [int]($
.FreeSpace/1GB)}},
@{n='Capacity';e={'{0} GB' -f [int]($_.Capacity/1GB)}} |
Export-Csv 'C:\output.csv' -NoType

Best regards,
Luis P

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

Something like this?

 Get-ADcomputer -Filter "OperatingSystem -like '*server*'" -Properties Name, OperatingSystem |
     ForEach-Object{
         $Status = "Offline/Unreachable"
         $Drives = @()
         $OS = $_.OperatingSystem
         if(Test-Connection -ComputerName $_.Name -Quiet){
             $Status = "Pingable"
             #Get only 10%  
             Try{     
                 Get-WMIObject win32_logicaldisk -ComputerName $_.Name -Filter "DriveType=3" -ErrorAction Stop |
                     ForEach-Object{
                         $drives += $_.DeviceID.Substring(0,1)
                     }
             }
             Catch{
                 $Drives += "Unavailable"
             }
         }
         else{    # you can remove the "else" condition entirely if you don't want to add anything to the processing
             # do nothing
         }
         [PSCustomObject]@{
             "Server Name" = $_.Name
             Status = $Status
             OS = $_.OperatingSystem
             "Drive Letter" = $Drives -join ';'
         }
     } | Export-Csv C:\Junk\Servers.csv -NoTypeInformation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.