Cannot connect to MG-Graph with script

Bühler Gabriel 81 Reputation points
2024-05-22T14:43:14.5333333+00:00

Hey Guys

I created a Script that should automatically assign a License to a specific User and then also enable MFA. Here is the sript:

# Install and import the required modules
<#Install-Module -Name Microsoft.Graph -Scope CurrentUser -AllowClobber
Install-Module -Name AzureAD -Scope CurrentUser
Import-Module Microsoft.Graph
Import-Module AzureAD
#>
# Connect to MSOlService
#Import-Module MSOnline
Connect-MsolService
# Connect to Microsoft Graph
Connect-MgGraph
# Connect to Azure Account
#Import-Module Az
Update-AzConfig -DefaultSubscriptionForLogin 8a7d513c-2bc5-42ac-a652-585171248245
Connect-AzAccount
# Tenant and Usernames
$tenantID = "TENANTID"
$username = read-host "Enter the username"
$usageLocation = "CH"
Set-MgUser -UserId $username -UsageLocation $usageLocation
# Get E3 SKU
$e3Sku = Get-MgSubscribedSku -All | Where-Object { $_.SkuPartNumber -eq 'ENTERPRISEPACK' }
# Assign the license to the user
$licenseSkuId = $e3Sku.SkuId
# Create an array of license objects
$licensesToAdd = @(
    @{
        "SkuId" = $licenseSkuId
    }
)
# Convert to the required type
$licensesToAdd = $licensesToAdd | ForEach-Object {
    $license = New-Object -TypeName Microsoft.Graph.PowerShell.Models.MicrosoftGraphAssignedLicense
    $license.SkuId = $_.SkuId
    $license
}
# Set the user license
Set-MgUserLicense -UserId $username -AddLicenses $licensesToAdd -RemoveLicenses @()
# Set Strong Authentication Requirement
$st = New-Object -TypeName PSObject
$st | Add-Member -MemberType NoteProperty -Name "RelyingParty" -Value "*"
$st | Add-Member -MemberType NoteProperty -Name "State" -Value "Enabled"
$st | Add-Member -MemberType NoteProperty -Name "RememberDevicesNotIssuedBefore" -Value (Get-Date)
# Ensure the AzureAD module is imported
Import-Module AzureAD
# Set Strong Authentication Requirements for the user
Set-AzureADUser -ObjectId $username -StrongAuthenticationRequirements @($st)

The issue is that I cannot connect to graph. I already uninstalled and reinstalled the module, I also tried it on a different device with a newly installed graph module and I also installed "Microsoft.Identity.Client". But i Still get this error:

new machine, this error. Why? Connect-MgGraph : InteractiveBrowserCredential authentication failed: Method not found: '!0 Microsoft.Identity.Client.AbstractAcquireTokenParameterBuilder`1.WithTenantIdFromAuthority(System.Uri)'. At C:\Users\AdmGBU101\Documents\Untitled1.ps1:12 char:1 + Connect-MgGraph + ~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Connect-MgGraph], AuthenticationFailedException + FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Authentication.Cmdlets.ConnectMgGraph

Do you have an Idea what I could try to solve this?

Thank you for your help.

Kind regards,

Gabriel

Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sindhuja Lingaswaran 5 Reputation points
    2024-10-08T15:26:04.28+00:00

    I had to Install-Module Microsoft.Graph as an Administrator in Powershell, but it gave me this error because it could not start the browser for authentication as Administrator. For Connect-MgGraph I had to run in a non-Admin session.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.