Public/New-IntuneWin32AppRequirementRuleScript.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
function New-IntuneWin32AppRequirementRuleScript {
    <#
    .SYNOPSIS
        Create a new script type of Requirement rule object to be used for the Add-IntuneWin32App function.
 
    .DESCRIPTION
        Create a new script type of Requirement rule object to be used for the Add-IntuneWin32App function.
 
    .PARAMETER StringOutputDataType
        Select output data type as a string, used when determining a detection match requirement.
 
    .PARAMETER IntegerOutputDataType
        Select output data type as a integer, used when determining a detection match requirement.
 
    .PARAMETER BooleanOutputDataType
        Select output data type as a boolean, used when determining a detection match requirement.
 
    .PARAMETER DateTimeOutputDataType
        Select output data type as a date time, used when determining a detection match requirement.
 
    .PARAMETER FloatOutputDataType
        Select output data type as a floating point, used when determining a detection match requirement.
 
    .PARAMETER VersionOutputDataType
        Select output data type as a version, used when determining a detection match requirement.
 
    .PARAMETER ScriptFile
        Specify the full path to the PowerShell script file, e.g. 'C:\Scripts\Rule.ps1'.
 
    .PARAMETER ScriptContext
        Specify to either run the script in the local system context or with signed in user context.
 
    .PARAMETER StringComparisonOperator
        Specify the operator. Supported values are: equal, notEqual.
 
    .PARAMETER IntegerComparisonOperator
        Specify the operator. Supported values are: equal, notEqual, greaterThanOrEqual, greaterThan, lessThanOrEqual, lessThan.
 
    .PARAMETER BooleanComparisonOperator
        Specify the operator. Supported values are: equal, notEqual.
 
    .PARAMETER DateTimeComparisonOperator
        Specify the operator. Supported values are: equal, notEqual, greaterThanOrEqual, greaterThan, lessThanOrEqual, lessThan.
     
    .PARAMETER FloatComparisonOperator
        Specify the operator. Supported values are: equal, notEqual, greaterThanOrEqual, greaterThan, lessThanOrEqual, lessThan.
 
    .PARAMETER VersionComparisonOperator
        Specify the operator. Supported values are: equal, notEqual, greaterThanOrEqual, greaterThan, lessThanOrEqual, lessThan.
 
    .PARAMETER StringValue
        Specify the detection match value.
 
    .PARAMETER IntegerValue
        Specify the detection match value.
 
    .PARAMETER BooleanValue
        Specify the detection match value.
 
    .PARAMETER DateTimeValue
        Specify the detection match value.
 
    .PARAMETER FloatValue
        Specify the detection match value.
 
    .PARAMETER VersionValue
        Specify the detection match value.
 
    .PARAMETER RunAs32BitOn64System
        Set as True to run as a 32-bit process in a 64-bit environment.
 
    .PARAMETER EnforceSignatureCheck
        Set as True to verify that the script executed is signed by a trusted publisher.
                 
    .NOTES
        Author: Nickolaj Andersen
        Contact: @NickolajA
        Created: 2020-04-29
        Updated: 2020-04-29
 
        Version history:
        1.0.0 - (2020-04-29) Function created
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [parameter(Mandatory = $true, ParameterSetName = "String", HelpMessage = "Select output data type as a string, used when determining a detection match requirement.")]
        [switch]$StringOutputDataType,

        [parameter(Mandatory = $true, ParameterSetName = "Integer", HelpMessage = "Select output data type as a integer, used when determining a detection match requirement.")]
        [switch]$IntegerOutputDataType,

        [parameter(Mandatory = $true, ParameterSetName = "Boolean", HelpMessage = "Select output data type as a boolean, used when determining a detection match requirement.")]
        [switch]$BooleanOutputDataType,

        [parameter(Mandatory = $true, ParameterSetName = "DateTime", HelpMessage = "Select output data type as a date time, used when determining a detection match requirement.")]
        [switch]$DateTimeOutputDataType,

        [parameter(Mandatory = $true, ParameterSetName = "Float", HelpMessage = "Select output data type as a floating point, used when determining a detection match requirement.")]
        [switch]$FloatOutputDataType,

        [parameter(Mandatory = $true, ParameterSetName = "Version", HelpMessage = "Select output data type as a version, used when determining a detection match requirement.")]
        [switch]$VersionOutputDataType,

        [parameter(Mandatory = $true, ParameterSetName = "String", HelpMessage = "Specify the full path to the PowerShell script file, e.g. 'C:\Scripts\Rule.ps1'.")]
        [parameter(Mandatory = $true, ParameterSetName = "Integer")]
        [parameter(Mandatory = $true, ParameterSetName = "Boolean")]
        [parameter(Mandatory = $true, ParameterSetName = "DateTime")]
        [parameter(Mandatory = $true, ParameterSetName = "Float")]
        [parameter(Mandatory = $true, ParameterSetName = "Version")]
        [ValidateNotNullOrEmpty()]
        [string]$ScriptFile,

        [parameter(Mandatory = $true, ParameterSetName = "String", HelpMessage = "Specify to either run the script in the local system context or with signed in user context.")]
        [parameter(Mandatory = $true, ParameterSetName = "Integer")]
        [parameter(Mandatory = $true, ParameterSetName = "Boolean")]
        [parameter(Mandatory = $true, ParameterSetName = "DateTime")]
        [parameter(Mandatory = $true, ParameterSetName = "Float")]
        [parameter(Mandatory = $true, ParameterSetName = "Version")]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("system", "user")]
        [string]$ScriptContext,
               
        [parameter(Mandatory = $true, ParameterSetName = "String", HelpMessage = "Specify the operator. Supported values are: equal, notEqual.")]
        [ValidateSet("equal", "notEqual")]
        [ValidateNotNullOrEmpty()]
        [string]$StringComparisonOperator,

        [parameter(Mandatory = $true, ParameterSetName = "Integer", HelpMessage = "Specify the operator. Supported values are: equal, notEqual, greaterThanOrEqual, greaterThan, lessThanOrEqual, lessThan.")]
        [ValidateSet("equal", "notEqual", "greaterThanOrEqual", "greaterThan", "lessThanOrEqual", "lessThan")]
        [ValidateNotNullOrEmpty()]
        [string]$IntegerComparisonOperator,

        [parameter(Mandatory = $true, ParameterSetName = "Boolean", HelpMessage = "Specify the operator. Supported values are: equal, notEqual.")]
        [ValidateSet("equal", "notEqual")]
        [ValidateNotNullOrEmpty()]
        [string]$BooleanComparisonOperator,

        [parameter(Mandatory = $true, ParameterSetName = "DateTime", HelpMessage = "Specify the operator. Supported values are: equal, notEqual, greaterThanOrEqual, greaterThan, lessThanOrEqual, lessThan.")]
        [ValidateSet("equal", "notEqual", "greaterThanOrEqual", "greaterThan", "lessThanOrEqual", "lessThan")]
        [ValidateNotNullOrEmpty()]
        [string]$DateTimeComparisonOperator,

        [parameter(Mandatory = $true, ParameterSetName = "Float", HelpMessage = "Specify the operator. Supported values are: equal, notEqual, greaterThanOrEqual, greaterThan, lessThanOrEqual, lessThan.")]
        [ValidateSet("equal", "notEqual", "greaterThanOrEqual", "greaterThan", "lessThanOrEqual", "lessThan")]
        [ValidateNotNullOrEmpty()]
        [string]$FloatComparisonOperator,

        [parameter(Mandatory = $true, ParameterSetName = "Version", HelpMessage = "Specify the operator. Supported values are: equal, notEqual, greaterThanOrEqual, greaterThan, lessThanOrEqual, lessThan.")]
        [ValidateSet("equal", "notEqual", "greaterThanOrEqual", "greaterThan", "lessThanOrEqual", "lessThan")]
        [ValidateNotNullOrEmpty()]
        [string]$VersionComparisonOperator,

        [parameter(Mandatory = $true, ParameterSetName = "String", HelpMessage = "Specify the detection match value.")]
        [ValidateNotNullOrEmpty()]
        [string]$StringValue,

        [parameter(Mandatory = $true, ParameterSetName = "Integer", HelpMessage = "Specify the detection match value.")]
        [ValidateNotNullOrEmpty()]
        [string]$IntegerValue,

        [parameter(Mandatory = $true, ParameterSetName = "Boolean", HelpMessage = "Specify the detection match value.")]
        [ValidateNotNullOrEmpty()]
        [bool]$BooleanValue,

        [parameter(Mandatory = $true, ParameterSetName = "DateTime", HelpMessage = "Specify the detection match value.")]
        [ValidateNotNullOrEmpty()]
        [datetime]$DateTimeValue,

        [parameter(Mandatory = $true, ParameterSetName = "Float", HelpMessage = "Specify the detection match value.")]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("^((\+|-)?(0|([1-9][0-9]*))(\.[0-9]+)?)$")]
        [string]$FloatValue,

        [parameter(Mandatory = $true, ParameterSetName = "Version", HelpMessage = "Specify the detection match value.")]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^(\d+(\.\d+){0,3})$")]
        [string]$VersionValue,

        [parameter(Mandatory = $false, ParameterSetName = "String", HelpMessage = "Set as True to run as a 32-bit process in a 64-bit environment.")]
        [parameter(Mandatory = $false, ParameterSetName = "Integer")]
        [parameter(Mandatory = $false, ParameterSetName = "Boolean")]
        [parameter(Mandatory = $false, ParameterSetName = "DateTime")]
        [parameter(Mandatory = $false, ParameterSetName = "Float")]
        [parameter(Mandatory = $false, ParameterSetName = "Version")]        
        [ValidateNotNullOrEmpty()]
        [bool]$RunAs32BitOn64System = $false,

        [parameter(Mandatory = $false, ParameterSetName = "String", HelpMessage = "Set as True to verify that the script executed is signed by a trusted publisher.")]
        [parameter(Mandatory = $false, ParameterSetName = "Integer")]
        [parameter(Mandatory = $false, ParameterSetName = "Boolean")]
        [parameter(Mandatory = $false, ParameterSetName = "DateTime")]
        [parameter(Mandatory = $false, ParameterSetName = "Float")]
        [parameter(Mandatory = $false, ParameterSetName = "Version")]
        [ValidateNotNullOrEmpty()]
        [bool]$EnforceSignatureCheck = $false
    )
    Process {
        # Handle initial value for return
        $RequirementRuleScript = $null

        # Detect if passed script file exists
        Write-Verbose -Message "Attempting to locate given script file in provided path: $($ScriptFile)"
        if (Test-Path -Path $ScriptFile) {
            # Get script file name from provided path
            $ScriptFileName = [System.IO.Path]::GetFileName("$($ScriptFile)")

            # Convert script file contents to base64 string
            $ScriptContent = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("$($ScriptFile)"))

            switch ($PSCmdlet.ParameterSetName) {
                "String" {
                    # Construct ordered hash-table with least amount of required properties for default requirement rule
                    $RequirementRuleScript = [ordered]@{
                        "@odata.type" = "#microsoft.graph.win32LobAppPowerShellScriptRequirement"
                        "operator" = $StringComparisonOperator
                        "detectionValue" = $StringValue
                        "displayName" = $ScriptFileName
                        "enforceSignatureCheck" = $EnforceSignatureCheck
                        "runAs32Bit" = $RunAs32BitOn64System
                        "runAsAccount" = $ScriptContext
                        "scriptContent" = $ScriptContent
                        "detectionType" = "string"
                    }
                }
                "Integer" {
                    # Construct ordered hash-table with least amount of required properties for default requirement rule
                    $RequirementRuleScript = [ordered]@{
                        "@odata.type" = "#microsoft.graph.win32LobAppPowerShellScriptRequirement"
                        "operator" = $IntegerComparisonOperator
                        "detectionValue" = $IntegerValue
                        "displayName" = $ScriptFileName
                        "enforceSignatureCheck" = $EnforceSignatureCheck
                        "runAs32Bit" = $RunAs32BitOn64System
                        "runAsAccount" = $ScriptContext
                        "scriptContent" = $ScriptContent
                        "detectionType" = "integer"
                    }
                }
                "Boolean" {
                    # Construct ordered hash-table with least amount of required properties for default requirement rule
                    $RequirementRuleScript = [ordered]@{
                        "@odata.type" = "#microsoft.graph.win32LobAppPowerShellScriptRequirement"
                        "operator" = $BooleanComparisonOperator
                        "detectionValue" = $BooleanValue
                        "displayName" = $ScriptFileName
                        "enforceSignatureCheck" = $EnforceSignatureCheck
                        "runAs32Bit" = $RunAs32BitOn64System
                        "runAsAccount" = $ScriptContext
                        "scriptContent" = $ScriptContent
                        "detectionType" = "boolean"
                    }
                }
                "DateTime" {
                    # Convert input datetime object to ISO 8601 string
                    $DateValueString = ConvertTo-JSONDate -InputObject $DateTimeValue
                    
                    # Construct ordered hash-table with least amount of required properties for default requirement rule
                    $RequirementRuleScript = [ordered]@{
                        "@odata.type" = "#microsoft.graph.win32LobAppPowerShellScriptRequirement"
                        "operator" = $DateTimeComparisonOperator
                        "detectionValue" = $DateValueString
                        "displayName" = $ScriptFileName
                        "enforceSignatureCheck" = $EnforceSignatureCheck
                        "runAs32Bit" = $RunAs32BitOn64System
                        "runAsAccount" = $ScriptContext
                        "scriptContent" = $ScriptContent
                        "detectionType" = "dateTime"
                    }
                }
                "Float" {
                    # Construct ordered hash-table with least amount of required properties for default requirement rule
                    $RequirementRuleScript = [ordered]@{
                        "@odata.type" = "#microsoft.graph.win32LobAppPowerShellScriptRequirement"
                        "operator" = $FloatComparisonOperator
                        "detectionValue" = $FloatValue
                        "displayName" = $ScriptFileName
                        "enforceSignatureCheck" = $EnforceSignatureCheck
                        "runAs32Bit" = $RunAs32BitOn64System
                        "runAsAccount" = $ScriptContext
                        "scriptContent" = $ScriptContent
                        "detectionType" = "float"
                    }
                }
                "Version" {
                    # Construct ordered hash-table with least amount of required properties for default requirement rule
                    $RequirementRuleScript = [ordered]@{
                        "@odata.type" = "#microsoft.graph.win32LobAppPowerShellScriptRequirement"
                        "operator" = $IntegerComparisonOperator
                        "detectionValue" = $IntegerValue
                        "displayName" = $ScriptFileName
                        "enforceSignatureCheck" = $EnforceSignatureCheck
                        "runAs32Bit" = $RunAs32BitOn64System
                        "runAsAccount" = $ScriptContext
                        "scriptContent" = $ScriptContent
                        "detectionType" = "version"
                    }
                }
            }
        }
        else {
            Write-Warning -Message "Unable to detect specified script file in given path: $($ScriptFile)"
        }

        # Handle return value with constructed requirement rule for file
        return $RequirementRuleScript
    }
}