PowerShell – Command Line – Error – “Cannot process argument transformation on parameter – value of type “System.String” to type “System.Management.Automation.ParameterAttribute”

Background

Want to quickly touch on an error that I stumbled upon this morning.

Error

Error Image

Error Text

Cannot process argument transformation on parameter - value of type "System.String" to type "System.Management.Automation.ParameterAttribute".

At line:1 char:1
+ ./powershell.parameter.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [powershell.parameter.ps1], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,powershell.parameter.ps1

 

Code

Erroring Code


[CmdletBinding()]
param ( `
		  [Parameter] `
		  [string] $computer = $env:COMPUTERNAME `
      )
	  
Set-StrictMode -Version latest

Write-Host "Computer name is $computer"


Code Correction

Parameter – Property – Mandatory is False

Outline

  1. For parameter flag
    • Supply property name and value
      • In our case
        • Mandatory is false
        • As we are setting a default value for Computer
        • The default value is $env.COMPUTERNAME

Code


[CmdletBinding()]
param ( `
		  [Parameter(mandatory=$false)] `
		  [string] $computer = $env:COMPUTERNAME `
      )
	  
Set-StrictMode -Version latest

Write-Host "Computer name is $computer"

Source Code

Git

Gist

DanielAdeniji/powershell.parameter.attributesMissing.ps1

Link

 

Summary

Quick Takeaways:-

  1. One can not use the Parameter “Tag” has a placeholder
  2. Once it is in your code, the PowerShell parser expects you to supply the attributes
  3. In our case the following attributes
    • Mandatory
      • datatype:- switch
      • values:-
        • true
        • false

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s