# Set up command to run and vars for that command $ScriptBlock = {New-InboundConnector -Name $connector_name -SenderDomains {smtp:*;1} -EFSkipIPs $spamfilterips -RestrictDomainsToCertificate $true -RequireTLS $true -TlsSenderCertificateName $hostname -CloudServicesMailEnabled $true -ConnectorType OnPremises} $connector_name = 'Spamfilter Connector' $hostname = 'spamgateway.aperturect.com' $spamfilterips = {127.0.0.1}, {127.0.0.2} # Establish a PowerShell session with Office 365. You'll be prompted for your Delegated Admin credentials $Cred = Get-Credential Connect-MsolService -Credential $Cred # Locate .csv with targets $CsvLocation = Read-Host -Prompt 'Tenants CSV path (use "Get-MsolPartnerContract -All | export-csv" and delete the rows you do not need' $customers = Import-Csv $CsvLocation Write-Host "Found $($customers.Count) customers in the .csv file." # Go through the targets and execute $ScriptBlock foreach ($customer in $customers) { $InitialDomain = Get-MsolDomain -TenantId $customer.TenantId | Where-Object {$_.IsInitial -eq $true} Write-Host "Creating connector for $($Customer.Name)" $DelegatedOrgURL = "https://ps.outlook.com/powershell-liveid?DelegatedOrg=" + $InitialDomain.Name Invoke-Command -ConnectionUri $DelegatedOrgURL -Credential $Cred -Authentication Basic -ConfigurationName Microsoft.Exchange -AllowRedirection -ScriptBlock $ScriptBlock -HideComputerName }