outputs not working as expected

Welcome Forums General PowerShell Q&A outputs not working as expected

Viewing 3 reply threads
  • Author
    • #202703
      Topics: 2
      Replies: 0
      Points: 7
      Rank: Member

      Hi All


      definitely new to powershell and this is breaking me. i have a script that works fine until i run it through an invoke-command to do it remotely. ive pulled this out of a larger script but then this is in it messes up the output to csv and i end up with this

      <colgroup><col style=”width: 48pt;” width=”64″ /> </colgroup>


      Help me Powershell.org. You’re my only hope

      #this works until i put it through the invoke-command.
      #some pointless notes on straight forward code :)
      #create a file and put a couple of servers in it, change this to that file
      $complist = "C:\temp\ewan\computers.txt"
      #change this where you want the output spitting out too
      $outreport = "C:\temp\ewan\superballs_" + $((Get-Date).ToString('MM-dd-yyyy')) + ".csv"
      #Invoke command breaks this 
      Invoke-Command -ComputerName (Get-Content $complist) -Credential $credentials -scriptblock {
      #gets physical disks on device
      $colDisks = get-wmiobject Win32_LogicalDisk -Filter "DriveType = 3"
      $Space = "No Space Issues"
      #works out %free on any disks
      foreach ($disk in $colDisks) {
      if ($disk.size -gt 0) {$PercentFree = [Math]::round((($disk.freespace/$disk.size) * 100))}
      else {$PercentFree = 0}
      $Drive = $disk.DeviceID
      "$Drive - $PercentFree"
      #if less that 10% free it should complain
      if ($PercentFree -le 10) {
      $Space = "Low on space";
      #writes some stuff that can by output to a csv
      $infoObject = New-Object PSObject
      Add-Member -inputObject $infoObject -memberType NoteProperty -name "Disk Space" -value $Space -Force
      $infoObject} | Select-Object * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName | Export-Csv -path $outreport -NoTypeInformation


    • #203487
      Topics: 5
      Replies: 12
      Points: 93
      Rank: Member

      Line 36 “}” right after $infoObject.  That should be at the end.  I am not at a place to test to confirm.

    • #203514
      Topics: 12
      Replies: 1642
      Points: 2,660
      Helping Hand
      Rank: Community Hero

      The script block code could be a bit more efficient, look at this:

      Get-CimInstance -ClassName Win32_logicaldisk -Filter "DriveType = '3'" | 
      Select-Object -Property DeviceID, 
                              @{Name='FreeSpaceGB';Expression={"{0:N2}" -f ($_.FreeSpace /1GB)}},
                              @{Name="CapacityGB" ;Expression={"{0:N2}" -f ($_.Size /1GB)}},
                              @{Name="PctFree"    ;Expression={([math]::Round(($_.FreeSpace /1GB) / ($_.Size /1GB) * 100))}}


      DeviceID    : C:
      DriveType   : 3
      VolumeName  : Windows-SSD
      FreeSpaceGB : 277.10
      CapacityGB  : 475.69
      PctFree     : 58

      Consider gathering information first and then do analysis. If you have results for computer, then just search for items that need action:

      $result = @()
      $result += [pscustomobject]@{ComputerName = 'Computer1';PctFree = 58}
      $result += [pscustomobject]@{ComputerName = 'Computer2';PctFree = 9}
      $result += [pscustomobject]@{ComputerName = 'Computer3';PctFree = 26}
      $result | Where{$_.PctFree -le'10'}
    • #204324
      Topics: 1
      Replies: 20
      Points: 109
      Helping Hand
      Rank: Participant

      Maybe PSDrive could work?  You’ll have to do some translations for the CSV output though, I think..

      $list = get-content C:\temp\ewan\computers.txt
      $space = %{Invoke-Command -ComputerName $list {Get-PSDrive C}}


Viewing 3 reply threads
  • The topic ‘outputs not working as expected’ is closed to new replies.