{"id":914,"date":"2019-09-24T07:25:26","date_gmt":"2019-09-24T06:25:26","guid":{"rendered":"http:\/\/www.vatland.no\/?p=914"},"modified":"2019-09-24T07:42:51","modified_gmt":"2019-09-24T06:42:51","slug":"csp-access-to-tenants-using-powershell-part-4","status":"publish","type":"post","link":"https:\/\/www.vatland.no\/index.php\/csp-access-to-tenants-using-powershell-part-4\/","title":{"rendered":"CSP access to tenants using powershell. Part 4"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>All my credentials are stored in <a href=\"https:\/\/thycotic.com\/products\/secret-server\/\">SecretServer<\/a> . I use a web service request to get those credentials. I will show these modules in a later post. <\/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;height:300px;\"><div class=\"text codecolorer\">import-module partnercenter<br \/>\n<br \/>\n#$app = AppID + AppKey<br \/>\n$app=Get-SecretID -secretID xxxx<br \/>\n#rt = refreshtoken.<br \/>\n$rt=Get-SecretID -secretID yyyy<br \/>\n$refreshtoken=$rt.GetNetworkCredential().Password<br \/>\n$tid=&quot;'csp tenant directory id'&quot;<br \/>\n<br \/>\n&nbsp; &nbsp;<br \/>\n&nbsp; &nbsp;<br \/>\n#Connect PartnerCenter<br \/>\n$pcToken = New-PartnerAccessToken -RefreshToken $refreshToken -Resource https:\/\/api.partnercenter.microsoft.com -Credential $app -TenantId $tid<br \/>\nConnect-PartnerCenter -AccessToken $pcToken.AccessToken -ApplicationId $app.username -TenantId $tid <br \/>\n<br \/>\n&nbsp; &nbsp; $customers=get-partnercustomer | sort-object -Property name<br \/>\n&nbsp; &nbsp; $counter=0<br \/>\n&nbsp; &nbsp; foreach($cust in $customers){<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;$($counter) - $($cust.Name) - $($cust.domain)&quot;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; $counter++<br \/>\n&nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; $custid=Read-Host &quot;Enter customer #&quot;<br \/>\n&nbsp; &nbsp; $customer=$customers[$custid]<br \/>\n&nbsp; &nbsp; Write-host &quot; Targeting : $($customer.name) - $($customer.Domain)&quot;<br \/>\n<br \/>\n$azureToken = New-PartnerAccessToken -Resource &quot;https:\/\/graph.microsoft.com\/&quot; -Credential $app -RefreshToken $refreshtoken -TenantId $customer.CustomerId <br \/>\n$graphToken = New-PartnerAccessToken -RefreshToken $refreshToken -Resource &quot;https:\/\/graph.windows.net\/&quot; -Credential $app -TenantId $customer.CustomerId &nbsp;<br \/>\n<br \/>\nConnect-Azuread -aadAccessToken $graphToken.AccessToken -msAccessToken $azureToken.AccessToken -TenantId $customer.CustomerId &nbsp;-AccountId $app.username<br \/>\n<br \/>\n<br \/>\n$azure2Token = New-PartnerAccessToken -Resource https:\/\/management.azure.com\/ -Credential $app -RefreshToken $refreshtoken -TenantId $customer.CustomerId <br \/>\n$graph2Token = New-PartnerAccessToken -RefreshToken $refreshToken -Resource https:\/\/graph.windows.net\/ -Credential $app -TenantId $customer.CustomerId &nbsp;<br \/>\n<br \/>\n&nbsp;Connect-AzAccount -AccessToken $azure2Token.AccessToken -GraphAccessToken $graph2Token.AccessToken -TenantId $customer.CustomerId &nbsp;-AccountId $app.username<\/div><\/div>\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 will show these modules in &hellip; <a href=\"https:\/\/www.vatland.no\/index.php\/csp-access-to-tenants-using-powershell-part-4\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CSP access to tenants using powershell. Part 4<\/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":"","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,48],"tags":[60,77,65,59,75],"class_list":["post-914","post","type-post","status-publish","format-standard","hentry","category-azure","category-csp","category-powershell","tag-azure","tag-azuread","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":899,"url":"https:\/\/www.vatland.no\/index.php\/csp-access-to-tenants-using-powershell-part-2\/","url_meta":{"origin":914,"position":0},"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":914,"position":1},"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":914,"position":2},"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":902,"url":"https:\/\/www.vatland.no\/index.php\/csp-access-to-tenants-using-powershell-part-3\/","url_meta":{"origin":914,"position":3},"title":"CSP access to tenants using powershell. Part 3","author":"Atle","date":"September 23, 2019","format":false,"excerpt":"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\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":914,"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":957,"url":"https:\/\/www.vatland.no\/index.php\/hash-tables-in-powershell\/","url_meta":{"origin":914,"position":5},"title":"Hash tables in powershell","author":"Atle","date":"March 23, 2020","format":false,"excerpt":"We all have the need to store data in some kind of arrays. I use hashtables a lot. Preferred use is as a lookup table, I can use 'contains' instead of looping through each item or reference an object by name instead of index number. Lookup table for licenses is\u2026","rel":"","context":"In &quot;CSP&quot;","block_context":{"text":"CSP","link":"https:\/\/www.vatland.no\/index.php\/category\/csp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/914","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=914"}],"version-history":[{"count":4,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/914\/revisions"}],"predecessor-version":[{"id":918,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/914\/revisions\/918"}],"wp:attachment":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/media?parent=914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/categories?post=914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/tags?post=914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}