Error in PS Script while adding parameters

Welcome Forums General PowerShell Q&A Error in PS Script while adding parameters

This topic contains 1 reply, has 2 voices, and was last updated by

 
Moderator
1 month ago.

  • Author
    Posts
  • #124598

    Participant
    Points: 11
    Rank: Member

    Hello,

    Getting started with PS scripting and putting together few scripts that helps in my day to day work. I am receiving the below error when I try and pass few parameters in the below script. The details are as below. What am I doing wrong ?

    Script I am running is below.

    Param (
    [Parameter(Mandatory=$True)]
    [string]$Name,

    [Parameter(Mandatory=$True)]
    [string]$Description)

    New-CMAntimalwarePolicy -Name -Policy ExclusionSettings -Description -Verbose

    It asks me to enter Name and Description just fine but when I hit enter again I get the below error.

    New-CMAntimalwarePolicy : Missing an argument for parameter 'Name'. Specify a parameter of type 'System.String' and
    try again.
    At line:9 char:25
    + New-CMAntimalwarePolicy -Name -Policy ExclusionSettings -Description  ...
    +                         ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-CMAntimalwarePolicy], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.ConfigurationManagement.Cmdlets.EP.Commands.NewAntimalwarePoli
    cyCommand

  • #124616

    Moderator
    Points: 103
    Team MemberHelping Hand
    Rank: Participant

    So, the first problem here is that you're apparently setting parameters, but never using the variables that you create.

    When you are calling a function or script with arguments, you need to specify the value that you pass – so, "-Name" should be "-Name $Name" (using the parameter you created). It doesn't get automatically passed down!

    param(
        [Parameter(Mandatory=$true)]
        [string]$Name,
    
        [Parameter(Mandatory=$true)]
        [string]$Description
    )
    
    New-CMAntimalwarePolicy -Name $Name -Policy 'ExclusionSettings' -Description $Description -Verbose

You must be logged in to reply to this topic.