How to use export-csv with this Powershell script

Biju Thankappan 86 Reputation points
2021-08-12T02:54:26.017+00:00

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 for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2021-08-18T07:41:29.967+00:00

    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}},
    @{n='Capacity';e={'{0} GB' -f int}} |
    Export-Csv 'C:\output.csv' -NoType

    Best regards,
    Luis P

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2021-08-18T19:14:17.967+00:00

    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
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.