Module/HelperFunctions/ModuleProfileHelper.ps1
function CreateProfileConfig { Param ( [Parameter(ValueFromPipelineByPropertyName, Mandatory = $false)] [string] $userProfilePath, [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)] [string] $azureUserName, [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)] [SecureString] $azurePassword, [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)] [string] $databaseUserName, [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)] [SecureString] $databasePassword ) process { if (-not $userProfilePath) { $userProfilePath = "$env:LOCALAPPDATA\BCSPowershellModule\config.json" } if (-not(Test-Path -Path $userProfilePath)) { Write-Host "Creating new BCSPowershellModule profile" Write-Host "Profile file path: $($userProfilePath)" New-Item -Path $userProfilePath -Force } else { Write-Host "Updating BCSPowershellModule profile" $userProfile = Get-Content $userProfilePath -Raw | ConvertFrom-Json } $azurePasswordPlainText = $azurePassword | ConvertFrom-SecureString $databasePassworddPlainText = $databasePassword | ConvertFrom-SecureString $userProfile = CreateUserProfilePSObject; $userProfile.azureUserName = $azureUserName; $userProfile.azurePassword = $azurePasswordPlainText; $userProfile.databaseUserName = $databaseUserName; $userProfile.databasePassword = $databasePassworddPlainText; $userProfileJson = $userProfile | ConvertTo-Json | Format-Json -Indentation 2 } end { $userProfileJson | out-file $userProfilePath Write-Host "BCSPowershellModule profile was saved to $($userProfilePath)" } } function CreateUserProfilePSObject { $userProfileJson = New-Object PSObject $userProfileJson | Add-Member -type NoteProperty -Name 'azureUserName' -Value "" $userProfileJson | Add-Member -type NoteProperty -Name 'azurePassword' -Value "" $userProfileJson | Add-Member -type NoteProperty -Name 'databaseUserName' -Value "" $userProfileJson | Add-Member -type NoteProperty -Name 'databasePassword' -Value "" $userProfileJson } function LoadProfileConfig { [string] $userProfilePath Get-Content $userProfilePath -Raw | ConvertFrom-Json } |