Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) My talk is here: https://www.youtube.com/watch?v=40k-aoJdaoM
    PowerShell Connect
    @PoshConnect_twitter
    jkavanagh58 (slack) Okay so IncludeDefaultRules = $true in a PSSA settings file nullifies anything in the Rules = @{..} definitions? Just trying to confirm what I am seeing.
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) Yes. The purpose of this switch is when it is used together with the custom rule path parameters , i.e. to run custom and default rules together. The Rules parameter you would use only when you select a specific set of default rules when being used together with custom rules. I have tried to summarise it here: https://github.com/PowerShell/PSScriptAnalyzer/issues/675#issuecomment-377783190
    PowerShell Connect
    @PoshConnect_twitter
    JamesTruher_msft (slack) cool - my build changes passed CI: PowerShell/PSScriptAnalyzer#1247
    PowerShell Connect
    @PoshConnect_twitter
    jkavanagh58 (slack) @c.bergmeister So if I read all the related docs... if I have Rules = @{..} I need to include them in a IncludeRules = @(..) ?
    c.bergmeister (slack) IncludeRules is a list of rules that you want to use and Rules is for additional customization of those
    PowerShell Connect
    @PoshConnect_twitter
    jkavanagh58 (slack) Roger that @c.bergmeister I had the IncludeDefaultRules = $true but not an array of rules to include
    PowerShell Connect
    @PoshConnect_twitter
    jkavanagh58 (slack) Cant get the AvoidUsingCmdletAiases whitelist to work, boogered something up somewhere
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) @jkavanagh58 Here is an example that is used by PSSA's test suite (therefore assuming it works): https://github.com/PowerShell/PSScriptAnalyzer/blob/development/Tests/Engine/SettingsTest/Project1/ExplicitSettings.psd1
    PowerShell Connect
    @PoshConnect_twitter
    jkavanagh58 (slack) @c.bergmeister thanks. I think I have the syntax right but it does not seem to be applying... I have a feeling there is an issue somewhere else in my psd1... thank you
    c.bergmeister (slack) Hmm, yes, unfortunately, PSSA is not very good at telling when there is a syntax problem in the settings file. Tip: Use the -Verbose switch to get more info out. The settings file is just a powershell hashtable (you can in fact just supply a hashtable instead of a settings file instead to the -Settings parameter). Therefore double check that the content of the settings file is at least a valid hash table.
    PowerShell Connect
    @PoshConnect_twitter
    JamesTruher_msft (slack) @channel I've been working on a settings file validator. It would be great if I could get as many examples of settings files (especially the ones that caused you difficulty (with the errors, etc). I've got something working pretty well, but I would like to through a bunch more examples at it.
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) Awesome, looking forward to it but I suggest to focus first on getting the existing PRs in for 1.18.1
    The validation should be at minimum if the hashtable can be parsed
    Related issue:
    PowerShell/PSScriptAnalyzer#1053
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) PSSA 1.18.1 is now released, I wrote a short blog post
    https://devblogs.microsoft.com/powershell/release-of-powershell-script-analyzer-1-18-1/
    PowerShell Connect
    @PoshConnect_twitter
    freddie_sackur (slack) Double the speed for two releases in a row - smashing Moore's Law!
    I might hold out for 1.20...
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) Specifically for the use case of the VSCode extension we plan to publish PSSA as a NuGet library, which will make the performance inside the editor much faster as the overhead of invoking powershell and using pipelines is avoided.
    At the moment what happens when VSCode runs code analysis or formatting is quite insane:
    The extension is written in typescript (due to vscode being based on it). This code makes a request to call into .net code (PowerShellEditorServices), which invokes PowerShell, which invokes the PSSA cmdlets and PSSA (written in .net) then invokes PowerShell again for getting info from e.g. Get-Command
    The idea is to let PowerShellEditorServices call the PSSA classes directly as both modules are written in .net
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) What we have done in 1.18.0 was basically use a Cache for the expensive Get-Command calls that PSSA makes and to not use slimmer, thread safe locks around those calls. In 1.18.1 we started re-using the RunSpace pool and eliminated unnecessary calls. There is not much left for optimisation at the moment, we could try to reduce the number of cache misses using a more sophisticated cache but that's about all we can think of at the moment, hence why the focus is now on the more important editor performance, which has more optimisation potential.
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) What we have done in 1.18.0 was basically use a Cache for the expensive Get-Command calls that PSSA makes and to use slimmer, thread safe locks around those calls. In 1.18.1 we started re-using the RunSpace pool and eliminated unnecessary calls. There is not much left for optimisation at the moment, we could try to reduce the number of cache misses using a more sophisticated cache but that's about all we can think of at the moment, hence why the focus is now on the more important editor performance, which has more optimisation potential.
    PowerShell Connect
    @PoshConnect_twitter
    JamesTruher_msft (slack) also, there was a pretty bad bug in the AvoidAliases rule which ran 2 pipelines for every cache hit!
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) Calling all PSScriptAnalyzer contributors:
    The default branch has been changed from development to master for a simplified workflow and to be consistent with other repositories. We will still leave the old branch there for legacy reasons but this means you probably want to update your default branch in your fork to master. https://help.github.com/en/articles/setting-the-default-branch
    John Vandenberg
    @jayvdb
    I have the same question as @joeypiccola from https://gitter.im/PowerShell/PSScriptAnalyzer?at=5b33ccffa288503b3de47938 - it seems it wasnt answered. How does one inherit from & extend CodeFormattingOTBS . I want to automatically get new rules added to the base settings, when pssa is updated, but also have my own additions.
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) There is no way to inherit, you could either have your own settings file with your own definitions or run Invoke-ScriptAnalyzer twice (once for code analysis rules and once for code style rules if your customisation is only about the code analysis rules). Otherwise I suggest to look at the OTBS file on GitHub after each release, which should not be too much of a burden since PSSA does not release very often and reviewing new rules before adding them is good practice anyway IMHO to avoid that a new release suddenly breaks your build
    John Vandenberg
    @jayvdb
    thanks.
    I copied the OTBS file, and found that it failed the rules that it describes. It looks like CodeFormatting.psd1 also fails the rules which it describes
    John Vandenberg
    @jayvdb
    What is the default preset settings used if none is provided when invoking Invoke-ScriptAnalyzer
    John Vandenberg
    @jayvdb
    It looks like it is all rules in namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules, and that is all Rules/*.cs , except the follow fields which dont look like rules anyway ClangSuppresion, LoggerInfo, and Strings.Designer
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) Invoke-ScriptAnalyzer runs only code analysis rules by default that do not require configuration, which means that it does not run the code formatting rules or code analysis rules that need configuration (e.g the compatibility rules). The -Settings parameter should be used for customisation (or it finds the settings file automatically if it is named as PSScriptAnalyzerSettings.psd1
    John Vandenberg
    @jayvdb
    thx.
    PowerShell Connect
    @PoshConnect_twitter
    JamesTruher_msft (slack) could you provide a bit more details on the false positive?
    John Vandenberg
    @jayvdb
    PowerShell/PSScriptAnalyzer#769 run the formatter on formatter repo
    PowerShell Connect
    @PoshConnect_twitter
    JamesTruher_msft (slack) Thank you, but this seems to be a different, unrelated topic from "PSUseCmdletCorrectly" immediately above. wrt "PSUseCmdletCorrectly", it has a logic error in the rule wherein the rule reports only if the number of mandatory parameters is equal to, or greater than the number of parameter sets. This is clearly wrong. Remove-Item has a single mandatory parameter, with 2 parameter sets, so
    Invoke-ScriptAnalyzer -ScriptDefinition 'remove-item' should trigger the "PSUseCmdletCorrectly" rule, but does not.
    JamesTruher_msft (slack) It turns out this might be harder to fix than at first glance as in order to determine whether the mandatory parameter is missing, you must first determine which parameter set is active, and then determine whether the parameter was provided.
    John Vandenberg
    @jayvdb
    The question by
    https://github.com/joeypiccola ? That was about how to invoke pssa once go get default rules and formatting rules together
    PowerShell Connect
    @PoshConnect_twitter
    JamesTruher_msft (slack) along the lines of PSUseCmdletCorrectly, it should also be possible to determine those cases where two parameter sets are active. ex:
    Remove-Item -LiteralPath foo -Path foo
    is clearly an error and analyzer should be able to recognize that 2 parameter sets are active. This would be more difficult to do with the current rule, but should be possible.
    PowerShell Connect
    @PoshConnect_twitter
    OmniiOMEGA (discord) Can someone help me with my script?
    It's not correct:
    OmniiOMEGA (discord) Invoke-RestMethod : :"Unexpected character encountered while parsing value: {. Path 'Users', line 3, position 19. After parsing a value an unexpected
    character was encountered: :. Path 'Users[0]', line 4, position 30. After parsing a value an unexpected character was encountered: :. Path 'Users[0]', line 4, position 30.","error_code":1}
    At C:_RCT\IT TOOLS\Scripts\PowerShell Scripts\TeamViewerPSModules.psm1:701 char:5
    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) Without context it is hard to say something but it looks like the response from the REST API that it received is malformed
    PowerShell Connect
    @PoshConnect_twitter
    JamesTruher_msft (slack) it looks like that message is coming out of the Newtonsoft dll, and indicates a problem with parsing the json.
    PowerShell Connect
    @PoshConnect_twitter
    mrboring (discord) May I ask a question about custom PSScriptAnalyzer rules?

    mrboring (discord) I'm having problems with a custom PSScriptAnalyzer rule I've written. It checks that the function name is not plural. It works OK, but highlights the whole function. How can I get it to only highlight the name?

    A bit more info. I've tried using the GetScriptExtentForFunctionName function, but it only works if the function is in the same file. I notice that this function is used by the rule that checks for approved verbs.

    I have the singular nouns rule enabled, but it's not finding function names with plural nouns. In my case Find-Issues.
    From the docs for the rule:
    NOTE This rule is not available in PowerShell Core due to the PluralizationService API that the rule uses internally.
    I'm using PowerShell Core 6.2.2.
    My version uses the Humanizer library.

    PowerShell Connect
    @PoshConnect_twitter
    c.bergmeister (slack) Of course you can ask. The highlighting originates from the Extent of the DiagnosticRecord that you emit in your custom rule. I assume in your code somewhere you track down the PipelineAst/CommandAst and use it's StringConstantExpressionAst (that contains the function name in its Value property) in the Extent property of the DiagnosticRecord. The problem is that the properties in the Extent (which is a class of PowerShell itself) are read-only. The only thing that you could do is to try to generate a new Extent with the desired. The Extent is actually just an implementation of the IScriptExtent interface. The implementating class is ScriptExtent, which takes 2 ScriptPosition for the start and end position in its constructor. This way you can create a new Extent, which is only the part of the extent that you want to highlight
    c.bergmeister (slack) The constructors for creating your new ScriptExtent are:
    public ScriptExtent(ScriptPosition startPosition, ScriptPosition endPosition)
    public ScriptPosition(string scriptName, int scriptLineNumber, int offsetInLine, string line)
    PowerShell Connect
    @PoshConnect_twitter
    KevinBlumenfeld (discord) @mrboring ^
    PowerShell Connect
    @PoshConnect_twitter
    mrboring (discord) Thanks c.bergmeister, this is helpful. I'll try to create my own ScriptExtent.
    PowerShell Connect
    @PoshConnect_twitter

    mrboring (discord) c.bergmeister - I've done a quick prototype and it looks fine:
    ```PowerShell
    $startPos = [ScriptPosition]::new('Find-DurationIssue.ps1', 4, 10, 'function Find-DurationIssues {')
    $endPos = [ScriptPosition]::new('Find-DurationIssue.ps1', 4, 29, 'function Find-DurationIssues {')
    $extent = [ScriptExtent]::new($startPos, $endPos)

    $results.Add(
    [DiagnosticRecord]@{
    Message = "Avoid pluralised function names. $a"
    Extent = $extent
    RuleName = 'PSAvoidPluralisedFunctionNames'
    Severity = 'Warning'
    }
    ) | Out-Null
    ``` (edited)

    mrboring (discord) Thanks for the help.