Scheduled task syntax

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of lemonade lemonade 7 months ago.

  • Author
    Posts
  • #60696
    Profile photo of lemonade
    lemonade
    Participant

    I'm trying to set up a scheduled task that runs a powershell command to disable a domain account on a given day.

    I don't understand why this works

    $name = "test user"
    Disable-ADAccount -Identity $name
    

    And this does not?

    $name = "test user"
    powershell -NoProfile -WindowStyle Hidden -command {Disable-ADAccount -Identity $name}
    

    Error: 'Cannot validate argument on parameter 'Identity'. The argument is null.'

  • #60699
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    You're using a script block {} which does not automatically expand outside variables. Try to replace the curly brackets with double-quotes and enclose the variable in single-quotes to ensure the space gets included.

    $name = "test user"
    powershell -NoProfile -WindowStyle Hidden -command "Disable-ADAccount -Identity '$name'"
    

    I hope that helps.

    Daniel

  • #60700
    Profile photo of lemonade
    lemonade
    Participant

    Ah of course. Many thanks

You must be logged in to reply to this topic.