locked
Call function in .ps1 script from powershelgl command line RRS feed

  • Question

  • Hello,

    I have a simple function that resets an AD password. I have to change the username argument in ISE every time I run it.

    How can I run it from the command prompt and pass in the username each time, rather than editing the script each time?

    Thanks!

    function resetpw ($username) {

        $PW = read-host -AsSecureString -Prompt "Enter new password: "
        Get-ADUser $username
        Set-ADAccountPassword -reset -Confirm:$false -Identity $username -NewPassword $PW
    }

    resetpw user.name

    Friday, February 23, 2018 8:40 PM

All replies

  • Remove the function and make it a script with a param block.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Friday, February 23, 2018 8:42 PM
  • Can you give me an example or link to one please? The param block is the piece I'm unfamiliar with.
    Friday, February 23, 2018 8:48 PM
  • Get-Help about_Functions_Advanced_Parameters

    ... works for scripts as well ... 


    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Friday, February 23, 2018 8:51 PM
  • param(
        [Parameter(Position=0, Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $UserName
    )
    $PW = read-host -AsSecureString -Prompt "Enter new password: "
    Get-ADUser $username
    Set-ADAccountPassword -reset -Confirm:$false -Identity $username -NewPassword $PW
    ... something like this

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Friday, February 23, 2018 8:55 PM
  • did it work?

    Saturday, February 24, 2018 3:01 PM