How to move Powershell code running in Azure Webhook to C# application

Mohit kumar 0 Reputation points
2023-11-28T07:29:57.7333333+00:00

I have a powershell script running in Azure Automation as webhook. That has this commands as shown below. I want to run this using C# instead, so that I can track and log if something goes wrong. However, I need to understand, what does the Get-AutomationPSCredential actually does.

$myCred = Get-AutomationPSCredential -Name "ABC"
$userName = $myCred.UserName
$securePassword = $myCred.Password
$password = $myCred.GetNetworkCredential().Password

Write-Output $userName
Write-Output "Credentials Get Success"

Connect-ExchangeOnline -credential $myCred
Write-Output "Exchange Connected"

if ($addorremove -eq "Add") {
    Add-RecipientPermission $user -AccessRights SendAs -Trustee $trustee -confirm:$false
	Write-Output "User Add Successfully"
}
elseif ($addorremove -eq "Remove") {
    Remove-RecipientPermission $user -AccessRights SendAs -Trustee $trustee -confirm:$false
	Write-Output "User Removed Successfully"
}
Disconnect-ExchangeOnline -confirm:$false
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,980 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,273 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,110 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Swathi Dhanwada 18,781 Reputation points Microsoft Employee
    2023-11-28T10:25:54.2733333+00:00

    @Mohit kumar The Get-AutomationPSCredential cmdlet is used in Azure Automation to retrieve a credential asset that has been created in the Azure Automation account. The credential asset is a secure way to store and retrieve credentials that are used in runbooks or other automation tasks.

    When you create a credential asset in Azure Automation, you can specify a username and password that are used to authenticate to a target system or service. The credential asset is then stored securely in the Azure Automation account and can be retrieved using the Get-AutomationPSCredential cmdlet.

    The Get-AutomationPSCredential cmdlet returns a PSCredential object that contains the username and password for the credential asset. This PSCredential object can then be used in PowerShell commands or scripts to authenticate to the target system or service.

    For example, if you have a credential asset named "MyCredential" in your Azure Automation account, you can retrieve it using the following command:

    $credential = Get-AutomationPSCredential -Name "MyCredential"
    

    This command retrieves the "MyCredential" credential asset and stores it in the $credential variable as a PSCredential object. You can then use this $credential object in your PowerShell commands or scripts to authenticate to the target system or service.

    I hope this helps!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.