outputs not working as expected

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>
Length
7
7
7
6
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 :slight_smile:

#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

#HALP

 

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

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, 
                        DriveType, 
                        VolumeName, 
                        @{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))}}

Output:

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'}

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}}
$space