Forum Discussion
rblocker
Mar 04, 2025Copper Contributor
Function app script stopped working
I have a simple function app that uses this script to shut down inactive AVD VMs that had worked for about two years simply stopped working. Instead, it produce this error:
ERROR: Error stopping the VM: GenericArguments[0], 'Microsoft.Azure.Management.Compute.Models.VirtualMachine', on 'System.Nullable`1[T] MaxInteger[T](System.Collections.Generic.IEnumerable`1[System.Nullable`1[T]])' violates the constraint of type 'T'.
After much troubleshooting and searching for something relevant, I discovered that the function app's host.json file was set to:
{
"version":"2.0",
"managedDependency": {
"Enabled": true
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0]"
}
}
Since v.2 and v.3 are [no longer supported](http://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=isolated-process%2Cv4&pivots=programming-language-powershell), I changed it to
{
"version":"2.0",
"managedDependency": {
"Enabled": true
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.0.0, 5.0.0]"
}
}
```
I also noticed that the `requirements.psd1` file was set to
# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
# For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'.
# To use the Az module in your function app, please uncomment the line below.
'Az' = '8.*'
}
So I changed it to
# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
# For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'.
# To use the Az module in your function app, please uncomment the line below.
'Az' = '13.*'
}
None of this helped. The same error was generated.
I then realized I might need to restart the app, which I did.
After this, I see this warning and error:
2025-03-03T21:42:45Z [Warning] The first managed dependency download is in progress, function execution will continue when it's done. Depending on the content of requirements.psd1, this can take a few minutes. Subsequent function executions will not block and updates will be performed in the background.
2025-03-03T21:42:45Z [Error] Executed 'Functions.TwelveMinuteTimerTrigger' (Failed, Id=58b46678-c1d2-4ca8-8083-fab1e657c608, Duration=95ms)
After waiting a while for the download to finish, I get this error:
[Error] ERROR: Error getting a list of user sessions: The term 'Get-AzWvdUserSession' 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.
The line "Get-AzWvdUserSession is not recognized" seems to indicate that the download of the Az modules has failed. Does anyone know what would cause the download to fail and how to fix it?
Would suggest taking a look at below area:
- Managed Dependency Download Issue:
- The warning about the managed dependency download suggests that the required modules (like Az) might not have been downloaded successfully. This could be due to network restrictions or a temporary issue with the Azure Functions service.
- Solution: Ensure that your function app has access to the internet and that there are no firewall rules blocking the download. You can also try manually installing the required modules locally and deploying them with your function app.
- Version Compatibility:
- You've updated the host.json and requirements.psd1 files to use newer versions, but there might still be compatibility issues with the Az module or the specific cmdlet Get-AzWvdUserSession.
- Solution: Verify that the Az module version 13.* includes the Get-AzWvdUserSession cmdlet. If not, you might need to install a specific version of the Az module that supports this cmdlet.
- Cmdlet Recognition Issue:
- The error indicates that the Get-AzWvdUserSession cmdlet is not recognized. This could happen if the module containing this cmdlet is not loaded properly.
- Solution: Add an explicit Import-Module Az.DesktopVirtualization statement in your script to ensure the required module is loaded.
- Function App Restart:
- Restarting the function app was a good step, but sometimes a full redeployment is necessary to ensure all changes are applied.
- Solution: Redeploy your function app after making the changes to host.json and requirements.psd1.
- Debugging Locally:
- To isolate the issue, you can try running the script locally in a PowerShell session with the same Az module version. This can help identify if the problem is with the function app environment or the script itself.
- Managed Dependency Download Issue: