【问题标题】:Using New-MailContact, Set-MailContact, and Set-Contact inside background jobs在后台作业中使用 New-MailContact、Set-MailContact 和 Set-Contact
【发布时间】:2017-10-31 21:54:04
【问题描述】:

这是更新的,但仍然出现相同的错误。

$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

对于每个循环,它都失败了:

“New-MailContact”一词未被识别为 cmdlet 的名称, 函数、脚本文件或可运行的程序。检查拼写 名称,或者如果包含路径,请验证路径是否正确并 再试一次。 + CategoryInfo : ObjectNotFound: (New-MailContact:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException + PSComputerName : 本地主机

“Set-MailContact”一词未被识别为 cmdlet 的名称, 函数、脚本文件或可运行的程序。检查拼写 名称,或者如果包含路径,请验证路径是否正确并 再试一次。 + CategoryInfo : ObjectNotFound: (Set-MailContact:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException + PSComputerName : 本地主机

“Set-Contact”一词未被识别为 cmdlet 的名称, 函数、脚本文件或可运行的程序。检查拼写 名称,或者如果包含路径,请验证路径是否正确并 再试一次。 + CategoryInfo : ObjectNotFound: (Set-Contact:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException + PSComputerName : 本地主机

在后台作业中运行这些命令有什么技巧吗?

【问题讨论】:

    标签: powershell office365 jobs


    【解决方案1】:

    您需要导入正确的 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
    

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

    【讨论】:

    • 啊好吧,这需要在工作中完成,我事先运行了以下命令 $UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri @ 987654321@ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $session Connect-MsolService -cred $UserCredential
    • 是的,身份验证代码需要在作业中运行。新工作 = 一个新的 powershell 进程,它不会因其父级的任何过去而受到玷污。
    猜你喜欢
    • 2019-03-31
    • 2012-02-19
    • 2018-11-02
    • 2018-10-31
    • 2019-01-16
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多