简体   繁体   English

在PowerShell脚本文件上执行和设置参数

[英]Execute and set parameters on a PowerShell script file

I am trying to run a ps1 file sending one paramter. 我正在尝试运行发送一个参数的ps1文件。 This script is to unlock account on AD. 该脚本用于解锁AD上的帐户。

My ps1 file is: 我的ps1文件是:

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? 有人尝试过这种方式来解锁帐户吗?

For this you don't want to use the -Properties * parameter at all. 为此,您根本不需要使用-Properties *参数。 All you need is to get an ADUser object with enough properties to be able to send it through the pipeline. 您需要做的就是获取一个具有足够属性的ADUser对象,以便能够通过管道发送它。 Get-ADUser returns more than enough properties for that. Get-ADUser返回的属性足够多。

Having said that, You are piping from the Unlock-ADAccount cmdlet to the Sync-ADObject cmdlet, but... 话虽如此,您正在从Unlock-ADAccount cmdlet传递到Sync-ADObject cmdlet,但是...
according to the docs , the Unlock-ADAccount cmdlet by default does not return anything. 根据文档 ,默认情况下, Unlock-ADAccount cmdlet不返回任何内容。 For that part you need to add the parameter -PassThru . 为此,您需要添加参数-PassThru

Try this: 尝试这个:

Param([string]$user="")

Get-ADUser -Filter {mail -like "$user"} |
    Unlock-ADAccount -PassThru |
    Sync-ADObject -Destination "AZUDCMO01"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM