Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 在后台作业中使用New MailContact、Set MailContact和Set Contact_Powershell_Office365_Jobs - Fatal编程技术网

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

Powershell 在后台作业中使用New MailContact、Set MailContact和Set Contact,powershell,office365,jobs,Powershell,Office365,Jobs,这里是更新的,仍然得到相同的错误 $UserCredential = Get-Credential $contacts = Import-Csv "C:\temp\testgal.csv" Start-Job -Name Loop -ScriptBlock { param([pscustomobject[]]$contacts, [System.Management.Automation.PSCredential[]]$UserCredential) $s

这里是更新的,仍然得到相同的错误

$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:localhost

术语“Set-MailContact”未被识别为cmdlet的名称, 函数、脚本文件或可操作程序。检查单词的拼写 名称,或者如果包含路径,请验证该路径是否正确,并且 再试一次。 +CategoryInfo:ObjectNotFound:Set-MailContact:String[],CommandNotFoundException +FullyQualifiedErrorId:CommandNotFoundException +PSComputerName:localhost

术语“Set Contact”未被识别为cmdlet的名称, 函数、脚本文件或可操作程序。检查单词的拼写 名称,或者如果包含路径,请验证该路径是否正确,并且 再试一次。 +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

注意。这是未经测试的,但应该会让您走上正确的道路。

啊,好的,那么这需要在工作中完成吗,我已经运行了以下程序$UserCredential=Get Credential$Session=New PSSession-ConfigurationName Microsoft.Exchange-ConnectionUri-Credential$UserCredential-Authentication Basic-AllowDirection Import PSSession$Session Connect MsolService-cred$UserCredential是需要在作业内部运行身份验证代码。New job=一个新的powershell进程,它不会被父进程的任何过去所玷污。