{"id":902,"date":"2019-09-23T05:35:25","date_gmt":"2019-09-23T04:35:25","guid":{"rendered":"http:\/\/www.vatland.no\/?p=902"},"modified":"2019-09-23T05:49:15","modified_gmt":"2019-09-23T04:49:15","slug":"csp-access-to-tenants-using-powershell-part-3","status":"publish","type":"post","link":"https:\/\/www.vatland.no\/index.php\/csp-access-to-tenants-using-powershell-part-3\/","title":{"rendered":"CSP access to tenants using powershell. Part 3"},"content":{"rendered":"\n<p>In this part 3 of CSP and powershell I will show how you can connect to azureAD of a customer tenant using your CSP app credentials and refreshtoken. This is almost the same procedure as we use to connect to az. We will start with the same variables as in part 2. Remember to keep your credential and secure, as it will give access to all your tenants.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"text codecolorer\">$app=get-credential # Get AppID and Key for out partnecenter app. (created in part 1)<br \/>\n$refreshtoken = 'refreshtoken' # From part 1 or whenever we get a new one.<br \/>\n$CustomerTenantID= 'Azure directory object id'<\/div><\/div>\n\n<\/pre>\n\n\n\n<p>Struggled for a while to get this to work. The important thing is the endpoints and when to use the customer tenant ID.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"text codecolorer\">$azureToken = New-PartnerAccessToken -Resource &quot;https:\/\/graph.microsoft.com\/&quot; -Credential $app -RefreshToken $refreshtoken -TenantId $CustomerTenantID <br \/>\n$graphToken = New-PartnerAccessToken -RefreshToken $refreshToken -Resource &quot;https:\/\/graph.windows.net\/&quot; -Credential $app -TenantId $CustomerTenantID &nbsp;<br \/>\n<br \/>\nConnect-Azuread -aadAccessToken $graphToken.AccessToken -msAccessToken $azureToken.AccessToken -TenantId $CustomerTenantID &nbsp;-AccountId $app.username<\/div><\/div>\n\n<\/pre>\n\n\n\n<p>So now you can use get-azureaduser to get users from this customer tenant.<\/p>\n\n\n\n<p>You could also use the MS online module msol to query for users, this module requires you to use tenantid as an argument.<\/p>\n\n\n\n<p>In part 4 I will wrap this up in a simple script allowing you to select customer tenant.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this part 3 of CSP and powershell I will show how you can connect to azureAD of a customer tenant using your CSP app credentials and refreshtoken. This is almost the same procedure as we use to connect to az. We will start with the same variables as in part 2. Remember to keep &hellip; <a href=\"https:\/\/www.vatland.no\/index.php\/csp-access-to-tenants-using-powershell-part-3\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CSP access to tenants using powershell. Part 3<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"CSP access to tenants using powershell. Part 3, AzureAD","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[72,74,52,48],"tags":[60,65,59,75],"class_list":["post-902","post","type-post","status-publish","format-standard","hentry","category-azure","category-csp","category-office365","category-powershell","tag-azure","tag-csp","tag-powershell","tag-refreshtoken"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"jetpack-related-posts":[{"id":914,"url":"https:\/\/www.vatland.no\/index.php\/csp-access-to-tenants-using-powershell-part-4\/","url_meta":{"origin":902,"position":0},"title":"CSP access to tenants using powershell. Part 4","author":"Atle","date":"September 24, 2019","format":false,"excerpt":"This is a small script that connects to partnercenter list all customers tenants and let you select one. When one is selected it connects to azuread and az for that customer. All my credentials are stored in SecretServer . I use a web service request to get those credentials. I\u2026","rel":"","context":"In &quot;Azure&quot;","block_context":{"text":"Azure","link":"https:\/\/www.vatland.no\/index.php\/category\/azure\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":899,"url":"https:\/\/www.vatland.no\/index.php\/csp-access-to-tenants-using-powershell-part-2\/","url_meta":{"origin":902,"position":1},"title":"CSP access to tenants using powershell. Part 2","author":"Atle","date":"September 20, 2019","format":false,"excerpt":"In part 1 we created the Azure Enterprise App for Partnercenter and used this information to connect using powershell and connect-partnercenter. Now we will use this to connect to one of our customers tenants. First we will use AZ module and connect-azaccount. We will use the AZ module and the\u2026","rel":"","context":"In &quot;Azure&quot;","block_context":{"text":"Azure","link":"https:\/\/www.vatland.no\/index.php\/category\/azure\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":875,"url":"https:\/\/www.vatland.no\/index.php\/csp-access-to-tenants-using-powershell-part-1\/","url_meta":{"origin":902,"position":2},"title":"CSP access to tenants using powershell. Part 1","author":"Atle","date":"September 18, 2019","format":false,"excerpt":"A short explanation of how to access customer tenant using a CSP tenant SPN credential connectiong to AzureAD and AZ. Have been struggling for a while to manage all our customers tenants using powershell scripts. It can be complicated to organize all the credentials, tenant domain, tenant id's password expiry.\u2026","rel":"","context":"In &quot;Azure&quot;","block_context":{"text":"Azure","link":"https:\/\/www.vatland.no\/index.php\/category\/azure\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":975,"url":"https:\/\/www.vatland.no\/index.php\/log-tenants-from-office-365-to-local-db\/","url_meta":{"origin":902,"position":3},"title":"Log tenants from office 365 to local Db","author":"Atle","date":"January 3, 2021","format":false,"excerpt":"Hi, I like to keep control of how many licenses our cutomers use versus how many\u00a0 have been purchased. Here is 1st part\u00a0 my PS script to copy the info from csp to the DB. I will start creating a database and table to keep a list of all the\u2026","rel":"","context":"In &quot;Azure&quot;","block_context":{"text":"Azure","link":"https:\/\/www.vatland.no\/index.php\/category\/azure\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1010,"url":"https:\/\/www.vatland.no\/index.php\/add-tenant-licenses-from-csp-to-database\/","url_meta":{"origin":902,"position":4},"title":"Add tenant licenses from csp to database.","author":"Atle","date":"January 17, 2021","format":false,"excerpt":"This is a followup from previus post. In this post I will populate the database with what licenses a tenant has aquired. This will add records of what aquired skus and 'usage'. For me this is how many license are bought versus how many are assigned. This does not account\u2026","rel":"","context":"In &quot;Azure&quot;","block_context":{"text":"Azure","link":"https:\/\/www.vatland.no\/index.php\/category\/uncategorized\/azure-uncategorized\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":990,"url":"https:\/\/www.vatland.no\/index.php\/get-tenantlistindb-from-previous-post\/","url_meta":{"origin":902,"position":5},"title":"Get-tenantlistindb (from previous post)","author":"Atle","date":"January 8, 2021","format":false,"excerpt":"Now that we have our tenants listed in a database table, listing them is quite easy. Added a switch that allow you to also list deleted\/removed tenants. [cc language=\"powershell\"] function get-tenantlistindb { param( [switch]$all ) $SQLInstance = \"localhost\\SQLExpress\" $SQLDatabase = \"Microsoft365\" $sqlqr = \"select * from [Microsoft365].[dbo].[tenants]\" if (-not $all)\u2026","rel":"","context":"In &quot;Azure&quot;","block_context":{"text":"Azure","link":"https:\/\/www.vatland.no\/index.php\/category\/azure\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/comments?post=902"}],"version-history":[{"count":7,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":910,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/902\/revisions\/910"}],"wp:attachment":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/media?parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/categories?post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/tags?post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}