question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PowerShell needs a way to hide commands which won't work

See original GitHub issue

For example: Set-ExecutionPolicy. This command should simply not exist on Linux. Rather than throwing RuntimeErrorException terminating errors, I would prefer to get a command-not found, because that’s something I can test for ahead of time.

Years of developing for moving targets on the web has taught us that the right way to avoid features that don’t exist is to test for the feature, and not for the underlying OS, Architecture, or software version, because you never know when someone’s going to finally figure out how to implement something.

If non-functioning commands are not hidden, there is no easy, fast way for scripts to test for whether commands will work – you have to run them and catch the exception. However, many scripters will instead write incorrect conditional code based on the PSEdition (or operating system sniffing, but currently that’s not even in $PSVersionTable #1997) which will prevent them from using the feature in the case where it’s added later on.

In general, when a (core) module exposes a command (functioning or not), it’s more difficult for another module to provide an implementation – I know we can’t anyway in this particular example, but that’s the problem with picking examples 😉

Suggestion: This should be exposed in the manifest.

I would like an easy way to identify commands that should be exported based on PSEdition, PSVersion, OS, or Architecture from the module manifest.

I don’t want to have to wrap binary modules in script modules to put conditional Export-ModuleManifest statements in … especially since putting the right values in the manifest for exported cmdlets is vital to discoverability.

One thought is to allow specific module manifests for platforms, like MyModule.Core-Linux.psd1 or something – this would provide solutions to other design problems as well (like loading the right binary assemblies).

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
lzybkrcommented, Aug 21, 2016

The plan is to allow the variables $IsLinux and $IsWindows so you can use something like:

CmdletsToExport = "Get-Process",$(if ($IsWindows) { "Set-ExecutionPolicy" })
3reactions
SteveL-MSFTcommented, Jan 9, 2019

Module manifest already supports conditional exporting https://docs.microsoft.com/en-us/powershell/gallery/concepts/module-psedition-support

As for the ExecutionPolicy cmdlets themselves, I don’t recall exactly why we left them since we explicitly removed others. Let’s have Committee review.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Opening PowerShell Script and hide Command Prompt ...
To hide the console with this code, all you need to do is call Hide-Console ... If you run PowerShell from a shortcut...
Read more >
Powershell Script - Hide cmd Window?
I have a simple PS script that alternates/displays open Chrome windows (not tabs) after a preset time delay. The script is run with...
Read more >
about Hidden - PowerShell
To display members that you have hidden with the hidden keyword, add the Force parameter to a Get-Member command. The hidden keyword can...
Read more >
Discarding Output - powershell.one
When you run this command, it emits a ton of information, and there doesn't seem to be a way to hide or discard...
Read more >
How do you hide the Powershell console window when ...
My script will run on a customer's server, so I need a command within the script itself to minimize/disable the powershell console.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found