internal/functions/message/Convert-PsfMessageTarget.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function Convert-PsfMessageTarget
{
    <#
  .SYNOPSIS
   Transforms the target input to the message system.
   
  .DESCRIPTION
   Transforms the target input to the message system.
   
   If there is an exception running a transformation scriptblock, it will log the error in the transform error queue and return the original object instead.
   
  .PARAMETER Target
   The input target object, that might have to be transformed (may not either)
   
  .PARAMETER FunctionName
   The function writing the message
   
  .PARAMETER ModuleName
   The module, that the function writing the message is part of
   
  .EXAMPLE
   PS C:\> Convert-PsfMessageTarget -Target $Target -FunctionName 'Get-Test' -ModuleName 'MyModule'
   
   Checks internal storage for definitions that require a target transform, and either returns the original object or the transformed object.
 #>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        $Target,
        
        [Parameter(Mandatory = $true)]
        [string]
        $FunctionName,
        
        [Parameter(Mandatory = $true)]
        [string]
        $ModuleName
    )
    
    if ($null -eq $Target) { return }
    
    $typeName = $Target.GetType().FullName.ToLower()
    
    if ([PSFramework.Message.MessageHost]::TargetTransforms.ContainsKey($typeName))
    {
        $scriptBlock = [PSFramework.Message.MessageHost]::TargetTransforms[$typeName]
        try
        {
            $tempTarget = $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create($scriptBlock.ToString())), $null, $null)
            return $tempTarget
        }
        catch
        {
            [PSFramework.Message.MessageHost]::WriteTransformError($_, $FunctionName, $ModuleName, $Target, "Target", ([System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.InstanceId))
            return $Target
        }
    }
    
    if ($transform = [PSFramework.Message.MessageHost]::TargetTransformlist.Get($typeName, $ModuleName, $FunctionName))
    {
        try
        {
            $tempTarget = $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create($transform.ScriptBlock.ToString())), $null, $null)
            return $tempTarget
        }
        catch
        {
            [PSFramework.Message.MessageHost]::WriteTransformError($_, $FunctionName, $ModuleName, $Target, "Target", ([System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.InstanceId))
            return $Target
        }
    }
    
    return $Target
}