Answered by:
Split output and combine

Question
-
I am working on a script that gets all virtual machine configuration location with the goals to display the VM name and volume# in the ClusterStorage directory on a Hyper-V node in a failover cluster.
The get of the 'ConfigurationLocation' property of Get-VM is as follows:
$nodes = Get-ClusterNode foreach ( $node in $nodes ) { Get-VM -ComputerName $node -VMName * | Select-Object ConfigurationLocation }
The output is then:
C:\ClusterStorage\Volume1\vmname1
C:\ClusterStorage\Volume2\vmname2
C:\ClusterStorage\Volume3\vmname3
C:\ClusterStorage\Volume4\vmname4
C:\ClusterStorage\Volume5\vmname5But I want only the "Volume#" and "vmname".
So I've been reading about split and regex and output-string (for the Get-VM line) but I can't figure it out.I can split using "\", but when I try to combine the output isn't alright.
At one moment I has two lines for each $split[#].Ideally I want to sort on the Volume# as well, because when a LUN (volume) becomes unavailable, we need to inform the customers of the affected virtual machines.
I hope someone can give me some pointers.
Split and regex are new to me, and I can't seem to find the right information for what I want to do.
Everything online is about arrays of text or text files.Monday, December 17, 2018 1:58 PM
Answers
-
$nodes = Get-ClusterNode foreach ( $node in $nodes ) { Get-VM -ComputerName $node -VMName * | Select-Object -Property VMName,@{Name='Volume';Expression={(($_.ConfigurationLocation).split('\'))[2]}} }
As far as I can see at the moment is the volume not available as a seperate property. So you can use string acrobatics to extract the volume. ;-) :-DLive long and prosper!
(79,108,97,102|%{[char]$_})-join''
- Marked as answer by Tom Weustink Monday, December 17, 2018 2:19 PM
Monday, December 17, 2018 2:14 PM
All replies
-
$nodes = Get-ClusterNode foreach ( $node in $nodes ) { Get-VM -ComputerName $node -VMName * | Select-Object -Property VMName,@{Name='Volume';Expression={(($_.ConfigurationLocation).split('\'))[2]}} }
As far as I can see at the moment is the volume not available as a seperate property. So you can use string acrobatics to extract the volume. ;-) :-DLive long and prosper!
(79,108,97,102|%{[char]$_})-join''
- Marked as answer by Tom Weustink Monday, December 17, 2018 2:19 PM
Monday, December 17, 2018 2:14 PM -
That works really nice. I need to look up how to do that string acrobatics :-)
I see what you did there, but understanding it is something else for now.The [2] part is saying ignore the first two parts and only show the next part?
Monday, December 17, 2018 2:19 PM -
The -Split operator or the .split() method of strings splits a string on a charachter you specify into an array of parts of the string. With the number in brackets like this [2] you specify wich part you want to output (index starts by 0).
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
- Edited by BOfH-666 Monday, December 17, 2018 2:29 PM
Monday, December 17, 2018 2:28 PM -
I read up on -Split and .split() method. Quite confusing how it's explained all over the place.
That the index starts at 0 makes sense. I think I only saw (on the online articles) starting at 1...Monday, December 17, 2018 3:00 PM -
-split is a powershell operator that uses regular expressions, and .split() is a string .net method that doesn't.
https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.7.2
- Edited by JS2010 Monday, December 17, 2018 3:29 PM
Monday, December 17, 2018 3:14 PM