Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to pass my account credentials to Power shell script through C#.net in winforms

440 views
Skip to first unread message

sudeep gaadhe

unread,
Aug 7, 2020, 6:42:08 PM8/7/20
to
Hi, I am trying to pause and resume azure resources using c# winform application. For this i am trying to use powershell cmdlets.

I can invoke powershell script through powershell ISE by entering credentials in prompt. I would like to run same script using c#.code. I am not able to pass Get-Credentials equivalent PSCredentials object to powershell script through c#. Below is same code .Please help.

C# code

private void ExecutePowerShellScript()
{
try
{

var shell = PowerShell.Create();

string pwd = "testpwd;
string encryptedPassword = RunPowerShellCommand("\"" + pwd + "\" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString", null);
SecureString securePwd = new SecureString();
foreach (char c in encryptedPassword)
{
securePwd.AppendChar(c);
}
var pscredentialObject = new System.Management.Automation.PSCredential("TestUserName", securePwd);

Command cmd = new Command($@"C:\TestingPauseScript.ps1 -ServiceName {pscredentialObject}", true);
shell.Commands.AddCommand(cmd);
Collection<PSObject> res = shell.Invoke();
}
catch(Exception ex)
{
throw ex;
}
}

public static string RunPowerShellCommand(string command, Dictionary<string, object> parameters)
{
using (PowerShell powerShellInstance = PowerShell.Create())
{
// Set up the running of the script
powerShellInstance.AddScript(command);

// Add the parameters
if (parameters != null)
{
foreach (var parameter in parameters)
{
powerShellInstance.AddParameter(parameter.Key, parameter.Value);
}
}

// Run the command
Collection<PSObject> psOutput = powerShellInstance.Invoke();

StringBuilder stringBuilder = new StringBuilder();

if (powerShellInstance.Streams.Error.Count > 0)
{
foreach (var errorMessage in powerShellInstance.Streams.Error)
{
if (errorMessage != null)
{
throw new InvalidOperationException(errorMessage.ToString());
}
}
}

foreach (var outputLine in psOutput)
{
if (outputLine != null)
{
stringBuilder.Append(outputLine);
}
}

return stringBuilder.ToString();
}
}


***************************************************************************************************

Powershell script

[CmdletBinding()]
param (
[string] $PsObject
)
$PsObject | out-file 'C:\TestCleanup\text.txt'
$PsObject | Out-String
Try{

$Cred = $PsObject <#Get-Credential#>
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
Login-AzureRmAccount -SubscriptionName "<SubscriptionName here>" -TenantId "<TentantId enter here>" -Credential $Cred
Get-AzureRmSubscription -SubscriptionName "<SubscriptionName here>" | Select-AzureRmSubscription
$Input= Import-Csv -Path "C:\TestCleanup\AASName.csv"
foreach($i in $Input)
{
$name=$i.name
Write-Host "Processing : name : $name"
Resume-AzureRmAnalysisServicesServer $name
$name | Out-File -Append 'C:\TestCleanup\logfile.log'
}
}
0 new messages