如何从iSCSI Target中提取独立卷名称?
Absolutely! The NodeAddress returned by Get-IscsiTarget follows a structured format (varies a bit by your iSCSI target provider, but usually something like iqn.1991-05.com.microsoft:<server-name>-<volume-name>-<unique-guid>). We can parse this string to pull out just the volume name with a few practical PowerShell approaches:
Method 1: String Splitting (Simple, Format-Dependent)
If your NodeAddress uses consistent separators (like colons and hyphens), split the string and pick the segment that holds your volume name. Adjust the index based on your actual address structure:
Get-IscsiTarget | ForEach-Object { # Split the address using colon and hyphen as delimiters $addressSegments = $_.NodeAddress.Split(':-') # Example: If volume name is the 3rd segment (tweak this index for your environment) $volumeName = $addressSegments[2] # Output both values for verification [PSCustomObject]@{ NodeAddress = $_.NodeAddress ExtractedVolumeName = $volumeName } }
Pro Tip: Run Get-IscsiTarget | Select-Object NodeAddress first to check your address format, then adjust the $addressSegments index to match where your volume name resides.
Method 2: Regular Expressions (Flexible for Variable Formats)
Regex works better if your NodeAddress has variable-length segments before the volume name. For example, if your volume name sits between the last hyphen before a GUID-like unique ID, use this:
Get-IscsiTarget | ForEach-Object { # Regex pattern: captures text between the last hyphen (before the final GUID) and the preceding separator # Adjust the pattern to match your specific NodeAddress structure if ($_.NodeAddress -match ':(?:.*)-([^-]+)-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$') { $volumeName = $matches[1] } else { $volumeName = "Unable to parse volume name" } [PSCustomObject]@{ NodeAddress = $_.NodeAddress ExtractedVolumeName = $volumeName } }
Test the regex with your actual NodeAddress values to refine the pattern—it handles most common iSCSI address formats smoothly.
Method 3: Directly Fetch Target Name (For Microsoft iSCSI Targets)
If you're using Microsoft's built-in iSCSI Target Server, skip parsing entirely and use Get-IscsiServerTarget to get the target/volume name directly:
Get-IscsiServerTarget | Select-Object TargetName, NodeAddress
This returns the exact name you assigned to the iSCSI target, no parsing required.
内容的提问来源于stack exchange,提问作者Samselvaprabu

