File size: 5,994 Bytes
8c763fb | 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 | ## Copyright (c) Microsoft Corporation. All rights reserved.
## Licensed under the MIT License.
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateSet('get', 'set', 'export')]
[string]$Operation,
[Parameter(ValueFromPipeline)]
[string[]]$UserInput
)
Begin {
enum ProfileType {
AllUsersCurrentHost
AllUsersAllHosts
CurrentUserAllHosts
CurrentUserCurrentHost
}
function New-PwshResource {
param(
[Parameter(Mandatory = $true)]
[ProfileType] $ProfileType,
[Parameter(ParameterSetName = 'WithContent')]
[string] $Content,
[Parameter(ParameterSetName = 'WithContent')]
[bool] $Exist
)
# Create the PSCustomObject with properties
$resource = [PSCustomObject]@{
profileType = $ProfileType
content = $null
profilePath = GetProfilePath -profileType $ProfileType
_exist = $false
}
# Add ToJson method
$resource | Add-Member -MemberType ScriptMethod -Name 'ToJson' -Value {
return ([ordered] @{
profileType = $this.profileType
content = $this.content
profilePath = $this.profilePath
_exist = $this._exist
}) | ConvertTo-Json -Compress -EnumsAsStrings
}
# Constructor logic - if Content and Exist parameters are provided (WithContent parameter set)
if ($PSCmdlet.ParameterSetName -eq 'WithContent') {
$resource.content = $Content
$resource._exist = $Exist
} else {
# Default constructor logic - read from file system
$fileExists = Test-Path $resource.profilePath
if ($fileExists) {
$resource.content = Get-Content -Path $resource.profilePath
} else {
$resource.content = $null
}
$resource._exist = $fileExists
}
return $resource
}
function GetProfilePath {
param (
[ProfileType] $profileType
)
$path = switch ($profileType) {
'AllUsersCurrentHost' { $PROFILE.AllUsersCurrentHost }
'AllUsersAllHosts' { $PROFILE.AllUsersAllHosts }
'CurrentUserAllHosts' { $PROFILE.CurrentUserAllHosts }
'CurrentUserCurrentHost' { $PROFILE.CurrentUserCurrentHost }
}
return $path
}
function ExportOperation {
$allUserCurrentHost = New-PwshResource -ProfileType 'AllUsersCurrentHost'
$allUsersAllHost = New-PwshResource -ProfileType 'AllUsersAllHosts'
$currentUserAllHost = New-PwshResource -ProfileType 'CurrentUserAllHosts'
$currentUserCurrentHost = New-PwshResource -ProfileType 'CurrentUserCurrentHost'
# Cannot use the ToJson() method here as we are adding a note property
$allUserCurrentHost | Add-Member -NotePropertyName '_name' -NotePropertyValue 'AllUsersCurrentHost' -PassThru | ConvertTo-Json -Compress -EnumsAsStrings
$allUsersAllHost | Add-Member -NotePropertyName '_name' -NotePropertyValue 'AllUsersAllHosts' -PassThru | ConvertTo-Json -Compress -EnumsAsStrings
$currentUserAllHost | Add-Member -NotePropertyName '_name' -NotePropertyValue 'CurrentUserAllHosts' -PassThru | ConvertTo-Json -Compress -EnumsAsStrings
$currentUserCurrentHost | Add-Member -NotePropertyName '_name' -NotePropertyValue 'CurrentUserCurrentHost' -PassThru | ConvertTo-Json -Compress -EnumsAsStrings
}
function GetOperation {
param (
[Parameter(Mandatory = $true)]
$InputResource,
[Parameter()]
[switch] $AsJson
)
$profilePath = GetProfilePath -profileType $InputResource.profileType.ToString()
$actualState = New-PwshResource -ProfileType $InputResource.profileType
$actualState.profilePath = $profilePath
$exists = Test-Path $profilePath
if ($InputResource._exist -and $exists) {
$content = Get-Content -Path $profilePath
$actualState.Content = $content
} elseif ($InputResource._exist -and -not $exists) {
$actualState.Content = $null
$actualState._exist = $false
} elseif (-not $InputResource._exist -and $exists) {
$actualState.Content = Get-Content -Path $profilePath
$actualState._exist = $true
} else {
$actualState.Content = $null
$actualState._exist = $false
}
if ($AsJson) {
return $actualState.ToJson()
} else {
return $actualState
}
}
function SetOperation {
param (
$InputResource
)
$actualState = GetOperation -InputResource $InputResource
if ($InputResource._exist) {
if (-not $actualState._exist) {
$null = New-Item -Path $actualState.profilePath -ItemType File -Force
}
if ($null -ne $InputResource.content) {
Set-Content -Path $actualState.profilePath -Value $InputResource.content
}
} elseif ($actualState._exist) {
Remove-Item -Path $actualState.profilePath -Force
}
}
}
End {
$inputJson = $input | ConvertFrom-Json
if ($inputJson) {
$InputResource = New-PwshResource -ProfileType $inputJson.profileType -Content $inputJson.content -Exist $inputJson._exist
}
switch ($Operation) {
'get' {
GetOperation -InputResource $InputResource -AsJson
}
'set' {
SetOperation -InputResource $InputResource
}
'export' {
if ($inputJson) {
Write-Error "Input not supported for export operation"
exit 2
}
ExportOperation
}
}
exit 0
}
|