1

New to PowerShell in .NetCore and the script works fine in PowerShell ISE, but when I run through my console application.

Here is the script:

Get-WindowsCapability -Online -Name Microsoft.Windows.Notepad*

Here is the output through PowerShell ISE:

Name : Microsoft.Windows.Notepad~~~~0.0.1.0 State : Installed DisplayName : Notepad Description : View, edit, and search through plain text documents and source code files instantly. DownloadSize : 301710 InstallSize : 647868

Here is my code snippet from my .NetCore console application:

var psInstance = PowerShell.Create();
psInstance.AddScript("Get-WindowsCapability -Online -Name Microsoft.Windows.Notepad*");
var output  = psInstance.Invoke();
psInstance?.Runspace?.Close();

if (psInstance.HadErrors)
{
   var error = psInstance.Streams.Error.Select(e => e.ToString());
}

Here is the error I'm seeing in my pInstance object:

Count = 5 [0]: "The 'Get-WindowsCapability' command was found in the module 'Dism', but the module could not be loaded due to the following error: [File C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Dism\Dism.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.]\r\nFor more information, run 'Import-Module Dism'." [1]: "The 'Get-WindowsCapability' command was found in the module 'Dism', but the module could not be loaded due to the following error: [File C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Dism\Dism.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.]\r\nFor more information, run 'Import-Module Dism'." [2]: "[localhost] Connecting to remote server localhost failed with the following error message : WinRM cannot process the request. The following error with errorcode 0x8009030e occurred while using Negotiate authentication: A specified logon session does not exist. It may already have been terminated. \r\n Possible causes are:\r\n -The user name or password specified are invalid.\r\n -Kerberos is used when no authentication method and no user name are specified.\r\n -Kerberos accepts domain user names, but not local user names.\r\n -The Service Principal Name (SPN) for the remote computer name and port does not exist.\r\n -The client and remote computers are in different domains and there is no trust between the two domains.\r\n After checking for the above issues, try the following:\r\n -Check the Event Viewer for events related to authentication.\r\n -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use HTTPS transport.\r\n Note th at computers in the TrustedHosts list might not be authenticated.\r\n -For more information about WinRM configuration, run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.\r\n Other Possible Cause:\r\n -The domain or computer name was not included with the specified credential, for example: DOMAIN\UserName or COMPUTER\UserName." [3]: "Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again." [4]: "Cannot validate argument on parameter 'Id'. The argument is null. Provide a valid value for the argument, and then try running the command again."

I a running the console app as Administrator. Why does the script run successfully through PowerShell IDE, but throw errors through the sample app?

Is there something I additionally have to do to my PowerShell instance object?

JohnB
  • 3,921
  • 8
  • 49
  • 99
  • Check `[enum]::GetNames( [Microsoft.PowerShell.ExecutionPolicyScope]) | ForEach-Object {@{$_ = Get-ExecutionPolicy -Scope $_}}` (and [edit] your question to improve your [mcve] with that info, please). – JosefZ Jan 27 '23 at 20:52
  • @JosefZ - Not sure I follow you with the Check [enum] comment. The edited code snippet works fine cut & paste. Can you pls elaborate? Thx – JohnB Jan 27 '23 at 21:00
  • Works fine. of course… But is a difference if you run it in `ISE` and your problematic environment (`PowerShell.exe` or `pwsh.exe`?) - both elevated and _normal_? – JosefZ Jan 27 '23 at 21:14

1 Answers1

0

tl;dr

  • Configure your PowerShell SDK-based session with an execution policy that permits script execution, such as RemoteSigned

  • See this answer for an example.


Background information.

The obsolescent[1] Windows PowerShell ISE invariably runs Windows PowerShell, whereas your .NET (Core) C# application of necessity uses PowerShell (Core)'s SDK.

The two PowerShell editions have separate execution policies, and it sounds like while Windows PowerShell on your system is configured to permit script execution, PowerShell (Core) is not.

It may be surprising that the Dism module, which Get-WindowsCapability is a part of, is subject to the script execution policy, given that modules aren't .ps1 script files.

However, many modules are, namely if they (also) contain PowerShell code, which is not only true for script modules that are entirely implemented in PowerShell code (*.psm1), but also for primarily binary modules (those that provide their cmdlets via assemblies, i.e. compiled code), if such modules also contain PowerShell code and/or formatting files (*.format.ps1xml) or type-definition files (*.types.ps1xml).

The most robust solution is to set the desired execution policy on a per-session basis, as shown in the linked answer.


[1] The Windows PowerShell ISE is no longer actively developed and there are reasons not to use it (bottom section), notably not being able to run PowerShell (Core) 6+. The actively developed, cross-platform editor that offers the best PowerShell development experience is Visual Studio Code with its PowerShell extension.

mklement0
  • 382,024
  • 64
  • 607
  • 775