2

I'm playing around with Azure Functions, specifically a PowerShell script function. I'm wondering how I can use a script that is reaching out to SharePoint Online.

To run against SharePoint Online I would normally use the "SharePoint Online Management Shell" which is a version of PowerShell that pre-loads the SharePoint Online library so that I can use methods like Get-SPOSite, etc.

How can I include this library in my Azure Function PowerShell script so that I can use these functions? I'm assuming I need to load the library at the top of my script, but how do I load the library?

I've uploaded the DLL into my function and am trying to use:

[System.Reflection.Assembly]::LoadFrom('Microsoft.Online.SharePoint.Client.Tenant.dll')

But this doesn't appear to work. I think my issue is that I don't know the absolute path to my uploaded file. This is what I see on the left hand pane:

enter image description here

But I don't know what the path of these files are.

Any ideas? Documentation is frustratingly slim with the Azure Functions at the minute.

Ray Hogan
  • 319
  • 3
  • 12

2 Answers2

5

The path to your Azure Functions directory is

D:\home\site\wwwroot\<YourFunctionName>

In the most recent Azure Functions release (version 0.3) we support loading DLLs on your behalf. You will need to create a folder named modules and upload the DLL into that folder. In fact, you are now be able to upload script (.psm1), binary (.dll) and manifest (.psd1) modules into the modules folder and they will be automatically loaded before executing the script.

Let's use the sample MyMathLib assembly as a reference.

Suppose you have a Function named RunSimplePowerShell, and have uploaded the custom library named MyMathLib.dll into the folder as follows,

D:\home\site\wwwroot\RunSimplePowerShell\modules\MyMathLib.dll

Then, your PowerShell script named run.ps1 that resides in

D:\home\site\wwwroot\RunSimplePowerShell\run.ps1

could be written as follows,

[MyMathLib.Methods]::Sum(5, 2)

$calculatorInstance= New-Object MyMathLib.Methods
$calculatorInstance.Product(5,2)

With Azure Functions release 0.3, you can now skip the line

[Reflection.Assembly]::LoadFile("D:\home\site\wwwroot\RunSimplePowerShell\MyMathLib.dll")
Community
  • 1
  • 1
Ling Toh
  • 2,404
  • 1
  • 16
  • 24
1

Figured it out, somewhat.

To find the absolute path of your files you can do so by accessing the console in Azure Functions. To get to the console requires a bit of clicking about. From inside your Azure Function:

Function app settings -> Advanced Settings -> Go to App Service Settings -> Tools -> Console

You'll see the directory name in the command prompt. You can then use:

[Reflection.Assembly]::LoadFile("<directory>\MyLibraryNameHere.dll")

To pull in your assembly.

Ray Hogan
  • 319
  • 3
  • 12