Hi all,
so im building some functions in powershell where i need to choose between Windows or SQL authentication, so im trying to put that into my scripts but having a couple of issues.. My current code is :
FUNCTION Get-Test {
[CmdletBinding()]
Param
(
# List of server(s) to run against
[Parameter(Mandatory=$true)]
[string[]]
$Servers
,
# Define SQL Version you want to find
[ValidateSet("SQL","Windows")]
[string]
$Authentication
,
# Query to run against server
[Parameter(Mandatory=$true)]
[string[]]
$Query
,
# if SQL Auth then enter UserName
[Parameter]
[System.Management.Automation.ParameterAttribute]
$UserName
,
# If SQL auth then enter password
[Parameter]
[Security.SecureString]
$Password
)
PROCESS {
foreach ($s in $servers)
{
IF ($Authentication -eq "Windows")
{
$results = invoke-sqlcmd -ServerInstance $s -Query $query
}
ELSE
{
$results = invoke-sqlcmd -ServerInstance $s -Query $query -Username $UserName -Password $Password
}
}
}
}
So When i run this with
Get-Test -Servers $servers -Query $query -Authentication SQL -UserName MyUserName -Password myPassworld
I have the following problems:
1: I get an error trying to use SQL auth
Cannot process argument transformation on parameter 'UserName'. Cannot convert the "MyuserName" value of type "System.String" to type "System.Management.Automation.ParameterAttribute".
At line:1 char:78
2: Id like to hide the password being seen. If seen you can hide text -asSecureString and thought i had the right thing with what i have below: [Security.SecureString] but its not working.
3: is it possible to make the username and password not show up in the parameters options unless you use authentication sql ?
Thanks for any advice!