I am very new to Powershell and was wondering if someone could help me out with parsing the following command as well as provide an explanation regarding the logic..
$BranchNumber = "2.14Main"
$MajorVersion = $BranchNumber.Split('.')[0]
$MinorVersion = $BranchNumber.Split('.')[1]
$PatchVersion = $BranchNumber.Split('.')[2]
$HotfixVersion = $BranchNumber.Split('.')[3]
Write-Host $MajorVersion $MinorVersion $PatchVersion $HotfixVersion
The Write-Host that I am looking for needs to be the following.
2 14 0 0
Why do you think that code should do what you want?
Here is a line by line explanation of that code:
Give variable BranchNumber the value of string "2.14Main"
Give variable MajorVersion the value of the first element of the array resulting from the String.Split method using
the character . as a delimiter ( '2' is that value )
Give
variable MinorVersion the value of the second element of the array resulting from the String.Split method using the character . as a delimiter ( '12Main' is that value )
Give
variable PatchVersion the value of the third element of the array resulting from the String.Split method using the character . as a delimiter (there is no third element. so the value of PatchVersion is set to the empty string)
Give
variable HotfixVersion the value of the fourth element of the array resulting from the String.Split method using the character . as a delimiter (there is no fourth element so the value of HotfixVersion is set to the empty string)
Output to host the values of variables MajorVersion MinorVersion PatchVersion HotfixVersion with a space between
the values. (this results in
"2 14Main "
written to the host but without the doublequotes)
-
Edited by
LarryWeiss
5 hours 59 minutes ago