Need help with Write-Verbose vs Write-Host

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Corey Thomas Corey Thomas 2 years, 6 months ago.

  • Author
    Posts
  • #15451
    Profile photo of Corey Thomas
    Corey Thomas
    Participant

    Hi,

    Like most of you, I'm great with vbscript but trying to force myself into doing things with PS. I was looking at one of Don's functions in the report to html doc and trying a few things to play with it. I took his function for win32_process and tried adding a write-verbose to it (as well as the cmdletbinding).

    The verbose option works fine, but I noticed that the output is different. For example, if I try to output multiple properties from the process object, it gives me the full path to the WMI object, not just the name. If I use write-host, it outputs as expected, just the name of the process. If I use write-verbose and the variable, it's fine too. But once I try to put them in double quotes, output goes wonky again. For example, this works as expected: Write-Verbose $proc.name. This outputs the full path: Write-Verbose "$proc.name $proc.processID $proc.ExecutablePath"

    I know I could probably store the process name to a variable and use that instead, but the point of powershell is to use less code not more. 🙂

    Any tips on how I can get write-verbose to display only the process name, not the full path AND use other variables??

    Running WMF 5.0 May Preview and here is the code:

    $procs = Get-WmiObject -class Win32_Process -ComputerName $ComputerName

    foreach ($proc in $procs) {

    $props = @{'ProcName'=$proc.name;
    'ProcessID' = $proc.ProcessID;
    'Executable'=$proc.ExecutablePath}
    Write-Verbose "$props.procname $proc.ProcessID $proc.ExecutablePath"
    New-Object -TypeName PSObject -Property $props

    } #foreach

    it outputs like this:
    VERBOSE: System.Collections.Hashtable.procname \\W520\root\cimv2:Win32_Process.Handle="8400".ProcessID
    \\W520\root\cimv2:Win32_Process.Handle="8400".ExecutablePath

    what I would like:
    VERBOSE:name tab ID tab exepath

    I also tried using 't for tabs and write-verbose didn't like that at all.

  • #15453
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    You'll have to create subexpressions for them to be evaluated correctly within the Write-Verbose string.

    Write-Verbose "$($props.procname) $($proc.ProcessID) $($proc.ExecutablePath)"

  • #15454
    Profile photo of Corey Thomas
    Corey Thomas
    Participant

    Perfect. That worked! Any idea how to get tabs in the output?

  • #15455
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    Write-Verbose "$($props.procname)t$($proc.ProcessID)t$($proc.ExecutablePath)"

    Keep in mind, it's still not going to look "pretty" as the verbose tabbing is based on 4-space tabs, and the boundary will vary based on the previous fields. Write-Verbose was meant to be strictly utilitarian. It's a great tool when debugging a script. But it was never intended for production-ready, end user output.

  • #15456
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    Sorry that didn't post correctly with the code tags. Essentially you put the tab escape character between the entries (backtick t).

  • #15457
    Profile photo of Corey Thomas
    Corey Thomas
    Participant

    I figured it out. I was using single quote ('t) not the tab escape character (`t)

    Don is absolutely right about using those. It can be tough to see the difference sometimes. Thanks for your help Bob!
    40 hours in for powershell so far. Only 9,960 to go. 🙂

You must be logged in to reply to this topic.