powershell - 表达式或语句 powershell 中的意外标记 'if'

标签 powershell csv exchange-server powershell-3.0 hybrid

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

其他信息:

Exchange 2013 混合服务器

Forrest Functional Level = 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}

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

最佳答案

问题是语法。您要么需要将其设为多行,要么在 $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

关于powershell - 表达式或语句 powershell 中的意外标记 'if',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25901593/

相关文章:

python - 循环中嵌套字典

c# - 页面发送邮件超时,即使发送了邮件

powershell - 忽略文件中的第一行和最后一行

python - 在 Python Pandas 中连接大量 CSV 文件(30,000)

MySQL load data infile 恰好加载一半的记录

exchange-server - Exchange Server 将每日重复模式更改为每周?

c# - 如何使用 C# 向 Exchange 分发列表发送电子邮件

arrays - 如果字符串的子字符串存在于同一数组中,则删除字符串

powershell - 使用 Powershell 从一个文件夹中的多个 CSV 文件中删除前 15 行

xml - Powershell 和 XML : How to count specific elements for each node