Public/New-IntuneWin32AppDetectionRuleFile.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
function New-IntuneWin32AppDetectionRuleFile {
    <#
    .SYNOPSIS
        Create a new file based detection rule object to be used for the Add-IntuneWin32App function.
 
    .DESCRIPTION
        Create a new file based detection rule object to be used for the Add-IntuneWin32App function.
 
    .PARAMETER Existence
        Define that the detection rule will be existence based, e.g. if a file or folder exists or does not exist.
 
    .PARAMETER DateModified
        Define that the detection rule will be based on a file or folders date modified value.
 
    .PARAMETER DateCreated
        Define that the detection rule will be based on when a file or folder was created.
 
    .PARAMETER Version
        Define that the detection rule will be based on the file version number specified as value.
 
    .PARAMETER Size
        Define that the detection rule will be based on the file size in MB specified as 0 or a positive integer value.
 
    .PARAMETER Path
        Specify a path that will be combined with what's passed for the FileOrFolder parameter, e.g. C:\Windows\Temp.
 
    .PARAMETER FileOrFolder
        Specify a file or folder name that will be combined with what's passed for the Path parameter, e.g. File.exe.
 
    .PARAMETER Check32BitOn64System
        Decide whether environment variables should be expanded in 32-bit context on 64-bit environments.
 
    .PARAMETER DetectionType
        Specify the detection type of an file or folder, if it either exists or doesn't exist.
 
    .PARAMETER Operator
        Specify the operator. Supported values are: equal, notEqual, greaterThanOrEqual, greaterThan, lessThanOrEqual or lessThan.
 
    .PARAMETER DateTimeValue
        Specify a datetime object as the value.
 
    .PARAMETER VersionValue
        Specify a string version object as the value, e.g. 1.0, 1.0.0 or 1.0.0.0 as input.
 
    .PARAMETER SizeInMBValue
        Specify the file size in MB as a positive integer or 0.
 
    .NOTES
        Author: Nickolaj Andersen
        Contact: @NickolajA
        Created: 2020-09-17
        Updated: 2020-09-17
 
        Version history:
        1.0.0 - (2020-09-17) Function created
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [parameter(Mandatory = $true, ParameterSetName = "Existence", HelpMessage = "Define that the detection rule will be existence based, e.g. if a file or folder exists or does not exist.")]
        [switch]$Existence,

        [parameter(Mandatory = $true, ParameterSetName = "DateModified", HelpMessage = "Define that the detection rule will be based on a file or folders date modified value.")]
        [switch]$DateModified,

        [parameter(Mandatory = $true, ParameterSetName = "DateCreated", HelpMessage = "Define that the detection rule will be based on when a file or folder was created.")]
        [switch]$DateCreated,

        [parameter(Mandatory = $true, ParameterSetName = "Version", HelpMessage = "Define that the detection rule will be based on the file version number specified as value.")]
        [switch]$Version,

        [parameter(Mandatory = $true, ParameterSetName = "Size", HelpMessage = "Define that the detection rule will be based on the file size in MB specified as 0 or a positive integer value.")]
        [switch]$Size,
        
        [parameter(Mandatory = $true, ParameterSetName = "Existence", HelpMessage = "Specify a path that will be combined with what's passed for the FileOrFolder parameter, e.g. C:\Windows\Temp.")]
        [parameter(Mandatory = $true, ParameterSetName = "DateModified")]
        [parameter(Mandatory = $true, ParameterSetName = "DateCreated")]
        [parameter(Mandatory = $true, ParameterSetName = "Version")]
        [parameter(Mandatory = $true, ParameterSetName = "Size")]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

        [parameter(Mandatory = $true, ParameterSetName = "Existence", HelpMessage = "Specify a file or folder name that will be combined with what's passed for the Path parameter, e.g. File.exe.")]
        [parameter(Mandatory = $true, ParameterSetName = "DateModified")]
        [parameter(Mandatory = $true, ParameterSetName = "DateCreated")]
        [parameter(Mandatory = $true, ParameterSetName = "Version")]
        [parameter(Mandatory = $true, ParameterSetName = "Size")]
        [ValidateNotNullOrEmpty()]
        [string]$FileOrFolder,

        [parameter(Mandatory = $false, ParameterSetName = "Existence", HelpMessage = "Decide whether environment variables should be expanded in 32-bit context on 64-bit environments.")]
        [parameter(Mandatory = $false, ParameterSetName = "DateModified")]
        [parameter(Mandatory = $false, ParameterSetName = "DateCreated")]
        [parameter(Mandatory = $false, ParameterSetName = "Version")]
        [parameter(Mandatory = $false, ParameterSetName = "Size")]
        [ValidateNotNullOrEmpty()]
        [bool]$Check32BitOn64System = $false,

        [parameter(Mandatory = $true, ParameterSetName = "Existence", HelpMessage = "Specify the detection type of an file or folder, if it either exists or doesn't exist.")]
        [ValidateSet("exists", "doesNotExist")]
        [ValidateNotNullOrEmpty()]
        [string]$DetectionType,

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

        [parameter(Mandatory = $true, ParameterSetName = "DateModified", HelpMessage = "Specify a datetime object as the value.")]
        [parameter(Mandatory = $true, ParameterSetName = "DateCreated")]
        [ValidateNotNullOrEmpty()]
        [datetime]$DateTimeValue,

        [parameter(Mandatory = $true, ParameterSetName = "Version", HelpMessage = "Specify a string version object as the value, e.g. 1.0, 1.0.0 or 1.0.0.0 as input.")]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^(\d+(\.\d+){0,3})$")]
        [string]$VersionValue,

        [parameter(Mandatory = $true, ParameterSetName = "Size", HelpMessage = "Specify the file size in MB as a positive integer or 0.")]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^\d+$")]
        [string]$SizeInMBValue
    )
    Process {
        # Handle initial value for return
        $DetectionRuleFile = $null

        switch ($PSCmdlet.ParameterSetName) {
            "Existence" {
                # Construct ordered hash-table with least amount of required properties for default detection rule
                $DetectionRuleFile = [ordered]@{
                    "@odata.type" = "#microsoft.graph.win32LobAppFileSystemDetection"
                    "operator" = "notConfigured"
                    "detectionValue" = $null
                    "path" = $Path
                    "fileOrFolderName" = $FileOrFolder
                    "check32BitOn64System" = $Check32BitOn64System
                    "detectionType" = $DetectionType
                }
            }
            "DateModified" {
                # 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 detection rule
                $DetectionRuleFile = [ordered]@{
                    "@odata.type" = "#microsoft.graph.win32LobAppFileSystemDetection"
                    "operator" = $Operator
                    "detectionValue" = $DateValueString
                    "path" = $Path
                    "fileOrFolderName" = $FileOrFolder
                    "check32BitOn64System" = $Check32BitOn64System
                    "detectionType" = "modifiedDate"
                }
            }
            "DateCreated" {
                # 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 detection rule
                $DetectionRuleFile = [ordered]@{
                    "@odata.type" = "#microsoft.graph.win32LobAppFileSystemDetection"
                    "operator" = $Operator
                    "detectionValue" = $DateValueString
                    "path" = $Path
                    "fileOrFolderName" = $FileOrFolder
                    "check32BitOn64System" = $Check32BitOn64System
                    "detectionType" = "createdDate"
                }
            }
            "Version" {
                # Construct ordered hash-table with least amount of required properties for default detection rule
                $DetectionRuleFile = [ordered]@{
                    "@odata.type" = "#microsoft.graph.win32LobAppFileSystemDetection"
                    "operator" = $Operator
                    "detectionValue" = $VersionValue
                    "path" = $Path
                    "fileOrFolderName" = $FileOrFolder
                    "check32BitOn64System" = $Check32BitOn64System
                    "detectionType" = "version"
                }
            }
            "Size" {
                # Construct ordered hash-table with least amount of required properties for default detection rule
                $DetectionRuleFile = [ordered]@{
                    "@odata.type" = "#microsoft.graph.win32LobAppFileSystemDetection"
                    "operator" = $Operator
                    "detectionValue" = $SizeInMBValue
                    "path" = $Path
                    "fileOrFolderName" = $FileOrFolder
                    "check32BitOn64System" = $Check32BitOn64System
                    "detectionType" = "sizeInMB"
                }
            }
        }

        # Handle return value with constructed detection rule for file
        return $DetectionRuleFile
    }
}