WASM cannot host PowerShell. Under the covers the PowerShell.Create() uses create process, which is not allowed by the WASM sandbox. Currently the sandbox has no file, network or DOM access. it must call js to access browser api calls.
Blazor WASM to execute powershell script
I'm developing a Blazor WASM using .Net 7 to deploy all of our applications on a server. Blazor WASM is hosted locally but run on browsers on the remote server where applications need to be deployed. Servers are not connected and not in the same network.
Blazor WASM was able to download the application package (zip file) and store locally on a temp folder on the server where the application needs to be deployed. I have powershell scripts (.ps1 file), which was done with the version powershell 5.1 is also on the temp folder.
This powershell script basically extracts the files form the zip, creates application pools on IIS if necessary and also creates virtual directory if does not exist and copy the "extracted" files of the application. So, the powershell uses WebAdministration Module. please note that the powershell script runs good manually.
In the Blazor WASM I try to run the powershell script file (snippet below), I get the following error:
Error: Failed to generate proxies for remote module 'WebAdministration'. Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files.Exception calling "ReadKey" with "1" argument(s): "A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the PowerShell Console, and remove prompt-related commands from command types that do not support user interaction."
My powershell scripts has user interaction command such as ReadKeysince it was also meant to run manually just in case. So I tried to suppress the user input with "Out-Null".
I know WASM may have some restriction but still want to try and ensure that I'm not doing any mistakes.
any help would be appreciated.
var scriptFullPath = @"C:\tempArtifacts\PPortal.ps1";
var scripts = File.ReadAllText(scriptFullPath);
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript(scripts);
ps.AddCommand("Out-String"); // Convert output to a string
ps.AddCommand("Out-Null");
//// Suppress user interaction
//ps.AddParameters(new Dictionary<string, object>
//{
// { "NonInteractive", true }
//});
var result = ps.Invoke();
Developer technologies | .NET | Blazor
Windows for business | Windows Server | User experience | PowerShell
1 answer
Sort by: Most helpful
-
Bruce (SqlWork.com) 81,971 Reputation points Volunteer Moderator
2023-12-12T22:57:20.4533333+00:00