Private/AdapterUtil.ps1
function Set-AdapterParameter([string] $Name, [string] $Value, $Parameters, $ParametersCollection, $ParametersTypeCollection, $CollectionName) { if (!($ParametersCollection.ContainsKey($Name))) { Write-Warning "Property $Name is not a valid property. Operation canceled." Write-ValidParameters $CollectionName $ParametersCollection return } $propertyName = $ParametersCollection.Get_Item($Name) if ($propertyName -eq $null) { Write-Warning "Property name for $Name cannot be empty" return } $propertyType = $ParametersTypeCollection.Get_Item($Name) if ($propertyType -eq "") { Write-Warning "Property type is missing in module. Continuing with type string." $propertyType = "string" } $validType = Test-ParameterType $propertyType $Value if (!$validType) { Write-Warning "Type of value $Value for property $Name is not valid. Operation canceled." return } $typedValue = Get-TypedValue $Value $propertyType Write-Verbose "Adapter property corresponding to $Name is $propertyName" Write-Verbose "Setting $propertyName to $Value..." if (($Parameters | Get-Member -name $propertyName) -eq $null) { $Parameters | Add-Member NoteProperty -Name $propertyName -Value $typedValue } else { $Parameters.$propertyName = $typedValue } } function Get-AdapterParameters([string] $Tenant, $StorePath) { if (Test-Path $StorePath) { Write-Verbose "Reading configuration in $StorePath" $json = Get-Content $StorePath $configuration = $json | ConvertFrom-Json return $configuration } else { Write-Verbose "No configuration in $storePath" return $null } } function Write-ValidParameters($CollectionName, $Collection) { Write-Section "List of valid $CollectionName parameters" $sorted = $Collection.GetEnumerator() | Sort-Object -Property Name foreach($pair in $sorted) { Write-Output $pair.Name } } function Is-Numeric ($Value) { return $Value -match "^[\d\.]+$" } function Test-ParameterType($PropertyType, $Value) { if ($PropertyType -eq "int") { $result = Is-Numeric $Value if (!$result) { Write-Warning "Value should be numeric" } return $result } if ($PropertyType -eq "bool") { if ($Value -ne "true" -and $Value -ne "false") { Write-Warning "Value should be boolean: true or false" return $false } } return $true } function Get-TypedValue($Value, $PropertyType) { if ($PropertyType -eq "int") { return [int]($Value) } if ($PropertyType -eq "bool") { if ($Value -eq "false") { return $false } else { return $true } } return $Value } function Get-ConfigurationPath($Tenant, $File) { $storePath = Get-AdapterConfigPathFromTenant $Tenant if (!(Test-Path $storePath)) { md $storePath | Out-Null } $storePath = $storePath + $File return $storePath } |