Wednesday, January 22, 2014

Calculate csv column data and append it in same CSV.

Calculating your CSV column data (sum)  and appending it at the end.

I have named the csv file data.csv and kept it in c:\temp and the CSV file contents looks like this



After running below script.


 $csv= import-csv c:\temp\Data.csv  
 $Numcpu = $csv.Numcpu | measure-object -sum  
 $MemoryTotalGB = $csv.MemoryTotalGB | measure-object -sum  
 $object = New-Object PSObject  
 $object | Add-Member -Name Name -Value "Total" -MemberType NoteProperty  
 $object | Add-Member -Name NumCpu -Value $Numcpu.sum -MemberType NoteProperty  
 $object | Add-Member -Name MemoryTotalGB -Value $MemoryTotalGB.sum -MemberType NoteProperty  
 $object | Export-Csv c:\temp\Data.csv -Append  





Source: https://communities.vmware.com/message/2337062#2337062


Thursday, January 9, 2014

Install softwares on redhat linux using powershell

After deploying trend micro deep security today, I created new task for my self to install its agent rpm on around 150 linux servers. I am not very good in linux scripting, and thought lets try it from windows powershell. In below script i have used pscp.exe and plink.exe tools to connect to linux server from windows. I have mentioned which fiield in the script you need to edit. Copy script from at the end of this blog.


In the script my notepad contents (ip addresses) are like this.

Commands.txt file contents are as below
Results look like this if you are successful.


You might get error something like below. as first time it will cache linux servers key and store in registry, for this servers you will need to rerun the script, and next time it will be succeeded.

root@ password:
Lost connection
PLINK.EXE : The server's host key is not cached in the registry. You
At line:12 char:26
+     Write-Output "yes" | PLINK.EXE -ssh $remoteserver -P 22 -pw $password -m $co ...
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (The server's ho...e registry. You:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 dc2:b4:56:aa:05:9c:a9:b8:4d:af:91:fb
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)
chmod:
cannot access `/tmp/Agent-RedHat_EL6-9.0.0-2008.x86_64.rpm'
: No such file or directory
error:
open of /tmp/Agent-RedHat_EL6-9.0.0-2008.x86_64.rpm failed: No such file or directory





 #####################################  
 ## http://kunaludapi.blogspot.com  
 ## Version: 1  
 ## Tested this script on  
 ##  1) Powershell v3  
 ##  2) Powercli v5.5  
 ##  3) Vsphere 5.x  
 ##  4) Redhat 6.4
 #####################################    

$iplist = Get-Content -path "c:\temp\iplist.txt"  
 $password = "Extrasecret"  
 $fileSource = "C:\temp\Agent-RedHat_EL6-9-2008.x86_64.rpm"  
 $filedestination = "/tmp/"  
 $user = "root"  
 $command = "C:\temp\commands.txt"  
 foreach ($ip in $iplist) {  
   $remoteserver = $user+"@"+$ip  
   $destination = $remoteserver+":"+$filedestination  
   Write-output $password | PSCP.EXE $fileSource $destination  
   Write-Output "yes" | PLINK.EXE -ssh $remoteserver -P 22 -pw $password -m $command  
 }