cant correctly pass array from c # to powershell

2021-08-30T09:33:21.487+00:00

Hey! Colleagues.
Sorry for my English, I use an automatic translator.

Here is my powershell script (packed in exe)

 param  
    (  
    [string]$User,  
    [String[]]$Computers  
    )  
      
    [System.Windows.Forms.MessageBox]::Show("сервера" + $computets)  
    foreach ($computer in $Computers)  
    {  
    [System.Windows.Forms.MessageBox]::Show("сервера" + $computer)  
    try  
    {  
      
    Invoke-Command -ComputerName $computer -ScriptBlock {  
    param ($user)  
    $localpath = 'c:\users\' + $user  Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -eq $localpath } |  Remove-WmiObject} -ArgumentList $user  
  
[System.Windows.Forms.MessageBox]::Show('Пользователь:' + $User + ' очищен с фермы:' + $computer)  
    }  
    catch   
    {  
    [System.Windows.Forms.MessageBox]::Show('Пользователь не вышел из сеанса на сервере '+$computer)  
    }  
    }  
      

from C # I am passing a value to it in the form:

 public static void DeleteUserProfile(string user, string ws)  
        {  
            string query = TerminalServer.Where(x => x == ws).FirstOrDefault().ToUpper();  
  
            string[] RDS = { "ALDEBARAN", "ALTAIR", "ANTARES", "TANATOS"};  
            string[] FDRDS = { "PHOEBE", "JULIET"};  
            string[] JDRDS = { "Erriapo" };  
            string[] WHRDS = { "Thrym" };  
            string[] Servers = { "Aldebaran", "Altair", "Antares", "Tanatos" };  
  
  
              
  
            if ((query == "ALDEBARAN") || (query == "ALTAIR") || (query == "ANTARES") || (query == "TANATOS"))  
            {  
                
                    ProcessStartInfo startInfo = new ProcessStartInfo(@"\\poison\rf\Reg\clearprofiles.exe");  
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;  
                    startInfo.Arguments = $"-user {user} -Computers {Servers}";  
                    Process.Start(startInfo);  
                 
            }  
    }  

I can't figure out how to pass an array to powershell
:(

I created a breakpoint in the code PS and get the value Servers param = "system.string[]";

[String[]]$Computers
[System.Windows.Forms.MessageBox]::Show("Servers param" + $computers)

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2021-08-31T08:07:15.63+00:00

    @Бутунин Клим Юрьевич , 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:

    127871-444.gif

    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.