Skip to content

Instantly share code, notes, and snippets.

@alx9r
Created May 7, 2020 16:46
Show Gist options
  • Save alx9r/8de9f7887eb328a97301ac78cbd20d0f to your computer and use it in GitHub Desktop.
Save alx9r/8de9f7887eb328a97301ac78cbd20d0f to your computer and use it in GitHub Desktop.
ODBC DSN bitness, platform, process test (PowerShell/PowerShell#12578)
param
(
[Parameter(Mandatory,Position=1)]
[ValidateSet('CimMethod','Wdac','CimCmdletAdapter')]
$Method,
[Parameter(Mandatory,Position=2)]
[ValidateSet('64-bit','32-bit')]
$Platform
)
function Add-CustomOdbcDsn {
param(
[Parameter(Mandatory, Position=0)]
[ValidateLength(1, 32)]
[ValidateNotNullOrEmpty()]
[string]
$Name,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$DriverName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateSet('32-bit','64-bit')]
[string]
$Platform,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateSet('User','System')]
[string]
$DsnType
)
$className = 'Root/Microsoft/Windows/Wdac/MSFT_OdbcDsnTask'
$methodName = 'Add'
$methodParameters = [System.Collections.Generic.List[Microsoft.PowerShell.Cmdletization.MethodParameter]]$(
[Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'Name'; ParameterType = 'System.String'; Bindings = 'In'; Value = $Name; IsValuePresent = $true}
[Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'DriverName'; ParameterType = 'System.String'; Bindings = 'In'; Value = $DriverName; IsValuePresent = $true}
[Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'Platform'; ParameterType = 'System.String'; Bindings = 'In'; Value = $Platform; IsValuePresent = $true}
[Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'DsnType'; ParameterType = 'System.String'; Bindings = 'In'; Value = $DsnType; IsValuePresent = $true}
)
$cimAdapter = [Microsoft.PowerShell.Cmdletization.Cim.CimCmdletAdapter]::new()
$cimAdapter.Initialize($PSCmdlet, $className, '1.0.0', '1.0.0', [System.Collections.Generic.Dictionary[string,string]]::new())
$cimAdapter.BeginProcessing()
$cimAdapter.ProcessRecord([Microsoft.PowerShell.Cmdletization.MethodInvocationInfo]::new($methodName, $methodParameters, $null))
$cimAdapter.EndProcessing()
}
$arguments = @{
Name = "test_{0}" -f [guid]::NewGuid().Guid.Split('-')[0]
DsnType = "User"
Platform = $Platform
DriverName = "Microsoft Access Driver (*.mdb)"
}
switch ($Method)
{
'CimMethod' {
$m = @{
ClassName = "MSFT_OdbcDsnTask"
Namespace = "ROOT\Microsoft\Windows\Wdac"
MethodName = "Add"
}
Invoke-CimMethod @m -Arguments $arguments
}
'Wdac' {
Import-Module Wdac
Add-OdbcDsn @arguments
}
'CimCmdletAdapter' {
Add-CustomOdbcDsn @arguments
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment