Functions/Common/ShouldProcess.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
function Get-ShouldProcessTargetWmi
{
    <#
        .SYNOPSIS
            Creates the target string for a should process call of Wmi functions.
        
        .DESCRIPTION
            Creates the target string for a should process call of Wmi functions.
        
        .PARAMETER BoundParameters
            The actual PSBoundParameters object of the calling function should be passed here.
        
        .PARAMETER Target
            Additional piece of targeting string to be appended to the base info.
        
        .EXAMPLE
            Get-ShouldProcessTargetWmi -BoundParameters $PSBoundParameters
    
            Returns a string for use as target in the ShouldProcess calls of Wmi queries.
            The string will contain the actual values used for ComputerName, SqlVersion and SqlInstance:
            "<ComputerName> (<SqlVersion>) \ <SqlInstance>"
    
            e.g.: "Server01 (SqlServer2016) \ ReportServer"
    
        .EXAMPLE
            Get-ShouldProcessTargetWmi -BoundParameters $PSBoundParameters -Target "Foo"
    
            Returns a string for use as target in the ShouldProcess calls of Wmi queries.
            The string will contain the actual values used for ComputerName, SqlVersion and SqlInstance, and also have a custom value appended:
            "<ComputerName> (<SqlVersion>) \ <SqlInstance> : <Target>"
    
            e.g.: "Server01 (SqlServer2016) \ ReportServer : Foo"
    #>

    [CmdletBinding()]
    Param (
        [AllowNull()]
        [object]
        $BoundParameters,
        
        [string]
        $Target
    )
    
    if ($BoundParameters["ComputerName"])
    {
        $Server = $BoundParameters["ComputerName"]
    }
    elseif ([Microsoft.ReportingServicesTools.ConnectionHost]::ComputerName)
    {
        $Server = [Microsoft.ReportingServicesTools.ConnectionHost]::ComputerName
    }
    else
    {
        $Server = $env:COMPUTERNAME
    }
    
    if ($BoundParameters["ReportServerVersion"])
    {
        $Version = $BoundParameters["ReportServerVersion"]
    }
    else
    {
        $Version = [Microsoft.ReportingServicesTools.ConnectionHost]::Version
    }    
    
    if ($BoundParameters["ReportServerInstance"])
    {
        $Instance = $BoundParameters["ReportServerInstance"]
    }
    else
    {
        $Instance = ([Microsoft.ReportingServicesTools.ConnectionHost]::Instance)
    }
    
    if ($PSBoundParameters.ContainsKey("Target"))
    {
        return "$Server ($Version) \ $Instance : $Target"
    }
    else
    {
        return "$Server ($Version) \ $Instance"
    }
}

function Get-ShouldProcessTargetWeb
{
    <#
        .SYNOPSIS
            Creates the target string for a should process call of Web functions.
        
        .DESCRIPTION
            Creates the target string for a should process call of Web functions.
        
        .PARAMETER BoundParameters
            The actual PSBoundParameters object of the calling function should be passed here.
        
        .PARAMETER Target
            Additional piece of targeting string to be appended to the base info.
        
        .EXAMPLE
            Get-ShouldProcessTargetweb -BoundParameters $PSBoundParameters
    
            Returns a string for use as target in the ShouldProcess calls of Web queries.
            The string will contain the actual value used for ReportServerUri:
            "<ReportServerUri>"
    
        .EXAMPLE
            Get-ShouldProcessTargetweb -BoundParameters $PSBoundParameters -Target "Foo"
    
            Returns a string for use as target in the ShouldProcess calls of Web queries.
            The string will contain the actual value used for ReportServerUri and also have a custom value appended:
            "<ReportServerUri> : <Target>"
    #>

    [CmdletBinding()]
    Param (
        [AllowNull()]
        [object]
        $BoundParameters,
        
        [string]
        $Target
    )
    
    if ($BoundParameters.ContainsKey("ReportServerUri"))
    {
        if ($Target)
        {
            return "$($BoundParameters["ReportServerUri"]) : $Target"
        }
        else
        {
            return $BoundParameters["ReportServerUri"]
        }
    }
    else
    {
        if ($Target)
        {
            return "$([Microsoft.ReportingServicesTools.ConnectionHost]::ReportServerUri) : $Target"
        }
        else
        {
            return [Microsoft.ReportingServicesTools.ConnectionHost]::ReportServerUri
        }
    }
}