question

08548742 avatar image
0 Votes"
08548742 asked JackJJun-MSFT edited

cant correctly pass array from c # to powershell

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)



dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@08548742, I have not heard from you for a couple of days. Please let me know if there is anything that I can help in here.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JackJJun-MSFT edited

@08548742, 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





444.gif (271.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.