【问题标题】:Is there any logic like if else or any other command in powershell to get the powershell script non-interactive?是否有任何逻辑,如 if else 或 powershell 中的任何其他命令,以使 powershell 脚本非交互式?
【发布时间】:2022-01-05 02:06:22
【问题描述】:

我正在准备powershell脚本,通过powershell脚本将本地邮箱移动到在线邮箱,迁移后将分配许可证

下面是示例:

#Created the user in AD , not mentioned here 
Enable-Mailbox -Identity "$firstname $lastname" -Database "XXXXXXX"
Start-Sleep -Seconds 10
Set-Mailbox "$firstname.$lastname" -PrimarySmtpAddress "$firstname.$lastname@XXXXXXX" -EmailAddressPolicyEnabled $false

一旦用户邮箱从本地交换同步到在线交换,是否有任何逻辑,如 if-else 或其他我可以在这里使用的触发以下命令?所以下面的命令成功运行,没有任何问题或错误。

Connect-ExchangeOnline
$Mailbox = "$firstname.$lastname@XXXXXXX"
$Endpoint = "XXXXXXXX"
$TargetDomain = "XXXXXXXXXXX"
$Cred = Get-Credential
New-MoveRequest -Identity $Mailbox -Remote -RemoteHostName $Endpoint -TargetDeliveryDomain $TargetDomain -RemoteCredential $Cred -Batchname "$Mailbox Move to O365"

是否有任何逻辑,如 if-else 或其他我可以在这里使用的逻辑,一旦邮箱迁移完成,就会触发下面的命令,然后执行下面的命令?

#Assign the license once migration is done successfully 
Set-MsolUser -UserPrincipalName $Mailbox -UsageLocation US
Set-MsolUserLicense -UserPrincipalName $Mailbox -AddLicenses "XXXXXXXXXXXX"

注意-我可以在两者之间使用Start-Sleep -Seconds XXX,但每次同步时间都不一样

【问题讨论】:

  • 是的,PowerShell 确实使用了if..else 语句。请参阅文档中的this 文章。

标签: powershell office365 exchange-server


【解决方案1】:

为了在邮箱迁移后分配许可证;您需要首先检查while 循环中move-request 的状态,如果状态更改为Completed,请执行您的命令,如下所示:

$i = $true
while ($i){

    if (Get-MoveRequest -Identity $Mailbox| where {$_.status -eq "Completed"}) {

        Set-MsolUser -UserPrincipalName $Mailbox -UsageLocation US
        Set-MsolUserLicense -UserPrincipalName $Mailbox -AddLicenses "XXXXXXXXXXXX"
        $i = $False
    } else {
            sleep 10
           }

}

【讨论】:

  • 非常感谢@Mahmoud Moawad 效果很好。
  • 不客气 Suri,请将代码标记为答案。
猜你喜欢
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多