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



The most immediate problem is that Invoke-Command runs just one command at a time as a child session of the parent. As described in the about_Remote documentation, when you run commands in a scriptblock with Invoke-Command, each command is executed in a separate session, and then the session terminates when that command finishes. Any data stored in variables is destroyed when the session terminates. For instance, the foreach statement at line 18 does not have access to $colDisks created on line 15 because as soon as storing the output from Get-WMIObject was finished, the command would be complete and that session would terminate.

What you need to do is create a remote session New-PSSession and then run your commands inside that session so that the variable data is retained and can be shared by multiple commands within the session until it closes, as described in Example 4.

You should also read the about_scopes documentation to understand how variable data is limited to sessions, and how to pass that data between sessions.

Mmm. No, the Invoke-Command session is a single session throughout its scriptblock. That’s not the issue here.

Your issue here is line 23, where you output a string to the pipeline. This is then captured with Select-Object and sent to Export-Csv. The only property a string has is Length, so that’s what you see in the CSV. Also, the CSV format can only really hold objects of the same shape (same number and name of properties), so the first object it receives is what determines the properties that are recorded. Since the string is received first, the cmdlet is only looking for the one Length property thereafter.

Rather than mixing the two output types, it would be much more effective to simply use Write-Host to print the string to the host rather than being caught up in the standard output stream.