microsoftgraph / microsoftgraph/msgraph-sdk-powershell

Set-MgDriveItemContent doesn't work with Azure Automation 7.2 runbook

Open
#3,112 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Environment: Automation runbooks Status: Needs Investigation type:bug
Dominant language
C#
Stars
898
Forks
230
Avg merge
2d 5h
Merged PRs (30d)
31

Description

Describe the bug

I created an Azure Automation PowerShell 7.2 runbook to add a file to a SharePoint site. The Microsoft.Graph.Files module (7.2) is loaded as a resource into the automation account. The runbook authenticates with a managed identity and can successfully use other SharePoint cmdlets like Get-MgSiteDrive and Get-MgSite for the target site, so we know that SDK cmdlets work for the target site. However, when the runbook attempts to run Set-MgDriveItemContent, the error is that the cmdlet cannot be found.

Image

The same command works perfectly with an interactive Graph SDK session (delegated and app-only).

$NewFile = Set-MgDriveItemContent -DriveId $DocumentsDrive.Id -DriveItemId $TargetFile -InFile $File

The Graph API PUT request works perfectly too.

$Uri = ("https://graph.microsoft.com/V1.0/sites/{0}/drive/items/root:/General/{1}:/content" -f $Site.Id, $ExcelOutputFile)
$NewFile = Invoke-MgGraphRequest -uri $uri -Method PUT -InputFilePath $ExcelOutputFile

Expected behavior

I expect the cmdlet to be available in an Azure Automation runbook when the module is loaded into the automation account.

How to reproduce

Create an automation account.
Add the necessary Graph SDK modules, including Microsoft.Graph.Files as account resources
Create a runbook with some code to authenticate using a managed identity and then upload a file to SharePoint with Set-MgDriveFileItemContent. Here's some example code.

$SiteUri = "https://office365itpros.sharepoint.com/sites/Office365Adoption"
$SiteId = $SiteUri.Split('//')[1].split("/")[0] + ":/sites/" + $SiteUri.Split('//')[1].split("/")[2]
$Site = Get-MgSite -SiteId $SiteId
If (!$Site) {
Write-Output ("Unable to connect to site {0} with id {1}" -f $Uri, $SiteId)
Exit
}
[array]$Drives = Get-MgSiteDrive -SiteId $Site.Id
$DocumentsDrive = $Drives | Where-Object {$_.Name -eq "Documents"}
$TargetFile = "root:/General/" + $ExcelOutputFile + ":"

$NewFile = Set-MgDriveItemContent -DriveId $DocumentsDrive.Id -DriveItemId $TargetFile -InFile $File

SDK Version

2.25

Latest version known to work for scenario above?

Unknown

Known Workarounds

Use the Graph API request instead.

Debug output

Runs in Azure Automation and as the cmdlet can't be found, no debug can be output

Configuration

No response

Other information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Azure Automation PowerShell 7.2 runbook with Microsoft.Graph.Files loaded, then compare cmdlet availability with the working interactive session. Check whether Set-MgDriveItemContent is present in the automation account resource and whether the documented Set-MgDriveFileItemContent example differs. Done means the cmdlet is available in the runbook or the limitation and workaround are documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, powershell
Domain
cloud, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.