Write output after script results

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Caki Caki 2 months, 4 weeks ago.

  • Author
    Posts
  • #69523
    Profile photo of Caki
    Caki
    Participant

    Hello everybody,
    I hope you are doing well.

    So I am reading and learning from the "Learn Powershell in a month of lunches" book.
    I am currently on Topic #22 and I am improving my scripts, but here is where I got stuck.

    I got this script:

    param(
    [Parameter(Mandatory=$True,HelpMessage = "Enter a computer name to query !")]
    [Alias("Hostname")]
    [string]$ComputerName,
    [ValidateSet(2,3)]
    [int]$DriveType = 3
    )
    
    Write-Verbose "Connecting to $ComputerName"
    Write-Verbose "Looking for drive type $DriveType"
    Get-WmiObject -Class win32_logicaldisk -ComputerName $ComputerName -Filter "drivetype=$DriveType" |
    Sort-Object -Property DeviceID |
    Select-Object -Property DeviceID,
    @{name="Free (GB)"; expression = {$_.Freespace / 1GB -as [int]}},
    @{name="Size (GB)"; expression = {$_.Size / 1GB -as [int]}},
    @{name="Free (GB) %"; expression = {$_.FreeSpace / $_.Size * 100 -as [int]}}
    
    Write-Verbose "Done!"
    

    Everything is running fine except the last Write-Verbose command.
    I want that message to display after my results, not before them.

    VERBOSE: Connecting to localhost
    VERBOSE: Looking for drive type 3
    
    VERBOSE: Done!
    DeviceID Free (GB) Size (GB) Free (GB) %
    -------- --------- --------- -----------
    C:             118       238          50
    D:             664       931          71
    

    I have tried to find some answers online, but without success.

    On this script it works as expected:

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True, HelpMessage = "Enter a computer name to query !")]
    [Alias("HostName")]
    $ComputerName
    )
    
    Write-Verbose "Connecting to $ComputerName"
    
    Get-WmiObject -class win32_NetworkAdapter -ComputerName $ComputerName |
    Where-Object {$_.PhysicalAdapter -eq $True} |
    Select-Object -Property MACAddress, AdapterType, DeviceID, Name, Speed
    
    Write-Verbose "Script execution finished successfully !"
    

    Anybody has an idea how to do it ?

    Thank you !

  • #69525
    Profile photo of Mark Prior
    Mark Prior
    Participant

    change write-verbose for write-output, there is always "write-host" but thats a dirty word here 🙂

    
    param(
    [Parameter(Mandatory=$True,HelpMessage = "Enter a computer name to query !")]
    [Alias("Hostname")]
    [string]$ComputerName,
    [ValidateSet(2,3)]
    [int]$DriveType = 3
    )
    
    write-output "Connecting to $ComputerName"
    write-output "Looking for drive type $DriveType"
    Get-WmiObject -Class win32_logicaldisk -ComputerName $ComputerName -Filter "drivetype=$DriveType" |
    Sort-Object -Property DeviceID |
    Select-Object -Property DeviceID,
    @{name="Free (GB)"; expression = {$_.Freespace / 1GB -as [int]}},
    @{name="Size (GB)"; expression = {$_.Size / 1GB -as [int]}},
    @{name="Free (GB) %"; expression = {$_.FreeSpace / $_.Size * 100 -as [int]}}
    
    write-output "Done!"
    
    
    • #69540
      Profile photo of Caki
      Caki
      Participant

      Hello Mark,

      funny thing is Write-Output works fine and the message is shown under the results.
      But that still doesn't solve my problem and doesn't answer my questions.
      1)Why is Write-Output working ?
      2)Why is Write-Verbose working on the second script?

      Plus I need Write-Verbose, not Write-Output.
      I have noticed that when I delete the Select-Object command the Write-Verbose moves below the results as it should.

      Really strange..

      I would appreciate some help on this one.

  • #69543
    Profile photo of Mark Prior
    Mark Prior
    Participant

    try

     $VerbosePreference 

    do you get "silentlycontinue" ?

    if yes try

     $VerbosePreference = "continue"

    and rerun script

    • #69553
      Profile photo of Caki
      Caki
      Participant

      Same thing..

  • #69577
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    Try this:

    param(
    [Parameter(Mandatory=$True,HelpMessage = "Enter a computer name to query !")]
    [Alias("Hostname")]
    [string]$ComputerName,
    [ValidateSet(2,3)]
    [int]$DriveType = 3
    )
    Get-WmiObject -Class win32_logicaldisk -ComputerName $ComputerName -Filter "drivetype=$DriveType" |
    Sort-Object -Property DeviceID |
    Select-Object -Property DeviceID,
    @{name="Free (GB)"; expression = {$_.Freespace / 1GB -as [int]}},
    @{name="Size (GB)"; expression = {$_.Size / 1GB -as [int]}},
    @{name="Free (GB) %"; expression = {$_.FreeSpace / $_.Size * 100 -as [int]}}
    
    Write-Output ""
    Write-Output "Connecting to $ComputerName"
    Write-Output "Looking for drive type $DriveType"
    Write-Output "Done!"
    

    DeviceID Free (GB) Size (GB) Free (GB) %
    ——– ——— ——— ———–
    C: 7 119 6

    Connecting to NYXXXXXX
    Looking for drive type 3
    Done!

  • #69628
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    To each there own, but you should stick to Write-Verbose. The point of Verbose is that you can use it to troubleshoot your functions as they execute and it can be turned on\off with a switch. With Verbose, you can watch your code execute in real-time. The goal isn't to log what a function did, it to see what the function is doing as it executes. Can you explain your requirements on why you want it to work that way? If you explain the goal, there is probably a better way to do what you require.

    • #69642
      Profile photo of Caki
      Caki
      Participant

      Yes, as you said Write-Output is not working for me because I want to use Write-Verbose and troubleshoot my scripts when I want it.
      There is no specific requirement on this one, this is an exercise in the book I mentioned earlier, but it doesn't work for me.
      The next script I posted was a Lab exercise from the book and Write-Verbose after the code worked without any issue.

      This is mostly a research as I want to know why it is happening and how to prevent it on another scripts (if it occurs).
      Also now the Write-Verbose is at the end. If I add some commands after the Write-Verbose, still the position is incorrect. Now Write-Verbose is in the middle of the script and seeing it execute in the correct place would be really important if I use this script daily.

      Thank you everyone for your help and ideas 🙂

  • #69634
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    Rob is right. You should not use Write-Output.

You must be logged in to reply to this topic.