dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

`Invoke-ManagedComputerCommand` - Returns `$true` On `$result.Exception` If No Error

Open mattcargile opened this issue 3 years ago • 1 comments

Verified issue does not already exist?

Yes

What error did you receive?

None. I have discovered this behavior when running Test-DbaSpn. Somewhat applicable Issue is #8443 . Ideally this Issue and the aforementioned would be fixed but I think this one would take precedent.

Steps to Reproduce

Set-BreakPoint allcommands.p1 -line 142049
Test-DbaSpn RemoteServer
# Then I had to step through it until the applicable line discussed below.

Are you running the latest release?

Yes

Other details or mentions

The below code path is always executed with Test-DbaSpn. The if block appears to always return true because of its object nature.

https://github.com/dataplat/dbatools/blob/ced439756a413c366af63299fa25a34d930e8fa6/internal/functions/Invoke-ManagedComputerCommand.ps1#L70-L76

My suggested code rewrite would be.

        $hasException = $false
        foreach ($e in $result) {
            if($e.Exception){$hasException = $true}
        }
        if ($hasException) {

What PowerShell host was used when producing this error

PowerShell Core (pwsh.exe), Windows PowerShell (powershell.exe)

PowerShell Host Version

Local

Name                           Value                                                                                                                                                                            
----                           -----                                                                                                                                                                            
PSVersion                      5.1.19041.1682                                                                                                                                                                   
PSEdition                      Desktop                                                                                                                                                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                          
BuildVersion                   10.0.19041.1682                                                                                                                                                                  
CLRVersion                     4.0.30319.42000                                                                                                                                                                  
WSManStackVersion              3.0                                                                                                                                                                              
PSRemotingProtocolVersion      2.3                                                                                                                                                                              
SerializationVersion           1.1.0.1               

Remote

Name                           Value                                                                                                                                      
----                           -----                                                                                                                                      
CLRVersion                     2.0.50727.8806                                                                                                                             
BuildVersion                   6.1.7601.17514                                                                                                                             
PSVersion                      2.0                                                                                                                                        
WSManStackVersion              2.0                                                                                                                                        
PSCompatibleVersions           {1.0, 2.0}                                                                                                                                 
SerializationVersion           1.1.0.1                                                                                                                                    
PSRemotingProtocolVersion      2.1                                                                                                                                        

SQL Server Edition and Build number

Microsoft SQL Server 2008 (SP4-GDR) (KB4057114) - 10.0.6556.0 (X64) 
	Dec 28 2017 15:21:01 
	Copyright (c) 1988-2008 Microsoft Corporation
	Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (VM)

.NET Framework Version

Local

.NET Framework 4.8.4515.0

Remote

PSChildName                      Version       
-----------                      -------       
v2.0.50727                       2.0.50727.5420
v3.0                             3.0.30729.5420
Windows Communication Foundation 3.0.4506.5420 
Windows Presentation Foundation  3.0.6920.5011 
v3.5                             3.5.30729.5420
Client                           4.0.30319     
Full                             4.0.30319     

mattcargile avatar Jul 12 '22 02:07 mattcargile

Looks good, thanks for the catch! Please include this in the PR with the 2.0 fix

potatoqualitee avatar Jul 21 '22 12:07 potatoqualitee

The problem is that Test-DbaSpn does not use Invoke-ManagedComputerCommand in the new way. I will fix that in the next days...

andreasjordan avatar Oct 21 '22 14:10 andreasjordan

Will also address #8443 while refactoring the command.

andreasjordan avatar Oct 21 '22 14:10 andreasjordan

I got a change half done and never got a PR going. For this change, I ended up changing line 70 too if ($result.psobject.Properties.Name -contains 'Exception') {

It sounds like you have a more robust fix though and understand the history of the commands. It seems like the way line 70 is written, then commands using Invoke-ManagedComputerCommand need to return a [PSCustomObject] or the like and an [object[]] won't work with the current if logic which is why I was settling on using psobject to find an Exception Property.

mattcargile avatar Oct 21 '22 15:10 mattcargile