'How to connect to sharepoint admin using CSOM PowerShell

I know using SPO-Service and PnP Module to connect to SharePoint Admin Site might be the best solution but I can't use those two module in our Remote Desktop Server because of conditional access policy and not domain joined issue so just wondering if there's a different method to connect,

I'm trying to use CSOM method but not really sure how to connect to the SharePoint Admin. I seen a few example online but they are trying to connect to their personal SharePoint Site instead of the SharePoint Admin Site.

Basically what I'm trying to do is to be able to connect to SharePoint Admin Site and then lookup if a particular personal SP/OD site is existed in our tenant.

$adminUPN=""
$pwd = ConvertTo-SecureString " " -AsPlainText -Force

$siteUrl = "https://company-admin.sharepoint.com/"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adminUPN, $pwd)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.ExecuteQuery()

Also some reason I'm getting this error "The remote server returned an error: (403) Forbidden." even though I already activated my PIM role and have access to the Admin site.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source