Tuesday, February 15, 2011

Get / Determine / Check your ESX version, major version or build version

To check your esx version, use in command line:


vmware -v
return for example in a 3.0.1 ESX
VMware ESX Server 3.0.1 build-32039

vmware -v
return for example in a 3.5 ESX
VMware ESX Server 3.5.0 build-238493

vmware -v
return for example in a 4.1 ESX
VMware ESX 4.1.0 build-260247



But If you need to know only version use

vmware -v | awk -F "uild" '{print $1}' | sed -e 's/[A-Za-z ]//g'

return in our 3 examples
3.0.1
3.5.0
4.1.0


In your script you can use this information into a variable

ESX_VERSION=`vmware -v | awk -F "uild" '{print $1}' | sed -e 's/[A-Za-z ]//g'`
echo $ESX_VERSION

return in our 3 examples
3.0.1
3.5.0
4.1.0




To know major version only

vmware -v | awk -F "uild" '{print $1}' | sed -e 's/[A-Za-z ]//g' | cut '-d.' -f1

return in our 3 examples
3
3
4


To know major and minor version only

vmware -v | awk -F "uild" '{print $1}' | sed -e 's/[A-Za-z ]//g' | awk -F "." '{print $1"."$2}'

return in our 3 examples
3.0
3.5
4.1


If you need to know build only

vmware -v | awk -F "uild-" '{print $2}'

return in our 3 examples
32039
238493
260247

Wednesday, February 9, 2011

Powershell to help you to sort, manipulate or consolidate CSV file

To help you to sort, manipulate or consolidate your information, Powershell is a very interesting tool.

First, you need a .csv file with this format (use excel file save as)


col1,col2,col3
c,cc,ccc
a,aa,aaa
b,bb,bbb
c,cc,ccc
b,bb,bbb


For example, to select only one column, just use colum name

Import-Csv .\test.csv | select col2



col2
----
cc
aa
bb
cc
bb


If your .CSV has ";" and not "," as delimiter you can change this by adding -Delimiter switch like that


Import-Csv .\test.csv -Delimiter ";" | select col2

(Warning: This switch does not work on Powershell 1.0)

Moreover, be sure to have a header else you will have a error
Import-Csv : The member "xxxx" is already present
And do not forget # is considered as a remark

Now, if you only need distinct col2 use -unique switch and Sort-object if you need to have only distinct field

Import-Csv .\test.csv | select -unique col2 | Sort-Object col2



col2
----
aa
bb
cc


To display each line with column 3 then column 1 then column 2 you can do like this

$content = import-csv ".\test.csv"
foreach ($line in $content) {
write-host $line.col3 $line.col2 $line.col1
}


result:

ccc c cc
aaa a aa
bbb b bb
ccc c cc
bbb b bb


or like this:

Import-Csv .\test.csv | select col3, col1, col2


result:

col3 col1 col2
---- ---- ----
ccc c cc
aaa a aa
bbb b bb
ccc c cc
bbb b bb


Or like this:

$Content = Get-Content ".\test.csv"
$arrLine = @( )
Foreach ($line in $Content) {
$line =$line.Trim()
If ($line.length -gt 0) {
$arrLine=$line.split(",")
write-host $arrLine[2] $arrLine[0] $arrLine[1]
}
}


result:

col3 col1 col2
ccc c cc
aaa a aa
bbb b bb
ccc c cc
bbb b bb