To execute PowerShell code from an ASP.NET application, I use the following code:
System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); runspace.Open(); System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(@"\\servername\path"); pipeline.Commands.Add("Out-String"); Collection<PSObject> results = pipeline.Invoke(); runspace.Close();
However, I’m getting the following error:
A command prompt or a Windows (Windows Forms) program both work perfectly with the same code.
Asked by umesh.chape
Due to the execution policy, your script is unable to run.
PowerShell must be executed as an administrator and set to Unrestricted on the client PC. You can accomplish so by invoking Invoke with the command:
Answered by ghord
You can follow the procedures recommended in the previous responses, verify that Execution Policy is configured correctly, and still have your scripts fail in some cases. If this happens to you, you’re most likely using a 64-bit machine that has both 32-bit and 64-bit versions of PowerShell installed, with the failure occurring in the version that doesn’t have Execution Policy enabled. Because the setting does not apply to both versions, you must set it twice.
Look for System32 and SysWOW64 in your Windows directory.
For each directory, repeat these steps:
Note that depending on the scope for which you’re trying to set the policy, you may need to execute PowerShell as administrator.
Answered by Jon Crowell
The issue is that each user’s execution policy is different. To make your program work, you’ll need to run the following command every time you run it:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
There’s certainly a way to do this for the ASP.NET user as well, but this method means you’re only opening up your application, not the entire system.
Answered by Matthew Steeples
Set-ExecutionPolicy must be run:
Set-ExecutionPolicy Unrestricted <-- Will allow unsigned PowerShell scripts to run. Set-ExecutionPolicy Restricted <-- Will not allow unsigned PowerShell scripts to run. Set-ExecutionPolicy RemoteSigned <-- Will allow only remotely signed PowerShell scripts to run.
Answered by Nadeem_MK
I encountered a similar problem and discovered that Windows Server 2012’s default cmd was executing the x64 version.
Run the following commands as Administrator on Windows 7, Windows 8, Windows Server 2008 R2, or Windows Server 2012:
Open the C:WindowsSysWOW64cmd.exe file. Execute the following command: powershell RemoteSigned-Set-ExecutionPolicy
Open the C:Windowssystem32cmd.exe file. Use the powershell command Set-ExecutionPolicy RemoteSigned.
You can use this to check the mode.
echo PROCESSOR ARCHITECTURE% PROCESSOR ARCHITECTURE% PROCESSOR ARCHITECTURE% PROCESSOR ARCHITEC [Environment]::Is64BitProcess in Powershell
I hope this information is useful to you.
Answered by Chandra sekhar P
Post is based on https://stackoverflow.com/questions/16460163/ps1-cannot-be-loaded-because-the-execution-of-scripts-is-disabled-on-this-syste