AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1817070
Accepted
FuxT
FuxT
Asked: 2023-11-17 23:15:52 +0800 CST2023-11-17 23:15:52 +0800 CST 2023-11-17 23:15:52 +0800 CST

使用 Microsoft Graph API 更新联系人文件夹

  • 772

我在使用新的 Microsoft API Graph 时遇到了一些问题。我在通用 Outlook 配置文件中有一个默认的联系人文件夹。我已经把它推送给我的所有用户了。每个人都有一个通用文件夹,里面有 530 个联系人。

现在的问题是更新它而不删除并重新启动,否则脚本执行时间长达14小时!

我已经看过API Graph Delta,但我不知道如何使用它,我对 powershell 是“新手”,所以 URI 请求和所有典型的事情都有点困难,有人能给我一个关于如何更新的例子吗一个包含我的默认文件夹的文件夹?剩下的我来做。

我编写了这个 powershell 代码,采用了互联网上第一个脚本的一部分,归功于Sean McAvinue

<#
Details: Graph / PowerShell Script t populate user contacts based on CSV input, 
        Please fully read and test any scripts before running in your production environment!
        .SYNOPSIS
        Populates mail contacts into user mailboxes from a CSV

        .DESCRIPTION
        Creates a new mail contact for each entry in the input CSV in the target mailbox.

        .PARAMETER Mailbox
        User Principal Name of target mailbox

        .PARAMETER CSVPath
        Full path to the input CSV

        .PARAMETER ClientID
        Application (Client) ID of the App Registration

        .PARAMETER ClientSecret
        Client Secret from the App Registration

        .PARAMETER TenantID
        Directory (Tenant) ID of the Azure AD Tenant

        .EXAMPLE
        .\graph-PopulateContactsFromCSV.ps1 -Mailbox  $mailbox -ClientSecret $clientSecret -ClientID $clientID -TenantID $tenantID -CSVPath $csv
        
        .Notes
        For similar scripts check out the links below
        
            Blog: https://seanmcavinue.net
            GitHub: https://github.com/smcavinue
            Twitter: @Sean_McAvinue
            Linkedin: https://www.linkedin.com/in/sean-mcavinue-4a058874/


#>
#>
#################################################
####################FONCTIONS####################
#################################################
function GetGraphToken {
    # Azure AD OAuth Application Token for Graph API
    # Get OAuth token for a AAD Application (returned as $token)
<#
        .SYNOPSIS
        This function gets and returns a Graph Token using the provided details
    
        .PARAMETER clientSecret
        -is the app registration client secret
    
        .PARAMETER clientID
        -is the app clientID
    
        .PARAMETER tenantID
        -is the directory ID of the tenancy
#>
    Param(
        [parameter(Mandatory = $true)]
        [String]
        $ClientSecret,
        [parameter(Mandatory = $true)]
        [String]
        $ClientID,
        [parameter(Mandatory = $true)]
        [String]
        $TenantID
    
    )
    
    # Construct URI
    $uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
         
    # Construct Body
    $body = @{
        client_id     = $clientId
        scope         = "https://graph.microsoft.com/.default"
        client_secret = $clientSecret
        grant_type    = "client_credentials"
    }
         
    # Get OAuth 2.0 Token
    $tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
         
    # Access Token
    $token = ($tokenRequest.Content | ConvertFrom-Json).access_token
    return $token
}

function ImportContact {
    <#
.SYNOPSIS
Imports contact into specified user mailbox

.DESCRIPTION
This function accepts an AAD token, user account and contact object and imports the contact into the users mailbox

.PARAMETER Mailbox
User Principal Name of target mailbox

.PARAMETER Contact
Contact object for processing

.PARAMETER Token
Access Token      
#>
    Param(
        [parameter(Mandatory = $true)]
        [String]
        $Token,
        [parameter(Mandatory = $true)]
        [String]
        $Mailbox,
         [parameter(Mandatory = $true)]
        [String]
        $folder,
        [parameter(Mandatory = $true)]
        [PSCustomObject]
        $contact
    
    )
    write-host "contactcompanyname $($contact.companyname)"
    write-host $contact
    #Creation de l'objet "contact"
    $ContactObject = @"
    {
        "assistantName": "$($contact.assistantName)",
        "businessHomePage": "$($contact.businessHomePage)",
        "businessPhones": [
            "$($contact.businessPhones)"
          ],
        "displayName": "$($contact.displayName)",
        "emailAddresses": [
            {
                "address": "$($contact.emailaddress)",
                "name": "$($contact.displayname)"
            }
        ],
        "givenName": "$($contact.givenname)",
        "middleName": "$($contact.middleName)",
        "nickName": "$($contact.nickName)",
        "surname": "$($contact.surname)",
        "title": "$($contact.title)"
    }
"@
    write-host "contact object: $contactobject"

#Creation du token d'autentification pour chaque boite mail contenue dans cible.txt
foreach($mail in $CSVPath){
    $apiUri = "https://graph.microsoft.com/v1.0/users/$person/contactFolders/$folderid/contacts"
    write-host $apiuri
  
        $NewContact = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)" } -ContentType 'application/json' -Body $contactobject -Uri $apiUri -Method Post)
        return $NewContact
         }
    catch {
        throw "Error creating contact $($contact.emailaddress) for $person $($_.Exception.Message)"
        continue
        }
}
                    
                    

##Import CSV
try {
    $Contacts = import-csv $CSVPath -ErrorAction stop
}
catch {
    throw "Erreur d'import CSV: $($_.Exception.Message)"
    break
}

##Graph Token
Try {
    $Token = GetGraphToken -ClientSecret $ClientSecret -ClientID $ClientID -TenantID $TenantID
}
catch {
    throw "Erreur d'obtention de token"
    break
}

##ProcessImport
foreach ($contact in $contacts) {
    $NewContact = ImportContact -Mailbox $person -token $token -contact $contact -folder "Sync-Opac"
}

然后我的脚本,需要很多“如果”来定位错误并向同事演示。它对于第一次“大型”部署很有用。


<#
"----Installation --ET-- importation du module Pwsh Microsoft Graph pour executer le script---" -ForegroundColor red
"----------Processus LONG, patientez 10/15min------"      
Install-module -Name Microsoft.Graph -verbose
Import-Module -Name Microsoft.Graph -verbose
#>

##Connexion###
$uri = "https://login.microsoftonline.com/4f232a97-8219-4450-9bec-410e6d0472d0/oauth2/v2.0/token"
#
    $body = @{
        client_id     = $clientId
        scope         = "https://graph.microsoft.com/.default"
        client_secret = $clientSecret
        grant_type    = "client_credentials"
    }  
#
$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing -verbose  
#
write-host "-------------Obtention & Conversion du token en cours...-------------" -ForegroundColor green
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token
#
    if ($? -eq $true){
        write-host "-------------Token genere >> securisation en cours-------------" -ForegroundColor green
                    }
else{
    write-host "-------------Erreur sur la recuperation du token-------------" -ForegroundColor red
    break
    }
$tokensecure = ConvertTo-SecureString -string $token -AsPlainText -Force -verbose
#
    if ($? -eq $true){
        write-host "-------------Token sécurisé obtenu, connexion...-------------" -ForegroundColor green
                try{
                    Connect-MgGraph -NoWelcome -AccessToken $tokensecure
                    "---------------------------------------"
                    "------------#CONNEXION OK!#------------"
                    "---------------------------------------"
                }catch{
                    write-host "-------------Connexion impossible avec le token specifie-------------" -ForegroundColor red
                    break
                      }
}

foreach ($person in $mailbox ){
    Clear-Host
    $folder = Get-MgUserContactFolder -UserID $person | ? {$_.DisplayName -eq"Sync-Opac"} -errorAction ignore
    if($folder -eq $null){

                                                ##Connexion###
                                               $uri = "https://login.microsoftonline.com/4f232a97-8219-4450-9bec-410e6d0472d0/oauth2/v2.0/token"
    
                                               $body = @{
                                               client_id     = $clientId
                                               scope         = "https://graph.microsoft.com/.default"
                                               client_secret = $clientSecret
                                               grant_type    = "client_credentials"
                                              }  

                                            $tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing -verbose  
                                            #
                                            write-host "-------------Obtention & Conversion du token en cours...-------------" -ForegroundColor green
                                            $token = ($tokenRequest.Content | ConvertFrom-Json).access_token
                                            #
                                                if ($? -eq $true){
                                                    write-host "-------------Token genere >> securisation en cours-------------" -ForegroundColor green
                                                                }
                                            else{
                                                write-host "-------------Erreur sur la recuperation du token-------------" -ForegroundColor red
                                                break
                                                }
                                            $tokensecure = ConvertTo-SecureString -string $token -AsPlainText -Force -verbose
                                            #
                                            if ($? -eq $true){
                                                write-host "-------------Token sécurisé obtenu, connexion...-------------" -ForegroundColor green
                                                        try{
                                                            Connect-MgGraph -NoWelcome -AccessToken $tokensecure
                                                            "---------------------------------------"
                                                            "------------#CONNEXION OK!#------------"
                                                            "---------------------------------------"
                                                            Clear-Host
                                                        }catch{
                                                            write-host "-------------Connexion impossible avec le token specifie-------------" -ForegroundColor red
                                                            break
                                                              }
                                        }
                                                #Fin de connexion
                              ########################################Creation contact##################
New-MgUserContactFolder -UserId $person -DisplayName Sync-Opac
start-sleep 5
$folder = Get-MgUserContactFolder -UserID $person | ? {$_.DisplayName -eq"Sync-Opac"}
$folderid = $folder.Id
./pwsh_graph_contacts.ps1
write-host "----------Contact importé && Dossier Contacts créé pour $person------------" -ForegroundColor Green
Clear-Host
continue
#si compte deja peuplé alors connexion et suppression
                                }else{
                                            ##Connexion###
                                            $uri = "https://login.microsoftonline.com/4f232a97-8219-4450-9bec-410e6d0472d0/oauth2/v2.0/token"
                                            #
                                                $body = @{
                                                    client_id     = $clientId
                                                    scope         = "https://graph.microsoft.com/.default"
                                                    client_secret = $clientSecret
                                                    grant_type    = "client_credentials"
                                                }  
                                            #
                                            $tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing -verbose  
                                            #
                                            write-host "-------------Obtention & Conversion du token en cours...-------------" -ForegroundColor green
                                            $token = ($tokenRequest.Content | ConvertFrom-Json).access_token
                                            #
                                                if ($? -eq $true){
                                                    write-host "-------------Token genere >> securisation en cours-------------" -ForegroundColor green
                                                                }
                                            else{
                                                write-host "-------------Erreur sur la recuperation du token-------------" -ForegroundColor red
                                                break
                                                }
                                            $tokensecure = ConvertTo-SecureString -string $token -AsPlainText -Force -verbose
                                            #
                                                if ($? -eq $true){
                                                    write-host "-------------Token sécurisé obtenu, connexion...-------------" -ForegroundColor green
                                                    
                                                            try{
                                                                Connect-MgGraph -NoWelcome -AccessToken $tokensecure
                                                                "---------------------------------------"
                                                                "------------#CONNEXION OK!#------------"
                                                                "---------------------------------------"
                                                                Clear-Host
                                                            }catch{
                                                                write-host "-------------Connexion impossible avec le token specifie-------------" -ForegroundColor red
                                                                break
                                                                  }
                                            }
                                            #Fin de connexion
                                            ########################################Creation contact##################
                                       
.\suppression_graph_contacts.ps1
Clear-Host
start-sleep 10
New-MgUserContactFolder -UserId $person -DisplayName Sync-Opac
$folder = Get-MgUserContactFolder -UserID $person | ? {$_.DisplayName -eq"Sync-Opac"}
$folderid = $folder.Id
./pwsh_graph_contacts.ps1
write-host "----------Contacts importés pour $person-------------------" -ForegroundColor Green
continue
   }
}

我清楚地了解我的部分,但我实际使用的只有第一个部分的 50%。

microsoft-outlook
  • 2 2 个回答
  • 33 Views

2 个回答

  • Voted
  1. Best Answer
    Faery
    2023-11-22T16:01:35+08:002023-11-22T16:01:35+08:00

    您可以使用 HTTP 请求,这是调用 Microsoft Graph API 的直接方式,让您可以更灵活地控制请求和响应。您可以使用 GET 方法获取联系人文件夹的增量更改,并使用 PATCH 方法更新联系人文件夹的属性,例如:

     // Get the incremental changes of the contact folders
    GET /me/contactFolders/delta
    // Iterate through the delta collection
    for each deltaFolder in response
    {
        // Check if it is your default folder
        if (deltaFolder.displayName == "Default Folder")
        {
            // Modify the folder's properties
            PATCH /me/contactFolders/{id}
            Content-type: application/json
            {
                "displayName": "Updated Folder"
            }
        }
    }
    
    • 2
  2. Cpt.Whale
    2023-11-18T01:21:15+08:002023-11-18T01:21:15+08:00

    New-MgUserContactFolder从脚本中删除命令。否则,将您的 CSV 导入到每个人的个人联系人中应该可以正常运行。它不会覆盖整个文件夹或删除 CSV 中没有的联系人。

    在单个用户上进行测试。

    如果您想减少所需时间,可以过滤已从 CSV 添加的联系人,或使用Get-MgUserContact查询现有联系人并仅根据需要进行更新。


    一般来说,您应该只对那些可能不会经常更新的重要联系人(如 IT 服务台、人力资源和其他内部服务)执行此操作,因为更新速度很慢。如果列表变得太大,请考虑使用中央共享联系人文件夹。

    • 0

相关问题

  • Outlook 规则 - 严格的单词匹配 - 如果字符串在 Word 中则不匹配 - 例如:匹配 XYZ 但不匹配 WXYZW

  • Office365密码-每天提示一次

  • 恢复丢失的 Skype 会议邀请的 Skype 会议 URL

  • 防止保存 PST 文件密码?

  • 主收件箱文件夹中的 Outlook 视图已更改,但其他文件夹未更改

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve