0

I try to simply call my script by using the System.Diagnostics.Process.Start command, but when powershell appears I have a sort of crash. I tried to use this command with other scripts and it was working. I give you the entire line :

System.Diagnostics.Process.Start("powershell.exe", @"..\..\script\rolesAndFeatures");

and my script:

function rolesAndFeatures{ 

param(
    [parameter(mandatory)]
    [string[]] $computerNames
)

foreach($computerName in $computerNames) { 
    Install-WindowsFeature FileAndStorage-Services , Storage-Services , Web-Server ,  Web-WebServer , Web-Default-Doc , Web-Dir-Browsing , Web-Http-Errors , Web-Static-Content , Web-Health , Web-Http-Logging , Web-ODBC-Logging , Web-Performance , Web-Stat-Compression , Web-Security , Web-Filtering , Web-Basic-Auth , Web-App-Dev , Web-Net-Ext , Web-Net-Ext45 , Web-Asp-Net , Web-Asp-Net45 , Web-ISAPI-Ext , Web-ISAPI-Filter , Web-Mgmt-Tools , Web-Mgmt-Console , Web-Mgmt-Compat , Web-Metabase , Web-Lgcy-Mgmt-Console , Web-WMI , NET-Framework-Features , NET-Framework-Core , NET-HTTP-Activation , NET-Framework-45-Features , NET-Framework-45-Core , NET-Framework-45-ASPNET , NET-WCF-Services45 , NET-WCF-HTTP-Activation45 , NET-WCF-TCP-PortSharing45 , RSAT , RSAT-Feature-Tools , RSAT-SMTP , RSAT-Role-Tools , RSAT-AD-Tools , RSAT-AD-PowerShell , RSAT-ADDS , RSAT-AD-AdminCenter , RSAT-ADDS-Tools , RSAT-ADLDS , RSAT-Hyper-V-Tools , Hyper-V-Tools , Hyper-V-PowerShell , RSAT-RDS-Tools , UpdateServices-RSAT , UpdateServices-API , UpdateServices-API, UpdateServices-UI , SMTP-Server , System-DataArchiver , Windows-Defender , PowerShellRoot , PowerShell , PowerShell-V2 , PowerShell-ISE , WAS , WAS-Process-Model , WAS-NET-Environment , WAS-Config-APIs , WoW64-Support , XPS-Viewer -restart
}

}
rolesAndFeatures

I don't understand why, hope you can help me thank you guys.

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
quentin5
  • 3
  • 1
  • 4
  • 2
    Please be more descriptive than "a sort of crash" - does PowerShell return unexpected output? Does the process halt immediately without output? What is the exit code? Does the C# program terminate? Does the computer catch on fire? – Mathias R. Jessen May 26 '20 at 13:19
  • about permission no need to execute with admin rights? – Frenchy May 26 '20 at 13:20
  • 1
    At first glance it looks like your rolesAndFeatures.ps1 script contains a function that is expecting mandatory input ($computerNames) - if this isn't populated, there's nothing for the script to do, perhaps that's where your issue lies? – LordPupazz May 26 '20 at 13:20
  • Thanks for your fast answers, I haven't been clear sorry, when I execute my application the powershell appears asks me the computerNames and after the entries, the shell just shutdown. – quentin5 May 26 '20 at 13:41
  • how do you suppose these are running on each computer.. from the looks of it, its running the same "install-windowsFeature" on the same computer thats running the script – Jawad May 26 '20 at 13:44
  • My script isn't finished yet but it's working when I use it on a powershell without the c# application. – quentin5 May 26 '20 at 13:47
  • The command you're trying to run in PowerShell will require admin rights. Are you running your C# app with admin rights too? That would be necessary for PowerShell to have enough permission to be able to do the changes. – FoxDeploy May 26 '20 at 14:09
  • Yes I did, and still not working :/ – quentin5 May 26 '20 at 14:13
  • @quentin5 Did you try passing credentials to `Install-WindowsFeature` ? You can also try passing credentials to `System.Diagnostics.Process.Start` – Bassie May 26 '20 at 14:41
  • I can launch my program with admin rights, but it's still shutdown after I marked the computerNames – quentin5 May 26 '20 at 14:57
  • @quentin5 I don't mean just launching with admin rights, I mean have you tried actually passing in admin user and password as a paramter to the method call? `Install-WindowsFeature` has a `-Credential` paramater, and so does `Process.Start` I would try that – Bassie May 26 '20 at 14:58
  • @Bassie How can I do that in the Process.Start ? – quentin5 May 26 '20 at 15:06
  • @quentin5 Take a look at the docs: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start?view=netcore-3.1 look for the bit entitled `Start(String, String, String, SecureString, String) ` – Bassie May 26 '20 at 15:07
  • Yes it's what I did: System.Diagnostics.Process.Start("powershell.exe", @"..\..\script\rolesAndFeatures" , "ADMINISTRATOR" , "pswd"); but the conversion to string for the third argument is impossible – quentin5 May 26 '20 at 15:09
  • @quentin5 I'm not sure what you mean by ' conversion to string for the third argument is impossible' How about passing creds to powershell? no luck? – Bassie May 26 '20 at 15:20
  • I have the error CS1503 (cannot convert from 'string' to 'System.Security.SecureString'), I'm trying to fix it. – quentin5 May 26 '20 at 15:24
  • So, I used this method: Start(String, String, String, SecureString, String) and unfortunately nothing changed... – quentin5 May 26 '20 at 15:50
  • I will try to find the solution tomorrow, thanks for your help guys. – quentin5 May 26 '20 at 16:05
  • @quentin5 Here are a couple of ideas you could try: 1. Try routing the error output from Process.Start to somewhere, so you can see if any error messages appear - it may be useful to see whats going on (see https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandarderror?view=netcore-3.1) 2. You could try this approach which worked for me in the past: https://stackoverflow.com/questions/40552976/script-works-in-powershell-but-not-c-sharp 3. Add some try/catches to your PS script and log the error! it could just be something really simple – Bassie May 26 '20 at 16:09
  • @quentin5, If you want to execute powershell code in c#, please refer to [Execute PowerShell Script from C# with Commandline Arguments](https://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments). – Jack J Jun May 27 '20 at 01:38
  • Thanks for your answers. I add a simply Read-Host -Prompt "Press Enter to exit" with a catch error to my powershell script, and I can now see the next error : « The term 'Install-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program. ». I am supposed to have the administrator rights on the powershell execution because I had them previously. I need help again.. – quentin5 May 27 '20 at 07:40
  • I tried to compil my application in 64 bits like I read here: https://stackoverflow.com/questions/13021547/powershell-cmdlet-install-windowsfeature-called-from-c-sharp-errors-and-is-not-l but still not working. – quentin5 May 27 '20 at 08:10
  • If you want to make your app run as administrator, I suggest that you can add the manifest file to do it. You can refer to [How do I force my .NET application to run as administrator?](https://stackoverflow.com/a/43941461/11507778). – Jack J Jun May 28 '20 at 01:49

0 Answers0