I’ve got the below script that I’m testing on my machine (Prometheus). I am pipelining 3 machine names into the Get-VolumeInfo function. Localhost is Prometheus, so I’m just getting the same volume information (2 volumes on the machine) twice from the same machine. I then have a fake machine name called Gotcha just to test the error capturing of the script. When I run the function with the -Verbose parameter, there is an unexpected blank line just after the first volume of the first computer name (i.e. localhost). See the output I get below the script code. It doesn’t happen when processing the volume information when the name Prometheus is passed to the function. Any idea why and how I can get rid of the blank line?
Function Get-VolumeInfo { [CmdletBinding()] Param ( [Parameter(Mandatory=$true, ValueFromPipeline = $True, HelpMessage = "Enter computer names, one a time. Hit the Enter key twice after typing in the last computer name. Or, re-run the script and put in a comma-separated listed after the -ComputerName parameter.")] [ValidateNotNullOrEmpty()] [string[]] $ComputerName, [string] $ErrorFile = 'C:\Data\Errors.txt', [switch] $LogErrors ) Begin { Write-Verbose "Starting Get-VolumeInfo." } #End Begin block Process { ForEach($Computer in $ComputerName) { Try { Write-Verbose "Getting Win32_Volume data from: $Computer" $Volumes = Get-WmiObject -Class Win32_Volume -ComputerName $Computer -Filter "DriveType = 3" -ErrorAction Stop } #End Try block Catch { If($LogErrors) { Write-Verbose "Failed to get Win32_Volume information. See $ErrorFile for further information." $ErrMsgCS = "$(Get-Date): [ERROR]: Failed to get the Win32_Volume information from $Computer. Error message was: $($_.Exception.Message)" $ErrMsgCS | Out-File -FilePath $ErrorFile -Append } Else { Write-Verbose "Failed to get Win32_Volume information. To log the error details, re-run the script with the -LogErrors parameter." } } #End Catch block If($Volumes) { ForEach($Volume in $Volumes) { Write-Verbose "Processing volume: $($Volume.Name)" $VolumeProps = @{ ComputerName = $Volume.SystemName; Drive = $Volume.Name; 'FreeSpace (GB)' = "{0:N2}" -f ($Volume.FreeSpace / 1GB); 'Size (GB)' = "{0:N2}" -f ($Volume.Capacity / 1GB) } New-Object -TypeName PSObject -Property $VolumeProps } #End ForEach block } #End If block Remove-Variable -Name 'Volumes' -ErrorAction SilentlyContinue } #End ForEach block } # End Process block End { Write-Verbose "Ending Get-VolumeInfo." } #End End block } #End Function block 'localhost','prometheus','gotcha' | Get-VolumeInfo -Verbose -LogErrors
Here is the output I get.
VERBOSE: Starting Get-VolumeInfo.
VERBOSE: Getting Win32_Volume data from: localhost
VERBOSE: Processing volume: C:
----Unexpected blank line ----
VERBOSE: Processing volume: \?\Volume{463fb708-9f47-410b-9051-d8ddaf9ae75b}
VERBOSE: Getting Win32_Volume data from: prometheus
VERBOSE: Processing volume: C:
VERBOSE: Processing volume: \?\Volume{463fb708-9f47-410b-9051-d8ddaf9ae75b}
VERBOSE: Getting Win32_Volume data from: gotcha
VERBOSE: Failed to get Win32_Volume information. See C:\Data\Errors.txt for further information.
VERBOSE: Ending Get-VolumeInfo.
Drive ComputerName FreeSpace (GB) Size (GB)
C:\ PROMETHEUS 41.71 237.20
\?\Volume{463fb708-9f47-410b-9051-d8ddaf9ae75b}\ PROMETHEUS 0.53 0.90
C:\ PROMETHEUS 41.71 237.20
\?\Volume{463fb708-9f47-410b-9051-d8ddaf9ae75b}\ PROMETHEUS 0.53 0.90