在后台作业中使用 New-MailContact、Set-MailContact 和 Set-Contact

Using New-MailContact, Set-MailContact, and Set-Contact inside background jobs

这是更新后的内容,仍然出现相同的错误。

$UserCredential = Get-Credential
$contacts = Import-Csv "C:\temp\testgal.csv"
    Start-Job -Name Loop -ScriptBlock {
        param([pscustomobject[]]$contacts, [System.Management.Automation.PSCredential[]]$UserCredential)
        $session2 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
        Import-PSSession $session2
        Connect-MsolService -cred $UserCredential
        foreach ($c in $contacts){
        ........
        }
    } -ArgumentList (,$contacts, $UserCredential)
Wait-Job -State Running | Receive-Job
Get-Job -State Completed | Remove-Job

我正在尝试使用脚本在 365 中创建大量联系人,并希望 运行 并行作业来加快速度,我在作业中构建了一个循环并使用在循环内部跟踪以确保它可以从 CSV 中提取正确的变量。

$name = $c1.displayname
New-Item -Path "C:\Temp\Output" -Name "$name"  -ItemType "file"

现在,当我尝试 运行 使用标题中的命令循环时

 $contacts = Import-Csv "C:\temp\gal\testgal.csv"
       Start-Job -Name Loop -ScriptBlock {
           param([pscustomobject[]]$contacts)
            foreach ($c in $contacts){
                $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
                New-MailContact -ExternalEmailAddress $c.Mail -Name "`"$name`"" -Alias $c.mailNickname -DisplayName $name -FirstName $c.givenName -Initials $c.initials -LastName $c.sn -AsJob
                Set-MailContact -Identity $c.mailNickname -CustomAttribute1 "CreatedWithScript" -CustomAttribute3 $c.extensionAttribute3 -EmailAddresses $proxy -AsJob
                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 -AsJob
           }
       } -ArgumentList (,$contacts)
       Wait-Job -State Completed | Receive-Job
       Get-Job -State Completed | Remove-Job

它没有为每个循环说出以下内容:

The term 'New-MailContact' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + CategoryInfo : ObjectNotFound: (New-MailContact:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException + PSComputerName : localhost

The term 'Set-MailContact' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + CategoryInfo : ObjectNotFound: (Set-MailContact:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException + PSComputerName : localhost

The term 'Set-Contact' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + CategoryInfo : ObjectNotFound: (Set-Contact:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException + PSComputerName : localhost

是否有技巧可以 运行在后台作业中执行这些命令?

您需要导入正确的 powershell 模块,在本例中为 Office365:

Import-Module MSOnline

您还需要进行身份验证,以便将用户名和密码传递给您的作业并创建凭据对象:

 $contacts = Import-Csv "C:\temp\gal\testgal.csv"
       Start-Job -Name Loop -ScriptBlock {
           param([pscustomobject[]]$contacts, [string]$username, [string]$password)
            Import-Module MSOnline
            $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
            $creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
            $O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $creds -Authentication Basic -AllowRedirection
            Connect-MsolService –Credential $creds
            foreach ($c in $contacts){
              ...
           }
       } -ArgumentList (,$contacts, $username, $password)
       Wait-Job -State Completed | Receive-Job
       Get-Job -State Completed | Remove-Job

注意。这是未经测试的,但应该会让你走上正轨。