Powershell: add new parameter -source to write-information

Created on 4 May 2020  路  1Comment  路  Source: PowerShell/PowerShell

Hi Guys :)

with the class InformationRecord wee can add source to our log information


PS C:\> $a = [System.Management.Automation.InformationRecord]::new('log me','please')
PS C:\> $a | Select-Object *

MessageData     : log me
Source          : please
TimeGenerated   : 5/4/2020 
Tags            : {}
User            : xxx\xxxx
Computer        : xxx
ProcessId       : xxxx
NativeThreadId  : xxxx
ManagedThreadId : xxx

with this parameter and tags parameter we can filter easily by source or/and by category _(tags)_...for example

$infoRecord | Where { $_.tags -contains 'power' -and $_.source -eq 'shell' }

Area-Cmdlets-Utility Issue-Question

Most helpful comment

Since the information is somewhat hidden in the issue's title, let me spell out what you're proposing:

You want to add a new, [string]-typed -Source parameter to Write-Information that allows setting the Source property of the resulting System.Management.Automation.InformationRecord instance _explicitly_.

Currently, Write-Information _implicitly_ sets this property, namely to the full path of the enclosing script (*.ps1 or *.psm1). (If you call Write-Information directly from the command prompt, Source is set to 'Write-Information').

>All comments

Since the information is somewhat hidden in the issue's title, let me spell out what you're proposing:

You want to add a new, [string]-typed -Source parameter to Write-Information that allows setting the Source property of the resulting System.Management.Automation.InformationRecord instance _explicitly_.

Currently, Write-Information _implicitly_ sets this property, namely to the full path of the enclosing script (*.ps1 or *.psm1). (If you call Write-Information directly from the command prompt, Source is set to 'Write-Information').

Was this page helpful?
0 / 5 - 0 ratings