Azure / Azure/azure-powershell
[Feature]: Expose SdkModels
- Dominant language
- C#
- Stars
- 4.8k
- Forks
- 4.3k
- Avg merge
- 3d 14h
- Merged PRs (30d)
- 54
Description
### Description of the new feature
We've made a concerted effort to utilize classes significantly in our workloads and it has yielded great success.
One pain point is that it's very easy to lose type information in powershell (or sometimes it seems, impossible). Exposing SDK types in AzModule, MsGraph module, etc would be highly beneficial.
The below code works completely fine with intelisense, and provides amazing visibility while developing, but it does not work at runtime.
```powershell
function Get-ResourceType([Microsoft.Azure.Commands.Resources.Models.PSResource] $Resource)
{
# Will return the ResourceType of the AzResource
#Examples: ResourceGroup, StorageAccount, SqlServer, KeyVault, etc
#using $Resource in this block gives great typeinfo support.
}
# Any type of AzResource,
$AzResources = @();
foreach ($resource in $AzResources)
{
$resourceType = Get-ResourceType -Resource $resource
if ($resourceType -eq "ResourceGroup")
{
$resourceGroup = [Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup] $resource;
# Get great typeinfo support now from $resourceGroup
$typeInfoWorks = $resourceGroup.ResourceGroupName;
}
# more switching on resourceType
}
```
```config
Unable to find type [Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup].
```
As a workaround, we've been doing the following so we can "toggle on/off" typing.
```powershell
function Get-ResourceType(
# Toggle this on and the other arg off to get intellisense support.
# [Microsoft.Azure.Commands.Resources.Models.PSResource]
# $Resource
[PSCustomObject]
$Resource
)
{
# Will return the ResourceType of the AzResource
#Examples: ResourceGroup, StorageAccount, SqlServer, KeyVault, etc
}
# Any type of AzResource,
$AzResources = @();
foreach ($resource in $AzResources)
{
$resourceType = Get-ResourceType -Resource $resource
if ($resourceType -eq "ResourceGroup")
{
# Toggle this on and the other arg off to get intellisense support.
# $resourceGroup = [Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup] $resource;
$resourceGroup = $resource;
# No more intellisense support ;(
$typeInfoWorks = $resourceGroup.ResourceGroupName;
}
# more switching on resourceType
}
```
Oddly enough, some types work, sometimes... (though, none from Az that I've found).
For example, it seems that if Graph is loaded once, [Microsoft.Graph.PowerShell.Models.MicrosoftGraphGroup] is available to use at runtime. But it always fails the first time, despite force calling Import-Module Microsoft.Graph or Connect-MgGraph, or tying to use "using".
I see that the .dlls for the SdkModels are denoted as "private." Is there a way we can get some sdk model base that we can start to use in greenfield powershell projects?
### Proposed implementation details (optional)
_No response_
Contributor guide
Research direction
Start by tracing how AzModule and the Microsoft Graph module load and expose their SDK model DLLs, especially the models currently marked private. Compare the first-use behavior with explicit Import-Module and Connect-MgGraph calls. Done means the supported SDK model types are consistently available at runtime for greenfield PowerShell projects while retaining the described type information.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, powershell
- Domain
- developer-experience, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100