Format table in text file

This topic contains 2 replies, has 3 voices, and was last updated by  js 1 week, 3 days ago.

  • Author
    Posts
  • #104111

    indra26
    Participant

    Hello all,
    I am newbie in powershell scripting and trying to get cpu,memory and diskspace in windows 2k12
    I have tried to make but i am getting output in mess manner and wrap text i need output should be in table format.
    Please help me to make this.
    Thanks.

    $ServerListFile = "D:\serverList.txt"  
    $ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue 
    $Result = @() 
    ForEach($computername in $ServerList) 
    {
    
    $AVGProc = Get-WmiObject -computername $computername win32_processor |
    Measure-Object -property LoadPercentage -Average | Select Average
    $OS = gwmi -Class win32_operatingsystem -computername $computername |
    Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ($_.FreePhysicalMemory / 1000000 ) }} 
    $mem = gwmi -Class win32_operatingsystem -computername $computername |
    Select-Object @{Name = "Memorypercentage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
    $vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" |
    Select-object @{Name = "C PercentFree"; Expression = {“{0:N2}” -f  (($_.FreeSpace/1GB));"GB";"/";(($_.Capacity/1GB));"GB"; } } 
    $dvol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'D:'" |
    Select-object @{Name = "D PercentFree"; Expression = {“{0:N2}” -f  (($_.FreeSpace/1GB));"GB";"/";(($_.Capacity/1GB));"GB"; } }
    
    $result += [PSCustomObject] @{ 
        ServerName = "$computername"
        CPULoad = "$($AVGProc.Average)"
        Mempercentage = "$($mem.Memorypercentage)%"
        MemLoad = "$($OS.MemoryUsage)"
        CDrive = "$($vol.'C PercentFree')"
        DDrive = "$($dvol.'D PercentFree')"
    }
    
    
    $Outputreport = "Server Health Report"
    $Outputreport += "$(Get-Date -Format yyyy-MM-dd-hhmm)"
    $Outputreport += "Server IP $($Result.ServerName)"
    $Outputreport += "CPU Load $($Result.CPULoad)"
    $Outputreport += "Memory Utilization $($Result.Mempercentage)"
    $Outputreport += "Memory Available $($Result.MemLoad)"
    $Outputreport += "C Drive $($Result.CDrive)"
    $Outputreport += "D Drive $($Result.DDrive)"
    }
    
    $Outputreport += ping 192.168.110.1 #gateway
    $Outputreport += netstat -n
    $Outputreport | out-file "D:\health.txt"
    
    
  • #104117

    iain Barnetson
    Participant

    Here's an extract of how I do it

    $Storage = Get-VolumeInfo $Servers 
    
    # Construct table 
    $Table = $null
    $Table = New-Object System.Data.DataTable "Storage"
    $Col1 = New-Object System.Data.DataColumn ServerName,([string])
    $Col2 = New-Object System.Data.DataColumn RunDate,([string])
    $Col3 = New-Object System.Data.DataColumn Disk,([string])
    $Col4 = New-Object System.Data.DataColumn SizeGB,([string])
    $Col5 = New-Object System.Data.DataColumn FreeSpaceGB,([string])
    $Col6 = New-Object System.Data.DataColumn FreeSpacepercent,([string])
    $Table.Columns.Add($Col1)
    $Table.Columns.Add($Col2)
    $Table.Columns.Add($Col3)
    $Table.Columns.Add($Col4)
    $Table.Columns.Add($Col5)
    $Table.Columns.Add($Col6)
    
    $Row = $Table.NewRow()
    $Row.ServerName = $server.ServerName
    $Row.RunDate = Get-Date
    $Row.Disk = $server.Name
    $Row.SizeGB = $server.SizeGB
    $Row.FreeSpaceGB = $server.FreeSpaceGB
    $Row.FreeSpacePercent = $server.FreeSpacePercent
    $Table.Rows.Add($row)
    
    
  • #104126

    js
    Participant

    Why don't you make $Outputreport another [pscustomobject]?

You must be logged in to reply to this topic.