Author Posts

May 21, 2014 at 5:05 am

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.

May 21, 2014 at 5:24 am

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

May 21, 2014 at 5:33 am

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

May 21, 2014 at 5:40 am

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.

May 21, 2014 at 5:42 am

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

May 21, 2014 at 5:44 am

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