New-AzurePIMRequest as .net-function?

Viewed 50

I want to initiate an Azure-PIM using c#/.net

I already found a PowerShell-Function to do this: New-AzurePIMRequest ... inside the "PIMTools"-packages: https://www.powershellgallery.com/packages/PIMTools/0.4.0.0

This is working just fine and of course I could just execute a PS-Script containing this from within my C#-Application. But I would prefer to natively achieving the same using a NuGet-Package or a library from within my application.

Is there a package that allows me to achieve the same from within C#?

1 Answers

Those PIMTools are just wrapping some existing powershell modules.You can check the details here: https://github.com/janegilring/PIMTools/blob/main/functions/New-AzurePIMRequest.ps1

As you can see they mainly use the AzureADPreview module which is giving access to the Microsoft Graph Endpoint. Microsoft Graph is a RESTful web API that enables you to access Microsoft Cloud service resources.

You can find the Graph SDK here: https://github.com/microsoftgraph/msgraph-sdk-dotnet

With the SDK installed you can use something like this to issue a PIM Request:

    var graphClient = new GraphServiceClient(new DefaultAzureCredential());

    var privilegedRoleAssignmentRequest = new PrivilegedRoleAssignmentRequestObject
    {
        Duration = "2",
        Reason = "DevWork",
        AssignmentState = "Active",
        RoleId = "b24988ac-6180-42a0-ab88-20f7382dd24c",
    };

    await graphClient.PrivilegedRoleAssignmentRequests
        .Request()
        .AddAsync(privilegedRoleAssignmentRequest);

Note: You might have to the use the /beta Endpoint of the SDK to get PIM working. However, APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs in production applications is not supported.

Related