|
| 1 | + |
| 2 | +<# |
| 3 | +.Synopsis |
| 4 | + Returns the status of Unity installs and the set of their overlapping components. |
| 5 | +.Parameter Versions |
| 6 | + The versions of Unity to test for. |
| 7 | +#> |
| 8 | +function Get-TargetResource |
| 9 | +{ |
| 10 | + [CmdletBinding()] |
| 11 | + [OutputType([System.Collections.Hashtable])] |
| 12 | + param |
| 13 | + ( |
| 14 | + [parameter(Mandatory = $true)] |
| 15 | + [System.String] |
| 16 | + $Versions |
| 17 | + ) |
| 18 | + Write-Verbose "Begin executing Get on $Versions" |
| 19 | + |
| 20 | + [string[]]$splitVersions = $Versions -split ',' | ForEach-Object { $_.Trim() } |
| 21 | + |
| 22 | + $setupInstances = Get-UnitySetupInstance | Where-Object { $splitVersions -contains $_.Version } |
| 23 | + $result = @{ |
| 24 | + "Versions" = $setupInstances | Select-Object -ExpandProperty Version | Sort-Object -Unique |
| 25 | + "Ensure" = if($setupInstances.Count -gt 0) { 'Present'} else { 'Absent' } |
| 26 | + } |
| 27 | + |
| 28 | + Write-Verbose "Found versions: $($result['Versions'])" |
| 29 | + |
| 30 | + if( $setupInstances.Count -gt 0 ) |
| 31 | + { |
| 32 | + $components = $setupInstances[0].Components; |
| 33 | + for( $i = 1; $i -lt $setupInstances.Count; $i++) |
| 34 | + { |
| 35 | + $components = $components -band $setupInstances[$i].Components; |
| 36 | + } |
| 37 | + |
| 38 | + $result["Components"] = $components |
| 39 | + } |
| 40 | + |
| 41 | + $result |
| 42 | + |
| 43 | + Write-Verbose "Found overlapping components: $($result['Components'])" |
| 44 | + Write-Verbose "End executing Get on $Versions" |
| 45 | +} |
| 46 | + |
| 47 | +<# |
| 48 | +.Synopsis |
| 49 | + Installs or uninstalls the specified Versions of Unity and corresponding Components. |
| 50 | +.Parameter Versions |
| 51 | + What versions are we concered with? |
| 52 | +.Parameter Ensure |
| 53 | + Should we ensure they're there or ensure they're not? |
| 54 | +.Parameter Components |
| 55 | + What components are we concerned with? |
| 56 | +.Notes |
| 57 | + Only uninstalls whole versions of Unity. Ensuring components doesn't |
| 58 | + mean other components weren't previously installed and aren't still available. |
| 59 | +#> |
| 60 | +function Set-TargetResource |
| 61 | +{ |
| 62 | + [CmdletBinding()] |
| 63 | + param |
| 64 | + ( |
| 65 | + [parameter(Mandatory = $true)] |
| 66 | + [System.String] |
| 67 | + $Versions, |
| 68 | + |
| 69 | + [ValidateSet("Present","Absent")] |
| 70 | + [System.String] |
| 71 | + $Ensure = 'Present', |
| 72 | + |
| 73 | + [System.String[]] |
| 74 | + $Components = @('All') |
| 75 | + ) |
| 76 | + |
| 77 | + Write-Verbose "Begin executing Set to Ensure $Versions with $Components are $Ensure" |
| 78 | + |
| 79 | + [string[]]$splitVersions = $Versions -split ',' | ForEach-Object { $_.Trim() } |
| 80 | + |
| 81 | + switch($Ensure) { |
| 82 | + 'Present' { |
| 83 | + foreach($version in $splitVersions) |
| 84 | + { |
| 85 | + $findArgs = @{ |
| 86 | + 'Version' = $version |
| 87 | + 'Components' = New-UnitySetupComponent -Components $Components |
| 88 | + } |
| 89 | + |
| 90 | + $installArgs = @{ 'Cache' = "$env:TEMP\.unitysetup" } |
| 91 | + |
| 92 | + $setupInstances = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version |
| 93 | + if($setupInstances.Count -gt 0) { |
| 94 | + $findArgs["Components"] = ($findArgs.Components -band (-bnot ($setupInstances[0].Components -band $findArgs.Components))) |
| 95 | + $installArgs["Destination"] = $setupInstances[0].Path |
| 96 | + } |
| 97 | + |
| 98 | + # No missing components for this version |
| 99 | + if( $findArgs.Components -eq 0 ) { |
| 100 | + Write-Verbose "All components of $version were installed" |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + Write-Verbose "Finding $($findArgs["Components"]) installers for $version" |
| 105 | + $installArgs["Installers"] = Find-UnitySetupInstaller @findArgs -WarningAction Stop |
| 106 | + if( $installArgs.Installers.Count -gt 0 ) { |
| 107 | + Write-Verbose "Starting install of $($installArgs.Installers.Count) components for $version" |
| 108 | + Install-UnitySetupInstance @installArgs |
| 109 | + Write-Verbose "Finished install of $($installArgs.Installers.Count) components for $version" |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + 'Absent' { |
| 114 | + $setupInstances = Get-UnitySetupInstance | Where-Object { $splitVersions -contains $_.Version } |
| 115 | + Write-Verbose "Found $($setupInstances.Count) instance(s) of $splitVersions" |
| 116 | + |
| 117 | + if( $setupInstances.Count -gt 0 ) { |
| 118 | + Write-Verbose "Starting uninstall of $($setupInstances.Count) versions of Unity" |
| 119 | + Uninstall-UnitySetupInstance -Instances $setupInstances |
| 120 | + Write-Verbose "Finished uninstall of $($setupInstances.Count) versions of Unity" |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + Write-Verbose "End executing Set to Ensure $Versions with $Components are $Ensure" |
| 126 | +} |
| 127 | + |
| 128 | +<# |
| 129 | +.Synopsis |
| 130 | + Test if the Unity installs are in the desired state. |
| 131 | +.Parameter Versions |
| 132 | + What versions are we concered with? |
| 133 | +.Parameter Ensure |
| 134 | + Should we ensure they're there or ensure they're not? |
| 135 | +.Parameter Components |
| 136 | + What components are we concerned with? |
| 137 | +.Notes |
| 138 | + This test is not strict. Versions and Components not described are not considered. |
| 139 | +#> |
| 140 | +function Test-TargetResource |
| 141 | +{ |
| 142 | + [CmdletBinding()] |
| 143 | + [OutputType([System.Boolean])] |
| 144 | + param |
| 145 | + ( |
| 146 | + [parameter(Mandatory = $true)] |
| 147 | + [System.String] |
| 148 | + $Versions, |
| 149 | + |
| 150 | + [ValidateSet("Present","Absent")] |
| 151 | + [System.String] |
| 152 | + $Ensure = 'Present', |
| 153 | + |
| 154 | + [System.String[]] |
| 155 | + $Components = @('All') |
| 156 | + ) |
| 157 | + |
| 158 | + Write-Verbose "Begin executing Test to verify $Versions with $Components are $Ensure" |
| 159 | + |
| 160 | + [string[]]$splitVersions = $Versions -split ',' | ForEach-Object { $_.Trim() } |
| 161 | + |
| 162 | + $result = $true |
| 163 | + switch( $Ensure ) |
| 164 | + { |
| 165 | + 'Present' { |
| 166 | + $setupComponents = New-UnitySetupComponent -Components $Components |
| 167 | + foreach($version in $splitVersions) |
| 168 | + { |
| 169 | + Write-Verbose "Starting test for $version" |
| 170 | + $setupInstances = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version |
| 171 | + Write-Verbose "Found $($setupInstances.Count) instance(s) of $version" |
| 172 | + |
| 173 | + if($setupInstances.Count -eq 0) { |
| 174 | + Write-Verbose "Found $version missing." |
| 175 | + $result = $false |
| 176 | + break |
| 177 | + } |
| 178 | + |
| 179 | + $availableComponents = ($setupInstances[0].Components -band $setupComponents) |
| 180 | + if($availableComponents -ne $setupComponents){ |
| 181 | + $missingComponents = New-UnitySetupComponent ($setupComponents -bxor $availableComponents) |
| 182 | + Write-Verbose "Found $version missing $($missingComponents)" |
| 183 | + $result = $false |
| 184 | + break |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + 'Absent' { |
| 189 | + foreach($version in $splitVersions) |
| 190 | + { |
| 191 | + $setupInstances = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version |
| 192 | + Write-Verbose "Found $($setupInstances.Count) instance(s) of $version" |
| 193 | + |
| 194 | + if($setupInstances.Count -gt 0) { |
| 195 | + Write-Verbose "Found $version installed." |
| 196 | + $result = $false |
| 197 | + break |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + $result |
| 204 | + |
| 205 | + Write-Verbose "End executing Test to verify $Versions with $Components are $Ensure" |
| 206 | +} |
| 207 | + |
| 208 | + |
| 209 | +Export-ModuleMember -Function *-TargetResource |
| 210 | + |
0 commit comments