.PARAMETER Credential
Allows you to login to the computer (not sql instance) using alternative Windows credentials

.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

$regroot=($sqlwmi.AdvancedProperties|Where-ObjectName-eqREGROOT).Value$vsname=($sqlwmi.AdvancedProperties|Where-ObjectName-eqVSNAME).Valuetry{$instancename=$sqlwmi.DisplayName.Replace('SQL Server (','').Replace(')','')# Don't clown, I don't know regex :(}catch{# Probably because the instance name has been aliased or does not exist or something# here to avoid an empty catch$null=1}$serviceaccount=$sqlwmi.ServiceAccount

try{$results=Invoke-Command2-ComputerName$resolved.FullComputerName-Credential$Credential-ArgumentList$regroot,$vsname,$instancename-ScriptBlock$scriptblock-ErrorActionStop-Rawforeach($resultin$results){[pscustomobject]$result}}catch{Stop-Function-Message"Failed to connect to $($resolved.FullComputerName) using PowerShell remoting!"-ErrorRecord$_-Target$instance-Continue}}}}