none
Powershell script prompts for values already stated in script RRS feed

  • Question

  • Hi

    I have a script to email users when passwords are about to expire. I have entered the values for the variables in the script but when I run the script it still prompts me to enter the values. Some the script -

    param(
        # $smtpServer Enter Your SMTP Server Hostname or IP Address
        [Parameter(Mandatory=$True,Position=0)]
        [ValidateNotNull()]
        [string]$smtpServer = "myemailserver.mydomain.com",
        # Notify Users if Expiry Less than X Days
        [Parameter(Mandatory=$True,Position=1)]
        [ValidateNotNull()]
        [int]$expireInDays = 10,
        # From Address, eg "IT Support <support@domain.com>"
        [Parameter(Mandatory=$True,Position=2)]
        [ValidateNotNull()]
        [string]$from="it@mydomain.com",

    So for $smtpServer, $expireInDays and $from it asks for values. Why is it not reading the values stated?

    Many thanks

    • Moved by Bill_Stewart Friday, July 27, 2018 7:46 PM Abandoned
    Tuesday, May 8, 2018 2:36 PM

All replies

  • You cannot both use "Mandatory" and set a default.

    Prompting is not really intended to be a way of setting parameters but is useful when you always want to prompt.


    \_(ツ)_/

    Tuesday, May 8, 2018 2:44 PM
  • param(
        [Parameter(Mandatory=$false,Position=0)][string]$smtpServer = "myemailserver.mydomain.com", #optional
        # Notify Users if Expiry Less than X Days
        [Parameter(Mandatory=$false,Position=1)] [int]$expireInDays = 10,
        # From Address, eg "IT Support <support@domain.com>"
        [Parameter(Mandatory=$false,Position=2)] [string]$from="it@mydomain.com"

    )

    If you are going to use "OPTIONAL" parameters you can't defined the "mandatory=$true" because this forces to enter the parameter value every single time you run it.

    Also if you use the [ValidateNotNull()] you are asking to validate the script if it's not provided at runtime.

    so it will prompt you for the values also.


    Wednesday, May 9, 2018 4:57 AM