PoshCode | PoshCode Modules for Packaging Searching | Command Line Interface library
kandi X-RAY | PoshCode Summary
kandi X-RAY | PoshCode Summary
PoshCode Package Manager Module (BETA).
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of PoshCode
PoshCode Key Features
PoshCode Examples and Code Snippets
Community Discussions
Trending Discussions on PoshCode
QUESTION
So PowerShell error reporting seems like a huge mess considering that there are four* ways to report an error (not including the effects of $ErrorActionPreference
, try/catch
, and trap
blocks which have the potential to alter how an error is reported by another source), with minute and poorly documented differences, and even worse there's no real instruction on when to use the different types. (I realize it varies on the situation, but what's the "standard" - what will others be expecting me to do?)
That's not really the main point here though. I've decided to go with the simplest option when possible as that's what I expect most others will be using, so at least we'll have consistency - but I'm either not understanding something properly or it's broken.
According to the PowerShell documentation, the Activity
property of ErrorCategoryInfo
class (observed in the CategoryInfo
property of System.Management.Automation.ErrorRecord
) should be a "text description of the operation which encountered the error". Considering the fact that the value is settable and (in the case of compiled cmdlets) indicates the name of the cmdlet unless otherwise specified, I would expect the same functionality in script-based error reporting methods, but I'm doing something wrong or I've found a bug in PowerShell that has pervaded since PowerShell 1.0.
Consider the following example:
...ANSWER
Answered 2021-Mar-23 at 19:28First, kudos for your in-depth analysis.
You've already found an effective workaround that also works in Windows PowerShell for having your custom .Activity
value honored: use of $PSCmdlet.WriteError()
- though note that $PSCmdlet
is only available in advanced functions and scripts.
That an .Activity
value isn't honored if it is part of a System.Management.Automation.ErrorRecord
instance passed in full to Write-Error
's -ErrorRecord
parameter, whereas it now is if you use the -CategoryActivity
parameter (shorter alias: -Activity
) in PowerShell (Core)7+, is arguably a bug that I encourage you to report at the PowerShell GitHub repo.
- Update: Thankfully, you have since done so in GitHub issue #15088.
Note: As you point out, the following alternative workarounds are effective in PowerShell (Core) 7+ only:
A slightly less obscure workaround that doesn't require $PSCmdlet
and is therefore also available in non-advanced functions and scripts:
QUESTION
I'm new to Powershell and as best as I can find it doesn't have anything like PEP8/PEP484 in Python. I found this document from Microsoft and this third party guide from Posh Code. I have written the following function:
...ANSWER
Answered 2020-Oct-31 at 19:43It's called "comment-based help" (about_Comment_Based_Help)
You have 3 options where to put the documentation:
• At the beginning of the function body.
• At the end of the function body.
• Before the function keyword. There cannot be more than one blank line between the last line of the function help and the function keyword.
So, you can easily put them at the top of your function (either inside or outside):
QUESTION
I'm trying to make use of module-qualified names[1] and a DefaultCommandPrefix
and not have it break if the module is imported with Import-Module -Prefix SomethingElse
. Maybe I'm just doing something really wrong here, or those two features aren't meant to be used together.
Inside the main module file using "ModuleName\Verb-PrefixNoun args...
" works as long as "Prefix" matches the DefaultCommandPrefix
in the manifest (the module-qualified syntax seems to require the prefix used for the import[2]). But importing the module with a different prefix, all module-qualified references inside the module breaks.
After a bit of searching and trial and error, the least horrible solution I've managed to get working is to use something like the following hackish solution. But, I can't help wonder if there isn't some better way that automatically handles the prefix (just as Import-Module
obviously manages to add the prefix, my first naive though was that using just ModuleName\Verb-Noun
would automatically append any prefix to the noun, but evidently not[2].
So this is the hack I came up with, that looks up the modules prefix and appends it, then using "." or "&" to expand/invoke the command:
...ANSWER
Answered 2020-May-31 at 22:48You can avoid the problem by not using a module qualifier not using a noun prefix when you call your module's own functions.
That is, call them as Verb-Noun
, exactly as named in the target function's implementation.
This is generally safe, because your own module's functions take precedence over any commands of the same name defined outside your module.
- The sole exception is if an alias defined in the global scope happens to have the same name as one of your functions - but that shouldn't normally be a concern, because aliases are used for short names that do not follow the verb-noun naming convention.
It also makes sense in that it allows you to call your functions module-internally with an invariable name - a name that doesn't situationally change, depending on what the caller decided to choose as a noun prefix via
Import-Module -Prefix ...
.- Think of the prefix feature as being a caller-only feature that is unrelated to your module's implementation.
As an aside: As of PowerShell 7.0, declaring a default noun prefix via the DefaultCommandPrefix
module-manifest property doesn't properly integrate with the module auto-loading and command-discovery features - see this GitHub issue.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install PoshCode
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page