我已经和微软谈过了,他们说过他们扼杀了他们这边的接口但是如果你运行7个工作,每个工作10,000个,而不是70,000工作,它应该工作正常,因为他们只是单独限制每个会话。
我使用Measure-Command
简化了脚本本身,使每个部分以最大速度运行,并使其按照我想要的方式工作。
所以我们现在回到最初的问题,以某种方式拆分CSV输入并在每个块上运行导入作业,并且还可能对删除执行相同操作并同时运行多个删除循环。
首先需要“多播”的两个循环,然后是下面的完整更新脚本:
从CSV导入:
Do {
$counter2 = 0
$CSVpath = "C:\temp\testgal.csv"
$contacts = Import-Csv "$CSVpath"
Measure-Command{
foreach ($c in $contacts) {
Try {
$ErrorActionPreference = "stop"
$counter2++
$name = $c.displayName
$rawProxy = $c.proxyAddresses
$proxysplit = $rawproxy -split '(?<!\\);'
$proxyquoted = $proxysplit.replace('x500','"x500').replace('x400','"x400').replace('X500','"X500').replace('X400','"X400')
$proxy = $proxyquoted
Write-Progress -Activity 'Creating Contacts...' -CurrentOperation $c.displayName -PercentComplete (($counter2 / $contacts.Count) * 100)
New-MailContact -ExternalEmailAddress $c.Mail -Name "`"$name`"" -Alias $c.mailNickname -DisplayName $name -FirstName $c.givenName -Initials $c.initials -LastName $c.sn
Set-MailContact -Identity $c.mailNickname -CustomAttribute1 "CreatedWithScript" -CustomAttribute3 $c.extensionAttribute3 -EmailAddresses $proxy
Set-Contact -Identity $c.mailNickname -City $c.l -Company $c.company -Department $c.department -Office $c.physicalDeliveryOfficeName `
-Phone $c.telephoneNumber -PostalCode $c.postalCode -Title $c.title
}Catch {$_ | Out-File 'C:\temp\Error_Log.txt' -Append
Write-Warning -message "$name Already exists."}
}
}
Write-Progress -Activity "Creating Contacts..." -Status "Completed." -Completed
Write-Host "----------------------------------"
Write-Host "All Contacts successfully created."
Write-Host "----------------------------------"
$ErrorActionPreference = "continue"
}
Until($? -eq "True")
从365中删除:
Do{
$totalnumberremaining = Get-Mailcontact -Filter "CustomAttribute1 -eq 'CreatedWithScript'" -ResultSize unlimited
$numberremaining = Get-Mailcontact -Filter "CustomAttribute1 -eq 'CreatedWithScript'" -ResultSize 1000
$counter = 0
Do{
foreach ($contact in $numberremaining) {
$counter++
Remove-MailContact -Identity $contact.Alias -Confirm:$False
}
Write-Progress -Activity 'Deleting Contacts...' -CurrentOperation $contact -PercentComplete (($counter / $totalnumberremaining.count) * 100)
}
Until($numberremaining -eq $null)
完整脚本:
#Logs into Office365
Do {
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $session
Connect-MsolService -cred $UserCredential
}
Until ($? -eq "True")
Do{
#Removes all the current Contacts
$totalnumberremaining = Get-Mailcontact -Filter "CustomAttribute1 -eq 'CreatedWithScript'" -ResultSize unlimited
$numberremaining = Get-Mailcontact -Filter "CustomAttribute1 -eq 'CreatedWithScript'" -ResultSize 1000
$counter = 0
Do{
foreach ($contact in $numberremaining) {
$counter++
Remove-MailContact -Identity $contact.Alias -Confirm:$False
}
Write-Progress -Activity 'Deleting Contacts...' -CurrentOperation $contact -PercentComplete (($counter / $totalnumberremaining.count) * 100)
}
Until($numberremaining -eq $null)
Write-Progress -Activity 'Deleting Contacts...' -Status "Completed." -Completed
Write-Host "----------------------------------"
Write-Host "All Contacts successfully removed."
Write-Host "----------------------------------"
Pause
# Imports new contacts from the CSV
Do {
$counter2 = 0
$CSVpath = "C:\temp\testgal.csv"
$contacts = Import-Csv "$CSVpath"
Measure-Command{
foreach ($c in $contacts) {
Try {
$ErrorActionPreference = "stop"
$counter2++
$name = $c.displayName
$rawProxy = $c.proxyAddresses
$proxysplit = $rawproxy -split '(?<!\\);'
$proxyquoted = $proxysplit.replace('x500','"x500').replace('x400','"x400').replace('X500','"X500').replace('X400','"X400')
$proxy = $proxyquoted
Write-Progress -Activity 'Creating Contacts...' -CurrentOperation $c.displayName -PercentComplete (($counter2 / $contacts.Count) * 100)
New-MailContact -ExternalEmailAddress $c.Mail -Name "`"$name`"" -Alias $c.mailNickname -DisplayName $name -FirstName $c.givenName -Initials $c.initials -LastName $c.sn
Set-MailContact -Identity $c.mailNickname -CustomAttribute1 "CreatedWithScript" -CustomAttribute3 $c.extensionAttribute3 -EmailAddresses $proxy
Set-Contact -Identity $c.mailNickname -City $c.l -Company $c.company -Department $c.department -Office $c.physicalDeliveryOfficeName `
-Phone $c.telephoneNumber -PostalCode $c.postalCode -Title $c.title
}Catch {$_ | Out-File 'C:\temp\Error_Log.txt' -Append
Write-Warning -message "$name Already exists."}
}
}
Write-Progress -Activity "Creating Contacts..." -Status "Completed." -Completed
Write-Host "----------------------------------"
Write-Host "All Contacts successfully created."
Write-Host "----------------------------------"
$ErrorActionPreference = "continue"
}
Until($? -eq "True")
# Exits or repeats script on user imput
$response = read-host "Repeat(R) or Quit(Q)?"
}
While ($response -ne "Q")
Remove-PSSession $session