How can i call the below PowerShell script from c# .. by passing arguments to Powershell script from c# (my arguments are string and list types)
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"powershell.exe";
//provide powershell script full path
**startInfo.Arguments = @"& 'C:\powershell-scripts\call-from-c-sharp.ps1'";**
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
// execute script call start process
process.Start();
// get output information
string output = process.StandardOutput.ReadToEnd();
// catch error information
string errors = process.StandardError.ReadToEnd();
------------------------------------------------------------------------------
Function Server($x,[string]$lsstr)
{
Remove-Item -Path D:\vamc\Powershell_scripts\di.txt
$server=$x
#Invoke-WmiMethod -ComputerName $server
foreach($act in $lsstr)
{
If( $act -eq 1 )
{
Remove-Item -Path D:\vamc\Powershell_scripts\di1.txt
}
ElseIf($act -eq 2)
{
Remove-Item -Path D:\vamc\Powershell_scripts\di2.txt
}
ElseIf($act -eq 3)
{
Remove-Item -Path D:\vamc\Powershell_scripts\di3.txt
}
ElseIf($act -eq 4)
{
Remove-Item -Path D:\vamc\Powershell_scripts\di4.txt
}
Else
{
Write-Host "Invalid selection"
}
}
}
Server
Can you please give me the detail code explanation.
I want to call the below PowerShell script from c# by using the same code.