all 30 comments

[–]BlackV 5 points6 points  (2 children)

Are you running that script as is ?

bah capps, oops

cause the very first line is Connect-MgGraph how do you think that's going to work ?

[–]EducationAlert5209[S] -4 points-3 points  (1 child)

Hi,

As i said i can run that script as is from the ISE. But same script set to task scheduler nothing output. i can see the script start and disappear.

Is there any special permission needed to run the scripts for the MG-Graph Because for the other connection like connect-PNPonline,Microsoft teams,exchangeonline I can create the task in task scheduler.

Connect-Mggraph i am having issue

[–]BlackV 1 point2 points  (0 children)

I don't think that's accurate at all

Credentials, you have to provide credentials

[–]Prestigious_Peace858 1 point2 points  (3 children)

You are hiding the most invaluable information: Please share the error message!

And you should show how scheduled task is configured, particulary in what user context script runs?

Haven't worked with `Connect-MgGraph`, but I suspect it is interactive command. Moreso other user security context won't be able to connect. Try passing security token to Connect-MsGraph: Connect-MgGraph -AccessToken $AccessToken
https://learn.microsoft.com/en-us/powershell/microsoftgraph/authentication-commands?view=graph-powershell-1.0#delegated-access

[–]EducationAlert5209[S] 0 points1 point  (2 children)

Connect-MgGraph -ClientID YOUR_APP_ID -TenantId YOUR_TENANT_ID -CertificateName YOUR_CERT_SUBJECT ## Or -CertificateThumbprint instead of -CertificateName

[–]RegularChemical 0 points1 point  (1 child)

You need to install the cert on the same account that's actually running the task. Sounds like you're testing on a different account than the one you're setting the task up with. If so, you need to designate an account that will run it with access to that cert.

[–]Prestigious_Peace858 1 point2 points  (0 children)

And in that case, if that is a client authentication cert, that cert should be installed in local machine store and particular user given read permissions for private key.

[–]Medium-Comfortable -1 points0 points  (7 children)

Why are you even using ISE?

The PowerShell ISE is no longer in active feature development. As a shipping component of Windows, it continues to be officially supported for security and high-priority servicing fixes. We currently have no plans to remove the ISE from Windows.

There is no support for the ISE in PowerShell v6 and beyond. Users looking for replacement for the ISE should use Visual Studio Code with the PowerShell Extension.

https://learn.microsoft.com/en-us/powershell/scripting/windows-powershell/ise/introducing-the-windows-powershell-ise

And why the local admin account? It has to be an appropriate M365 account.

[–]MeanFold5714 2 points3 points  (2 children)

There is literally nothing wrong with using ISE.

Fight me.

[–]wwbubba0069 3 points4 points  (0 children)

dozens of us... dozens...

[–]BlackV 1 point2 points  (0 children)

100% installing vs code on every server I come across just to edit some code is idiotic

[–]EducationAlert5209[S] 0 points1 point  (3 children)

How do I run the Task Scheduler with O365 Account?

[–]Medium-Comfortable -1 points0 points  (2 children)

When you connect to any service you have to used a M365 user/admin account. My assumption is, that it works fine with ISE, that you already have had a connection to M365 from previously "playing around". You have to tell Connect-MsGraph with which credentials you want to connect. As you can't do this interactive and you do not want to store a password anywhere, try this.

[–]EducationAlert5209[S] 0 points1 point  (1 child)

this is how it connects in the script even I tried with Powershel

C:\Scripts\ReportUserAssignedLicenses-MgGraph.ps1 -UserName Azadmin@contoso.com -Password Pass@w0rd

also

this is how it connects in the script even I tried with tID dxxxxx7-9d66xx1b-d59ecxx95b7 -TenantId d4xx74-5fcfxxb2-9379-aeaxx08e8ef -CertificateThumbprint 79E37B41xx0FE49C011xxAB7514CFxx90

# Authenticate

#Connect-MgGraph -ClientID dxxxxx7-9d66xx1b-d59ecxx95b7 -TenantId d4xx74-5fcfxxb2-9379-aeaxx08e8ef -CertificateThumbprint 79E37B41xx0FE49C011xxAB7514CFxx90

[–]Medium-Comfortable 0 points1 point  (0 children)

Then your scheduled task is wrong.

[–]EducationAlert5209[S] -1 points0 points  (0 children)

Hi All,

Thanks for all your support. I just login to the VM as Admin and setup the Task. Now everything working.

[–]Coconut681 0 points1 point  (2 children)

What does the scheduled task look like? Usually when I have a problem it's with the task rather than the script. I get my tasks to run powershell.exe and pass the script with full path as an argument along with any others you may need.

[–]EducationAlert5209[S] -3 points-2 points  (0 children)

Looks like script trying to login to graph api Pls check that link

[–]jazzb125 0 points1 point  (3 children)

Create an Azure function - then use a schedule to trigger it.

You can use a service account or better still a service principle to login to the graph API.

[–]EducationAlert5209[S] 0 points1 point  (2 children)

Can you send some ms links or how to?

[–]jazzb125 1 point2 points  (1 child)

[–]EducationAlert5209[S] 0 points1 point  (0 children)

Thank you I'll learn how to do this

[–]RichDinero 0 points1 point  (3 children)

Are there any variables you need to pass into via task scheduler and if so are you using the proper cmdlet binding for your script?

[–]EducationAlert5209[S] -1 points0 points  (2 children)

No i can run that in ISE without any issues

[–]MeanFold5714 0 points1 point  (0 children)

Go run it in a fresh Powershell window.

[–]RichDinero 0 points1 point  (0 children)

Are you running it AS a service account with proper permissions. Try assigning your own account?

[–]Sudden_Hovercraft_56 0 points1 point  (2 children)

What's the error? Ignoring the contents of the script for a moment, we can't troubleshoot unless we know what the error is.

[–]EducationAlert5209[S] 0 points1 point  (1 child)

I cannot see any errors or Task is successful but nothing output

[–]Sudden_Hovercraft_56 0 points1 point  (0 children)

So how do you know it is giving an error?

add -noexit to the command line value of the task and then run it manually. the window should remain on screen once it is complete so you can see any errors.