暂停Powershell命令

时间:2014-02-21 21:10:46

标签: powershell

我写了两个简单的命令,我试图将它们与Start-sleep命令结合在一起

命令1

Enable-CsUser –Identity "user" –RegistrarPool  pool01.com  –SipAddressType EmailAddress

命令2

get-csuser "user"  | FL DisplayName,sipaddress | Out-file D:\lync_creation.txt -append

现在每个命令都可以自己完美运行,但是当我添加start-sleep -s 5

Enable-CsUser –Identity "user" –RegistrarPool  pool01.west.com  –SipAddressType EmailAddress; Start-Sleep -S 5 | get-csuser "user"  | FL DisplayName,sipaddress | Out-file D:\lync_creation.txt -append

当我运行此操作时,我收到以下错误

  

Out-File:无法验证参数'Encoding'的参数。参数“Start-Sleep”不属于ValidateSet属性指定的集合“unicode,utf7,utf8,utf32,ascii,bigendianunicode,default,oem”。提供集合中的参数,然后再次尝试该命令   在线:1字符:179   + Enable-CsUser -Identity“kmarlowe”-RegistrarPool pool01.west.com -SipAddressType EmailAddress;开始睡眠-S 5 | get-csuser“kmarlowe”| FL DisplayName,sipaddress | Out-file<<<< D:\ lync_creation.txt -append Start-Sleep -s 5       + CategoryInfo:InvalidData :( :) [Out-File],ParameterBindingValidationException       + FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.PowerShell.Commands.OutFileCommand

2 个答案:

答案 0 :(得分:3)

问题原因

您正在Start-Sleep加注Get-CSUser,这将无效。

更正

这就是您的代码应该如何:

Enable-CsUser -Ident "user" -RegistrarP pool01.com -SipAddr EmailAddress;
Sleep 5;
Get-CSUser "user" | FL DisplayName,sipaddress | Out-file D:\lync_creation.txt -a;

进一步的建议(Powershell Jobs)

您应该了解Powershell 职位

见这个例子:

$Script = { 
    Enable-CsUser `
        -Ident "user" `
        -RegistrarP pool01.com `
        -SipAddr EmailAddress
}

$Job = Start-Job -ScriptBlock $script
Wait-Job -Id $Job.Id

Get-csuser "user" | FL DisplayName,sipaddress | Out-file D:\lync_creation.txt -a

这将等到作业完成,而不是随机的秒数。

答案 1 :(得分:0)

怎么样:

nable-CsUser –Identity "user" –RegistrarPool  pool01.west.com  –SipAddressType EmailAddress; Start-Sleep -S 5; get-csuser "user"  | FL DisplayName,sipaddress | Out-file D:\lync_creation.txt -append