Phrasing data from power shell

Welcome Forums General PowerShell Q&A Phrasing data from power shell

Viewing 3 reply threads
  • Author
    Posts
    • #235081
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member
      
      $ServerListFile = "C:\Servers.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 ((($_.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 / $_.Capacity)*100) } }
      
      $operatingSystem = Get-WmiObject -computername $computername Win32_Service | where {$_.Name -eq "ALG"} | select-object Name, State 
      
      $result += [PSCustomObject] @{ 
          ServerName = "$computername"
          CPULoad = "$($AVGProc.Average)%"
          MemLoad = "$($OS.MemoryUsage)%"
          CDrive = "$($vol.'C PercentFree')%"
          Uptime = "$operatingsystem"
      }
      $Outputreport = "<HTML><TITLE> Loftware Health Report </TITLE>
                       <BODY background-color:peachpuff>
                       <font color =""#99000"" face=""Microsoft Tai le"">
                       

      Server Health Check

      </font> " Foreach($Entry in $Result) { if(($Entry.CpuLoad) -or ($Entry.memload) -ge "80") { $Outputreport += "" } else { $Outputreport += "" } $Outputreport += "" } $Outputreport += "
      Server Name Avrg.CPU Utilization Memory Utilization Drive C Free Space Service
      $($Entry.Servername)$($Entry.CPULoad)$($Entry.MemLoad)$($Entry.CDrive)$($Entry.Uptime)
      </BODY></HTML>" } $Outputreport | out-file "C:\test.html"
    • #235099
      Participant
      Topics: 3
      Replies: 431
      Points: 1,533
      Helping Hand
      Rank: Community Hero

      Jimmy, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please. In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.

      Thanks in advance.

    • #235111
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      thanks man my question is how can i remove @{Name=ALG; State=Stopped} to just say Stopped

    • #235123
      Participant
      Topics: 3
      Replies: 431
      Points: 1,533
      Helping Hand
      Rank: Community Hero

      Are you referring to this line?

      $operatingSystem = Get-WmiObject -computername $computername Win32_Service | where {$_.Name -eq "ALG"} | select-object Name, State 
      

      I don’t see @{Name=ALG; State=Stopped}. It’s odd that the variable is called operatingsystem when it’s really collecting services. If you’re just wanting a list of all services that are stopped, this may be better.

      Get-Service -computername $computername | Where-Object {$_.Status -eq 'stopped'} | Select-Object name, status
      

      You could also get the display name as it’s usually more human friendly. Another consideration is if it’s set to automatic, manual, or disabled. Do you really want to know the services that are stopped if they are set to disabled? Also, you can break the lines at the | pipe character to improve readability. Lastly, you may also want to sort them further. This command may be more useful.

      Get-Service -computername $computername |
          Where-Object {$_.StartType -ne 'disabled' -and $_.Status -eq 'stopped'} |
              Select-Object Displayname, name, Starttype, status | Sort-Object -Property starttype, displayname
      

      After rereading I’m starting to think you are wanting the status of just the ALG service, and you want the output (that you haven’t shown) to only list Stopped instead of @{Name=ALG; State=Stopped} If this is the case, change your line to this.

      $operatingSystem = Get-WmiObject -computername $computername Win32_Service | where {$_.Name -eq "ALG"} | select-object -expandproperty state
      

      I still recommend renaming the variables to what they are actually for, so that you don’t confuse yourself or others down the road. I’d also encourage you to either use Get-Service or Get-CimInstance

      $ALGServiceState = Get-Service -computername $computername | where-object name -like "*alg*" | Select-Object -ExpandProperty status
      OR
      $ALGServiceState = Get-CimInstance -CimSession $computername -ClassName win32_service | where-object name -like "*alg*" | Select-Object -ExpandProperty state
      

      Hope this helps.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.