0
votes

I have written a system maintenance script which executes basic functions that retrieve statistics from a host, writes the output to a new PSObject, then finally combines the results and converts it all to a HTML web page.

I do not seem to be able to write the output of Optimize-Volume to the pipeline, I have to use -verbose - why is this? I would like to check the results of the Optimize-Volume cmdlet by looking for the following text which is generated at the end of the -verbose output, depending on the result:-

  • 'It is recommended that you defragment this volume.'
  • 'You do not need to defragment this volume.'

Here is the function:-

    function Get-DefragInfo {

    $getwmi = Get-WmiObject -Class Win32_volume -Filter "DriveType = 3" | Where-Object {$_.DriveLetter -cne $null} -ErrorAction SilentlyContinue
    $letter = $getwmi.DriveLetter -replace ':'

    foreach ($drive in $getwmi)
    {
    $analysis = Optimize-Volume -DriveLetter $letter -Analyze
    if ($analysis -like 'It is recommended that you defragment this volume.')
    {
    $props =[ordered]@{‘Drive Letter’=$letter
              'Defrag Recommended?'='Yes'} 
    }
    elseif ($analysis -like 'You do not need to defragment this volume.')
    {
    $props =@{‘Drive Letter’=$letter
              'Defrag Recommended?'='No'}
    }


  $obj = New-Object -TypeName PSObject -Property $props

  Write-Output $obj

  }
  }

How do I capture the output I need?

Thanks in advance.

3

3 Answers

0
votes

In PowerShell 3.0 and onward, you can use the stream redirection operator > to capture the Verbose ouput to a variable:

# Merge stream 4 (Verbose) into standard Output stream
$analysis = &{Optimize-Volume -DriveLetter $letter -Analyze -Verbose} 4>&1
# Check the "Message" property of the very last VerboseRecord in the output
if($analysis[-1].Message -like "*It is recommended*")
{
    # defrag
}
else 
{
    # don't defrag
}
0
votes

If we Get-Help Optimize-Volume -full we'll see the cmdlet has no output.

Some searching lead me to this Microsoft Scripting Guys article that pointed out using the following to check if Defrag is needed.

(gwmi -Class win32_volume -Filter "DriveLetter = 'C:'").DefragAnalysis()

Knowing this, we can easily make an IF Statement.

$DefragCheck = (gwmi -Class win32_volume -Filter "DriveLetter = 'C:'").DefragAnalysis() |
Select DefragRecommended

IF($DefragCheck){"Defrag recommended"}ELSE{"Defrag is not needed."}

It's helpful to pipe cmdlets to Get-Member in order to see if there are any options available. In the above example, we can pipe gwmi -Class win32_volume -Filter "DriveLetter = 'C:'" to Get-Member and find the DefragAnalysis method, which we use dotted notation to access (wrap the Get-WmiObject in () then use a . and the method name followed by (), it looks confusing until you try it a couple times!)

0
votes

Thanks, I went for the verbose redirection option and it seems to be working well. My method is not the cleanest way of doing it I understand, but it works for me.

I like the second option also, I'm going to look at using this once the script is complete and functionality is proofed.

Thanks for your help once again.