So, i think that I am going crazy and hoping someone can point out my flaw. If you review the function below I am expecting to be able to pass a string value of domain\username to the Credential parameter function and have that convert the string into a PSCredential object similar to MS AD cmdlets. However that’s not the case and I receive the conversion error listed. My expectations are set by this article on TechNet that states the following:
(i)If a parameter accepts a PSCredential object, Windows PowerShell supports several types of input, such as the following:(/i)
- (b)Empty (/b)If you supply no input to a mandatory –credential parameter, Windows PowerShell prompts you for the user name and password.
- (b)String (/b)If you supply a string to the –credential parameter, Windows PowerShell treats it as a user name and prompts you for the password.
- (b)Credential(/b) If you supply a credential object to the –credential parameter, Windows PowerShell accepts it as is.
(b)Keep in mind I haven’t actually finished the cmdlet but it should at least run as written with the correct input and switches.(/b)
Function Set-DNSServers{
[cmdletbinding(SupportsShouldProcess=$true)]
PARAM(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string[]]$Computers,
[Parameter(Mandatory=$true)]
[System.Management.Automation.PSCredential]$Credential
)
foreach($host in $computers){
write-verbose "Gathering network adapters on $host"
# get network adapaters from WMI
$nics = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Computers -Credential $Credential | Select-Object -Property PSComputerName,DNSHostname,Description,IPAddress,IPEnabled,DNSServerSearchOrder
# if verbose is enabled output your findings
if($PSCmdlet.MyInvocation.BoundParameters['Verbose']){
write-verbose "Found the following network adapters on $host"
write-verbose ($nics | FT -AutoSize | Out-String)
} # End IF
} # end For $host/$computers
} # End Function
Set-DNSServers -Computers -Credential -Verbose -WhatIf
Set-DNSServers : Cannot process argument transformation on parameter 'Credential'. Cannot convert the value of type "System.String" to type "System.Management.Automation.PSCredential".
At C:\jbtemp\PS\Scripts\Systems\Set-DNSServers.ps1:24 char:52
+ Set-DNSServers -Computers -Credential -Verbose -WhatIf
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-DNSServers], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-DNSServers