msgraph-sdk-powershell icon indicating copy to clipboard operation
msgraph-sdk-powershell copied to clipboard

Microsoft.Graph.Files not working in Automation Account

Open lucafrancescato opened this issue 1 year ago • 9 comments

Describe the bug

I am trying to use the module Microsoft.Graph.Files from an Automation Account PowerShell 7.2 script, but I get the following error when printing the error out:

The term 'Get-MgUserDrive' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Plus other errors that get printed, all of them are as follows:

OperationStopped: 
Line |
   3 |  … s' -Recurse -Include '*.ps1' -File | ForEach-Object { . $_.FullName }
     |                                                          ~~~~~~~~~~~~~
     | Exception of type 'System.OutOfMemoryException' was thrown.

I also tried with Get-MgDrive and I get the same error.

Expected behavior

The expected behavior is that the cmdlet works fine, returning the expected result, since I did install the required modules for the required runtime version, i.e. Microsoft.Graph.Authentication 2.19.0 and Microsoft.Graph.Files 2.19.0 for PowerShell 7.2.

How to reproduce

In an Automation Account:

  1. install module Microsoft.Graph.Authentication version 2.19.0 for PowerShell 7.2
  2. install module Microsoft.Graph.Files version 2.19.0 for PowerShell 7.2
  3. create a runbook for PowerShell 7.2
  4. execute Get-MgUserDrive in the runbook

SDK Version

2.19.0

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

Click to expand log ```
</details>


### Configuration

Found in Automation Account

### Other information

Found using PowerShell 7.2

lucafrancescato avatar May 23 '24 15:05 lucafrancescato

Hi @lucafrancescato ,

Thanks for reporting the issue! A few clarifying questions:

How did you install the module in Azure Automation, as described for example here? https://practical365.com/microsoft-graph-sdk-powershell-azure-automation/

Does the issue affect only the Get-MgUserDrive cmdlet or any other one as well?

Is the issue specific to Azure Automation runbooks, meaning you can install the cmdlet and use it locally without problems?

petrhollayms avatar May 27 '24 08:05 petrhollayms

Hi @petrhollayms,

I confirm I installed the modules as described in the provided link, i.e. leveraging the "Add a module" button from the interface provided in the Azure portal.

I also tried using Get-MgDrive -DriveId $driveId and Get-MgShareItem -SharedDriveItemId $itemId, and I got the same error, as if the module wasn't there.

If I try in a local environment using a user account with Files.Read.All permissions, I have no issues in running the Get-MgUserDrive cmdlet:

  • PowerShell 7.4.2 (Core)
  • Microsoft.Graph.Files 2.19.0

lucafrancescato avatar May 27 '24 10:05 lucafrancescato

Hi @lucafrancescato ,

This looks like a memory limitation of the Automation environment, could you please try to increase the memory limit? See also https://github.com/MicrosoftDocs/azure-docs/blob/main/includes/azure-automation-service-limits.md

I am assuming you've assigned permissions to your Automation Acccount, can you please confirm?

Also, could you please attach the output when running the cmdlet with the -Debug parameter? See https://learn.microsoft.com/en-us/powershell/microsoftgraph/troubleshooting?view=graph-powershell-1.0#using--debug

petrhollayms avatar May 27 '24 16:05 petrhollayms

Hi @petrhollayms, thanks for your help.

Permissions are correctly set, as I can see from the output of Disconnect-MgGraph:

...
Scopes: {Files.Read.All}
...

Regarding the memory limitation, I am not sure since there is nothing running in the Automation Account besides the code I tried to run with the Microsoft.Graph.Files module. Even if it was the case, I would not expect an error saying it cannot find the cmdlet.

With -Debug, I did not see anything new in the output. The full script I'm trying to run is:

try {
    Connect-MgGraph -Identity
    Get-MgUserDrive -UserId "some-user-id" -Debug
} catch {
    Write-Output $Error
} finally {
    Disconnect-MgGraph
}

The output is (omitting Connect-MgGraph -Identity and Disconnect-MgGraph outputs):

Get-MgUserDrive: 
Line |
   3 |      Get-MgUserDrive -UserId "some-user-id" -Debug
     |      ~~~~~~~~~~~~~~~
     | The term 'Get-MgUserDrive' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

OperationStopped: 
Line |
   3 |  … s' -Recurse -Include '*.ps1' -File | ForEach-Object { . $_.FullName }
     |                                                          ~~~~~~~~~~~~~
     | Exception of type 'System.OutOfMemoryException' was thrown.

OperationStopped: 
Line |
   3 |  … s' -Recurse -Include '*.ps1' -File | ForEach-Object { . $_.FullName }
     |                                                          ~~~~~~~~~~~~~
     | Exception of type 'System.OutOfMemoryException' was thrown.

OperationStopped: Exception of type 'System.OutOfMemoryException' was thrown.

OperationStopped: 
Line |
   3 |  … s' -Recurse -Include '*.ps1' -File | ForEach-Object { . $_.FullName }
     |                                                          ~~~~~~~~~~~~~
     | Exception of type 'System.OutOfMemoryException' was thrown.

OperationStopped: 
Line |
   3 |  … s' -Recurse -Include '*.ps1' -File | ForEach-Object { . $_.FullName }
     |                                                          ~~~~~~~~~~~~~
     | Exception of type 'System.OutOfMemoryException' was thrown.

I might try directly with Invoke-MgGraphRequest and see what happens. What do you think?

lucafrancescato avatar May 27 '24 20:05 lucafrancescato

Hi @petrhollayms,

a quick update: I tried using Invoke-MgGraphRequest and it worked! I guess there's something wrong with the imported module Microsoft.Graph.Files.

Code:

try {
    Connect-MgGraph -Identity
    $userId = "some-user-id"
    $uri = "/v1.0/users/$userId/drives"
    $method = "GET"
    $res = Invoke-MgGraphRequest -Uri $uri -method $method
    Write-Output $res
} catch {
    Write-Output $Error
} finally {
    Disconnect-MgGraph
}

lucafrancescato avatar May 28 '24 09:05 lucafrancescato

Hi @lucafrancescato ,

Thank you for confirming that the workaround with Invoke-MgGraphRequest works.

We will try to reproduce it in our environment but this will take some time, so please keep using Invoke-MgGraphRequest for the time being and we keep this issue here open until we figured out the right solution for it, OK?

Similar issue also is #2516 where an alternative setup with using a hybrid worker was mentioned as the sandbox one has a memory limit of 400 MB, see https://learn.microsoft.com/en-us/azure/automation/automation-runbook-execution and https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/azure-subscription-service-limits#automation-limits

petrhollayms avatar May 28 '24 10:05 petrhollayms

All clear, thank you very much Petr.

lucafrancescato avatar May 28 '24 11:05 lucafrancescato

@petrhollayms Any news about this issue? I don't want to use a hybrid-worker or any dirty workarounds! I think this is a major issue and should be fixed asap.

Jan1503 avatar Jul 06 '24 11:07 Jan1503

Hello, just wanted to chime in that I experienced the same bug in an Automation Account in a PS 7.2 runbook (Tried version 2.23 and 2.19). I tried importing the Microsoft.Graph.Files module in a script on my local computer and that task allocated up to 2 gb of ram, so it could be that some files are being added multiple times or perhaps even recursively.

I also ended up using Invoke-MgGraphRequest as a workaround.

faitdelamer avatar Sep 26 '24 11:09 faitdelamer

Hi, seems this issue is still present in 2.24.0, using a PS7.2 runbook. Now using Invoke-MgGraphRequest as a workaround.

kloosterj avatar Nov 15 '24 08:11 kloosterj

Still present in 2.25.0 in both PS 5.1 and 7.2 runbooks. Any update?

skadann avatar Dec 20 '24 20:12 skadann

Closing this as a duplicate of https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/2516. The issue is currently under investigation. Kindly let's all track it from there.

timayabi2020 avatar Feb 10 '25 13:02 timayabi2020

Hello, just wanted to chime in that I experienced the same bug in an Automation Account in a PS 7.2 runbook (Tried version 2.23 and 2.19). I tried importing the Microsoft.Graph.Files module in a script on my local computer and that task allocated up to 2 gb of ram, so it could be that some files are being added multiple times or perhaps even recursively.

I also ended up using Invoke-MgGraphRequest as a workaround.

Can you please share an example script using this alternative method? I am not a Graph API programmer. Thx!

RobAaldijk avatar Jul 24 '25 08:07 RobAaldijk

@RobAaldijk You can make plain restful calls with this::

$userId = '[email protected]' $userDrive = Invoke-MgGraphRequest -Method GET -Uri "v1.0/users/$userId/drive" -OutputType PSObject

$userDrive $userDrive.quota.used

And yes, for anyone future reading there is still a memory limitation within an Automation account for this library. CommandType Name Version Source


Function Get-MgUserDrive 2.30.0 Microsoft.Graph.Files

dwarfered avatar Sep 20 '25 00:09 dwarfered