从管道首个参数取值问题:PowerShell集群信息脚本排查
Hey there, let's fix that empty State field issue you're facing. The core problem here is that Get-ClusterResource (which holds the resource runtime state) and Get-ClusterParameter (which stores IP configuration details) return separate sets of objects—you need to link them using shared properties to pull both pieces of data together properly.
Here's a refined script that will fetch exactly what you need: cluster name, AlwaysOn AG names, their associated IPs, and current resource state:
# Grab the target cluster name $targetCluster = Get-Cluster | Select-Object -ExpandProperty Name # Get all SQL Server Availability Group resources (your AlwaysOn instances) $alwaysOnGroups = Get-ClusterResource | Where-Object { $_.ResourceType.Name -eq "SQL Server Availability Group" } # Build a detailed dataset by linking AGs to their IP resources and state $clusterDetails = $alwaysOnGroups | ForEach-Object { $agName = $_.Name # Fetch IP resources directly dependent on this AG (adjust if your dependency structure differs) $ipResources = $_ | Get-ClusterResourceDependency | Select-Object -ExpandProperty Dependency | Where-Object { $_.ResourceType.Name -eq "IP Address" } foreach ($ipRes in $ipResources) { # Extract the actual IP address from cluster parameters $ipValue = Get-ClusterParameter -Resource $ipRes.Name -Name "Address" | Select-Object -ExpandProperty Value # Pull the current state directly from the IP resource object $currentState = $ipRes.State # Output a custom object with all your required fields [PSCustomObject]@{ ClusterName = $targetCluster AlwaysOnName = $agName AssociatedIP = $ipValue ResourceState = $currentState } } } # Display results in a clean, readable table $clusterDetails | Format-Table -AutoSize
Why your original script had empty State values
Chances are, you were trying to pull the State property from Get-ClusterParameter—but that cmdlet only stores static configuration parameters (like IP addresses), not runtime state data. The State property lives exclusively on objects returned by Get-ClusterResource, so you need to reference that object directly after linking it to the IP parameter data.
Quick Adjustments for Your Environment
- If your IP resources aren't directly dependent on the AG (e.g., they're in the same cluster group instead), replace the dependency check with this:
$ipResources = Get-ClusterResource | Where-Object { $_.OwnerGroup.Name -eq $_.OwnerGroup.Name -and $_.ResourceType.Name -eq "IP Address" } - Ensure you're running the script with cluster administrator privileges—without proper permissions, you might get partial or empty data.
- For remote clusters, add the
-Clusterparameter to all cluster-related cmdlets (e.g.,Get-Cluster -Cluster "RemoteCluster01").
内容的提问来源于stack exchange,提问作者Alen

