Created
June 9, 2024 12:23
-
-
Save adbertram/352f58ca31df1ae8af91065a17e131a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-DatabaseData { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string]$DatabaseType | |
) | |
dynamicparam { | |
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary | |
$attributes = New-Object System.Collections.ObjectModel.Collection[System.Attribute] | |
$attributes.Add((New-Object System.Management.Automation.ParameterAttribute)) | |
if ($DatabaseType -eq 'SQL') { | |
$runtimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('SQLQuery', [string], $attributes) | |
} elseif ($DatabaseType -eq 'Mongo') { | |
$runtimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('MongoQuery', [string], $attributes) | |
} else { | |
throw "Unsupported database type" | |
} | |
$paramDictionary.Add($runtimeParam.Name, $runtimeParam) | |
return $paramDictionary | |
} | |
begin { | |
# Retrieve the dynamic parameter value | |
$dynamicParamValue = $PSBoundParameters[$DatabaseType + 'Query'] | |
} | |
process { | |
if ($DatabaseType -eq 'SQL') { | |
Write-Output "Executing SQL query: $dynamicParamValue" | |
# Simulate SQL query execution | |
} elseif ($DatabaseType -eq 'Mongo') { | |
Write-Output "Executing Mongo query: $dynamicParamValue" | |
# Simulate Mongo query execution | |
} | |
} | |
} | |
# Example usage: | |
# Get-DatabaseData -DatabaseType SQL -SQLQuery "SELECT * FROM Users" | |
# Get-DatabaseData -DatabaseType Mongo -MongoQuery "{ find: 'Users' }" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment