Cannot connect to MG-Graph with script

Bühler Gabriel 76 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

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,098 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,225 questions
0 comments No comments
{count} votes