{"id":4561,"date":"2025-06-30T13:19:40","date_gmt":"2025-06-30T12:19:40","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=4561"},"modified":"2025-06-30T13:19:40","modified_gmt":"2025-06-30T12:19:40","slug":"converting-onedrive-url-to-upn","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2025\/06\/converting-onedrive-url-to-upn\/","title":{"rendered":"Converting onedrive url to UPN"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Had a bit of a struggle converting onedrive for business url&#8217;s <strong>reliably<\/strong> to UPN&#8217;s. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since a path might contain \/personal\/ multiple times, and we have those pesky national tenants with special url&#8217;s to deal with&#8230;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So thought I&#8217;d share:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Get-UPNFromOnedriveUrl{\n    Param(\n        &#91;string]$url\n    )\n\n    $userName = ''\n    if ($url -match ':\\\/\\\/.*?\\..*?\\\/personal\\\/(.*?)\\\/') {\n        $userName = $matches&#91;1]\n    }else {\n        Throw \"Invalid OneDrive URL format: $url\"\n    }\n\n    if(!$global:tenantODStyleDomains){\n        $global:tenantODStyleDomains = New-GraphQuery -Uri \"https:\/\/graph.microsoft.com\/v1.0\/domains\" -Method GET -resource \"https:\/\/graph.microsoft.com\" | ForEach-Object { $_.id.replace(\".\",\"_\") }\n    }\n\n    foreach($tenantODStyleDomain in $global:tenantODStyleDomains){\n        if($userName -like \"*_$tenantODStyleDomain\"){\n            $prefix = $userName.Replace(\"$tenantODStyleDomain\",\"\") \n            $suffix = $tenantODStyleDomain.Replace(\"_\",\".\")\n            break\n        }\n    }\n\n    return \"$prefix@$suffix\".Replace(\"_\",\"\").ToLower()\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Had a bit of a struggle converting onedrive for business url&#8217;s reliably to UPN&#8217;s. Since a path might contain \/personal\/ multiple times, and we have those pesky national tenants with special url&#8217;s to deal with&#8230; So thought I&#8217;d share:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[24,39],"tags":[],"class_list":["post-4561","post","type-post","status-publish","format-standard","hentry","category-m365permissions","category-powershell"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/4561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=4561"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/4561\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=4561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=4561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=4561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}