Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.18k stars 3.79k forks source link

`Az.SecurityInsights` v3.1.1 randomly runs something in the terminal #23573

Open o-l-a-v opened 9 months ago

o-l-a-v commented 9 months ago

Description

I've had the same issue with Az.Accounts earlier. Thought it was vscode-powershells fault, but seems it was Az.

Here's what happened today: I started writing down a script to Get-AzSentinelDataConnector. Suddenly the terminal outputs the following and won't finish/quit unless I do CTRL+C:

PS C:\Users\olav.birkeland\OneDrive\IT\Code> PipelineStoppedException: The pipeline has been stopped.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
 Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
   at System.Management.Automation.ScriptBlock.DoInvokeReturnAsIs(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)
   at System.Management.Automation.CommandInvocationIntrinsics.InvokeScript(ScriptBlock sb, Boolean useNewScope, PipelineResultTypes writeToPipeline, IList input, Object[] args)       
   at System.Management.Automation.CommandInvocationIntrinsics.InvokeScript(String script)
   at Microsoft.Azure.PowerShell.Cmdlets.SecurityInsights.Runtime.PowerShell.PsHelpers.RunScript[T](CommandInvocationIntrinsics cii, String script)
   at Microsoft.Azure.PowerShell.Cmdlets.SecurityInsights.Runtime.PowerShell.PsExtensions.RunScript[T](PSCmdlet cmdlet, String script)
   at Microsoft.Azure.PowerShell.Cmdlets.SecurityInsights.Runtime.PowerShell.PsHelpers.GetScriptCmdlets(PSCmdlet cmdlet, String scriptFolder)
   at Microsoft.Azure.PowerShell.Cmdlets.SecurityInsights.Runtime.PowerShell.GetScriptCmdlet.ProcessRecord()
                                             ^C
PS C:\Users\olav.birkeland\OneDrive\IT\Code>

Issue script & Debug output

I can't reliably reproduce it.

Environment data

Module versions

Az.Accounts v2.13.2
Az.SecurityInsights v3.1.1

Error output

No output.
isra-fel commented 9 months ago

Thanks for reporting. Did Resolve-AzError not return anything?

@dolauli let's look into it tomorrow.

o-l-a-v commented 9 months ago

I answered in the vscode-powershell issue @isra-fel. Doesn't look obvious to me that this is Az's fault alone.

dolauli commented 9 months ago

@o-l-a-v @isra-fel

GetScriptCmdlet is called when autogen module is imported to get the function cmdlets within a folder. And the code is as below.

        public static IEnumerable<FunctionInfo> GetScriptCmdlets(PSCmdlet cmdlet, string scriptFolder)
        {
            // https://stackoverflow.com/a/40969712/294804
            var wrappedFolder = scriptFolder.Contains("'") ? $@"""{scriptFolder}""" : $@"'{scriptFolder}'";
            var getCmdletsCommand = $@"
$currentFunctions = Get-ChildItem function:
Get-ChildItem -Path {wrappedFolder} -Recurse -Include '*.ps1' -File | ForEach-Object {{ . $_.FullName }}
Get-ChildItem function: | Where-Object {{ ($currentFunctions -notcontains $_) -and $_.CmdletBinding }}
";
            return cmdlet?.RunScript<FunctionInfo>(getCmdletsCommand) ?? RunScript<FunctionInfo>(getCmdletsCommand);
        }

I do not see the issue in pwsh 7. Could it be an issue of vscode-powershell?