{"id":13337,"date":"2024-03-14T09:15:28","date_gmt":"2024-03-14T08:15:28","guid":{"rendered":"https:\/\/roglacup.com\/klaus62\/?p=13337"},"modified":"2024-03-19T07:26:57","modified_gmt":"2024-03-19T06:26:57","slug":"microsoft-graph-powershell-sdk","status":"publish","type":"post","link":"https:\/\/roglacup.com\/klaus62\/2024\/03\/14\/microsoft-graph-powershell-sdk\/","title":{"rendered":"Microsoft Graph &#038; PowerShell SDK"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-connecting-to-the-graph-sdk\">Connecting to the Graph SDK<\/h2>\n\n\n\n<p>The first step in any use of the Graph SDK is to connect to the Graph using the&nbsp;<em>Connect-MgGraph<\/em>&nbsp;cmdlet. When you run&nbsp;<em>Connect-MgGraph<\/em>&nbsp;to connect to the Graph, it\u2019s wise to specify the identifier of the tenant to which you want to connect.<\/p>\n\n\n\n<p>Connect-MgGraph -TenantId &#8220;828e1143-88e3-492b-bf82-24c4a47ada63&#8221;<\/p>\n\n\n\n<p>If you don\u2019t specify a tenant,&nbsp;<em>Connect-MgGraph<\/em>&nbsp;will choose the last tenant you signed into during a session (which might not be the one you want to connect to). I discovered this when I connected to the Graph and discovered that the data used belonged to my development tenant. I noticed then, but it\u2019s possible that someone might miss this elsewhere, so make it a habit to connect with the tenant identifier.<\/p>\n\n\n\n<p>A session lasts until you run&nbsp;<em>Disconnect-MgGraph<\/em>&nbsp;(see below) and can be reinitiated multiple times over days by running&nbsp;<em>Connect-MgGraph<\/em>. Behind the scenes, the Graph SDK keeps an encrypted token cache and will refresh the token as needed to allow you to work with Graph commands.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-gathering-new-permissions\">Gathering New Permissions<\/h2>\n\n\n\n<p>As people begin executing Graph SDK commands using the interactive client, they will need consent for the permissions needed to run the commands. For example, to use the&nbsp;<em>Get-MgUser<\/em>&nbsp;cmdlet to retrieve a set of Azure AD accounts, a user needs permission to read directory information, so they might request the permissions using the&nbsp;<em>Scope<\/em>&nbsp;parameter when making the connect as follows:<\/p>\n\n\n\n<p><strong>$RequiredScopes<\/strong> = @(&#8220;Directory.AccessAsUser.All&#8221;, &#8220;Directory.ReadWrite.All&#8221;)<\/p>\n\n\n\n<p>Connect-MgGraph -Scopes <strong>$RequiredScopes<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-reporting-your-connection\">Reporting Your Connection<\/h2>\n\n\n\n<p>To check that you\u2019re connected to the right tenant with the right profile and permissions, we can extract information about the tenant with the&nbsp;<em>Get-MgOrganization<\/em>&nbsp;cmdlet, the current connection with the&nbsp;<em>Get-MgContext<\/em>&nbsp;cmdlet, and the profile used with the&nbsp;<em>Get-MgProfile<\/em>&nbsp;cmdlet and display some useful information:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>$Details<\/strong> = Get-MgContext<\/p>\n\n\n\n<p><strong>$Scopes<\/strong> = <strong>$Details<\/strong> | Select -ExpandProperty Scopes<\/p>\n\n\n\n<p><strong>$Scopes<\/strong> = <strong>$Scopes<\/strong> -Join &#8220;, &#8220;<\/p>\n\n\n\n<p><strong>$OrgName<\/strong> = (Get-MgOrganization).DisplayName<\/p>\n\n\n\n<p>CLS<\/p>\n\n\n\n<p>Write-Host &#8220;Microsoft Graph Connection Information&#8221;<\/p>\n\n\n\n<p>Write-Host &#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#8220;<\/p>\n\n\n\n<p>Write-Host &#8221; &#8220;<\/p>\n\n\n\n<p>Write-Host (&#8220;Connected to Tenant {0} ({1}) as account {2}&#8221; -f <strong>$Details<\/strong>.TenantId, <strong>$OrgName<\/strong>, <strong>$Details<\/strong>.Account)<\/p>\n\n\n\n<p>Write-Host &#8220;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8221;<\/p>\n\n\n\n<p>Write-Host (&#8220;Profile set as {0}. The following permission scope is defined: {1}&#8221; -f <strong>$ProfileName<\/strong>, <strong>$Scopes<\/strong>)<\/p>\n\n\n\n<p>Write-Host &#8220;&#8221;<\/p>\n<\/blockquote>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-disconnect-when-you-re-done\">Disconnect When You\u2019re Done<\/h2>\n\n\n\n<p>When you\u2019re finished interacting with the Graph, remember to close off the session by running&nbsp;<em>Disconnect-MgGraph&nbsp;<\/em>to sign the session out from the Graph<em>.<\/em>&nbsp;Disconnecting the session removes the encrypted token cache and prevents a session from being reinitialized.<\/p>\n\n\n\n<p>Disconnect-MgGraph<\/p>\n\n\n\n<p>Posted from: <a href=\"https:\/\/practical365.com\/connect-microsoft-graph-powershell-sdk\/\">https:\/\/practical365.com\/connect-microsoft-graph-powershell-sdk\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Connecting to the Graph SDK The first step in any use of the Graph SDK is to connect to the Graph using the&nbsp;Connect-MgGraph&nbsp;cmdlet. When you run&nbsp;Connect-MgGraph&nbsp;to connect to the Graph, it\u2019s wise to specify the identifier of the tenant to which you want to connect. Connect-MgGraph -TenantId &#8220;828e1143-88e3-492b-bf82-24c4a47ada63&#8221; If you don\u2019t specify a tenant,&nbsp;Connect-MgGraph&nbsp;will choose&hellip;&nbsp;<a href=\"https:\/\/roglacup.com\/klaus62\/2024\/03\/14\/microsoft-graph-powershell-sdk\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Microsoft Graph &#038; PowerShell SDK<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":13408,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[384,20],"tags":[],"class_list":["post-13337","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","category-script"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/posts\/13337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/comments?post=13337"}],"version-history":[{"count":2,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/posts\/13337\/revisions"}],"predecessor-version":[{"id":13409,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/posts\/13337\/revisions\/13409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/media\/13408"}],"wp:attachment":[{"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/media?parent=13337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/categories?post=13337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/tags?post=13337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}