0

I am trying to run a ps1 file sending one paramter. This script is to unlock account on AD.

My ps1 file is:

Param([string]$user="")
Get-ADUser -Properties * -Filter {mail -like "$user"} |
    Unlock-ADAccount |
    Sync-ADObject -Destination "AZUDCMO01"

And I called it using:

PS C:\Users\fornecedor.bmc01> .\bmc_unlock_ad.ps1 "andreza.perez@grupomoura.com"

No error is returned, but the account is still locked.

Anyone tried this way to unlock accounts?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Barcat
  • 163
  • 16
  • Does `Get-ADUser -Filter {mail -like "$user"}` actually return a result? Because without wildcards the expression `mail -like "$user"` does the same as `mail -eq "$user"`. – Ansgar Wiechers Jan 10 '19 at 00:28

1 Answers1

0

For this you don't want to use the -Properties * parameter at all. All you need is to get an ADUser object with enough properties to be able to send it through the pipeline. Get-ADUser returns more than enough properties for that.

Having said that, You are piping from the Unlock-ADAccount cmdlet to the Sync-ADObject cmdlet, but...
according to the docs, the Unlock-ADAccount cmdlet by default does not return anything. For that part you need to add the parameter -PassThru.

Try this:

Param([string]$user="")

Get-ADUser -Filter {mail -like "$user"} |
    Unlock-ADAccount -PassThru |
    Sync-ADObject -Destination "AZUDCMO01"
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Hi, thank you! I did the first test from cmd calling the ps1 file. Does not work. When I run the command from PS Console, it works. The command is ok, but whe it starts from DOS don't run. – Barcat Jan 10 '19 at 10:32
  • @Barcat That depends on HOW you run the powershell script from a commandbox. If you try that using the syntax in your question, then indeed it will not work. Use `PS C:\Users\fornecedor.bmc01> powershell.exe -File "PATH_TO_THE_SCRIPT" "andreza.perez@grupomoura.com"`. I don't know why you don't simply run it from within PowerShell though.. – Theo Jan 10 '19 at 12:30
  • I would like to run from powershell, but it is a process that start on a thirdy-part-application. This app can run command send parameters to a file, so I use the ps1 file and the command line to call it and pass the username – Barcat Jan 10 '19 at 13:48
  • @Barcat Alrighty then. You should be able to do it using the syntax I used in my comment. – Theo Jan 10 '19 at 13:58