@Бутунин Клим Юрьевич , based on my test, If we want to pass array from C# to PowerShell, we have to use the following format string to transfer it.
string result=('Aldebaran','Altair','Antares','Tanatos')
Here is a code example you can refer to.
private void button1_Click(object sender, EventArgs e)
{
DeleteUserProfile("Administrator");
}
public static void DeleteUserProfile(string user)
{
string[] RDS = { "ALDEBARAN", "ALTAIR", "ANTARES", "TANATOS" };
string[] FDRDS = { "PHOEBE", "JULIET" };
string[] JDRDS = { "Erriapo" };
string[] WHRDS = { "Thrym" };
String[] Servers = { "Aldebaran", "Altair", "Antares", "Tanatos" };
string result = string.Empty;
foreach (string item in Servers)
{
result = result+"'" + item + "'"+",";
}
result = "(" + result + ")";
result = result.Remove(result.Length - 2,1); //('Aldebaran','Altair','Antares','Tanatos')
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
startInfo.CreateNoWindow = true;
startInfo.Arguments = $"D:\\TestPower.ps1 -user {user} -Computers {result}";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
Also, I modified your PowerShell script so that I make a test.
using namespace System.Windows.Forms
param
(
[string]$user,
[String[]] $computers
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("Hello " + $user)
foreach ($computer in $computers)
{
[System.Windows.Forms.MessageBox]::Show("Hello " + $computer)
}
Result: