【问题标题】:unexpected token 'if' in expression or statement powershell表达式或语句 powershell 中的意外标记“if”
【发布时间】:2014-11-12 03:22:12
【问题描述】:

我有一个 .CSV 文件,我正在用它填写一些广告信息。以下脚本应该在给定用户 AD 属性上填写“Office”。 CSV 文件中具有不同“Office”名称的 Header 称为“office”。

其他信息:

Exchange 2013 混合服务器

Forrest 功能级别 = 2003(上周刚刚降级旧 dc)

所有 DC 现在都是 2012 R2。任何建议将不胜感激。

这曾经与 QADuser cmdlet 一起使用,所以我只是找到了 cmdlet/参数等效项并更改了它们。

不起作用的新脚本:

Import-CSV d:\scripts\ExchangeExportQuery.csv | ForEach-Object {$myOff = $_.Office if ($_.DistinguishedName -ne $null -and ($_.DistinguishedName.StartsWith("CN=")) ) {Get-User -ResultSize   'unlimited' -identity $_.DistinguishedName | foreach-object { Set-User -identity $_.DistinguishedName -Office $myOff}}}

在带有 QAD cmdlet 的旧 Exchange 2007 服务器上实际运行的旧脚本:

Import-CSV d:\scripts\ExchangeExportQuery.csv | ForEach-Object { 
$myOff = $_.Office
if ($_.DN -ne $null -and ($_.DN.StartsWith("OU=")) ) {
    Get-QADUser -SizeLimit 0 -SearchRoot $_.DN | 
        foreach-object { Set-QADUser -identity $_.DN -Office $myOff}

我可能会离开这里,最好从头开始,但我们将不胜感激。

【问题讨论】:

    标签: powershell csv exchange-server powershell-3.0 hybrid


    【解决方案1】:

    问题在于语法。您要么需要将其设为多行,要么在$myOff = $_.Office 之后和If ( 之前插入一个分号来分隔命令。我的建议如下,或者让它更易于阅读:

    Import-CSV d:\scripts\ExchangeExportQuery.csv | ForEach-Object {
        $myOff = $_.Office 
        if ($_.DistinguishedName -ne $null -and ($_.DistinguishedName.StartsWith("CN=")) ) {
            Get-User -ResultSize   'unlimited' -identity $_.DistinguishedName | foreach-object { 
                Set-User -identity $_.DistinguishedName -Office $myOff
            } #End nested ForEach
        } #End If
    } #End Outer ForEach
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      相关资源
      最近更新 更多