Test-DbaPath Inconsistent return type with SqlInstance single instance vs list
Verified issue does not already exist?
I have searched and found no existing issue
What error did you receive?
Not an error, but a surprise (probably because of PowerShells unrolling behavior).
When I call Test-DbaPath with a list of SqlInstance objects it'll create a different output type (objects) than when invoked using just a single SqlInstance (bool).
Steps to Reproduce
[string]$primary_fqdn = "lab-db01.local"
[Microsoft.SqlServer.Management.Smo.Server]$DbaInstanceNodePrimary = Connect-DbaInstance -SqlInstance $primary_fqdn
[Microsoft.SqlServer.Management.Smo.Server[]]$DbaInstanceNodeSecondary = @()
if (-not [String]::IsNullOrEmpty($ag)) {
$DbaInstanceNodeSecondary = Get-DbaAgReplica -SqlInstance $DbaInstanceNodePrimary -AvailabilityGroup $ag -EnableException | Where-Object {$_.Name -ne ($DbaInstanceNodePrimary .ComputerName) } | ForEach-Object {Connect-DbaInstance -SqlInstance $_.Name}
}
# 1st case, single SqlInstance object
(Test-dbapath -SqlInstance $DbaInstanceNodePrimary -Path "C:\" -EnableException).FileExists
# ParentContainsErrorRecordException: The property 'FileExists' cannot be found on this object. Verify that the property exists.
# 2nd case, list with two or more SqlInstance objects
(Test-dbapath -SqlInstance $($DbaInstanceNodePrimary;$DbaInstanceNodePrimary) -Path "C:\" -EnableException).FileExists
# True
# True
# 3rd case, with unintended behavior, when DbaInstanceNodeSecondary is empty equal to 1st case.
# otherwise equal to 2nd case.
# Passing an implicit list type (no type definition for the inline created one
(Test-dbapath -SqlInstance $($DbaInstanceNodeSecondary;$DbaInstanceNodePrimary) -Path "C:\" -EnableException).FileExists
# 4th same as 3, but when list contains one element
# Passing a variable with an explicit list type of [Microsoft.SqlServer.Management.Smo.Server[]]
(Test-dbapath -SqlInstance $DbaInstanceNodeSecondary -Path "C:\" -EnableException).FileExists
# 5th case, it's even worse when piping
$DbaInstanceNodePrimary, $DbaInstanceNodePrimary | Test-DbaPath -Path "C:\"
# true
#
# SqlInstance : lab-db01.local
# InstanceName : MSSQLSERVER
# ComputerName : lab-db01
# FilePath : C:\
# FileExists : True
# IsContainer : True
Please confirm that you are running the most recent version of dbatools
2.1.0
Other details or mentions
Return type should be consistent.
At least case #4 (explicit list type) and #2 (list) should always have the same return type.
And #5 must be consistent.
What PowerShell host was used when producing this error
VS Code (integrated terminal)
PowerShell Host Version
Name Value
PSVersion 7.3.7 PSEdition Core GitCommitId 7.3.7 OS Microsoft Windows 10.0.17763 Platform Win32NT PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0
SQL Server Edition and Build number
Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64) Oct 8 2022 05:58:25 Copyright (C) 2022 Microsoft Corporation Developer Edition (64-bit) on Windows Server 2019 Datacenter 10.0 <X64> (Build 17763: ) (Hypervisor)
.NET Framework Version
.NET 7.0.11