Saturday, April 12, 2014

Download logs from all esxi host to desktop to review it later

I got some issue within my virtual environment, and found something suspicious in the ESXi logs.

But due to limited time I was not able to review the logs on the other esxi server. here I have created a script to download logs from all esxi host to your desktop or shared location. which will be helpful to review them later. 

I have used pscp.exe (kept under c:\windows\system32 folder) tool to connect to download logs.

  #####################################   
  ## http://kunaludapi.blogspot.com   
  ## Version: 1   
  ## Tested this script on   
  ## 1) Powershell v3   
  ## 2) Powercli v5.5   
  ## 3) Vsphere 5.x   
  #####################################  
$vcenteruser = "domain\user"
$vcenterpasswd = "Password"
$vCenterServer = "vCenterserver"

Connect-VIServer -Server $vCenterServer -User $vcenteruser -Password $vcenterpasswd    

 $iplist = Get-VMHost  
 $password = "Newsecret"   
 $user = "root"   
  foreach ($ip in $iplist) {   
   mkdir "c:\temp\logs\$ip"  
   cd "c:\temp\logs\$ip"  
   $SSHservice = Get-VMHost $ip | Get-VMHostService | where {$psitem.key -eq "tsm-ssh"}   
   if ($SSHservice.Running -eq $False) {   
    Get-VMHost $ip | Get-VMHostService | where {$psitem.key -eq "tsm-ssh"} | Start-VMHostService     
   }   
   
   $filesource = "/var/log/*"   
   $filedestination = "c:\temp\"  
   $source = $filedestination+$ip+"\vmkwarning.log"  
   $remoteserver = $user+"@"+$ip  
   $destination = $remoteserver+":"+$filesource  
   $dot = "."  
   Write-output "y" | pscp.exe -r -pw $password $destination $dot  
   Get-VMHost $ip | Get-VMHostService | where {$psitem.key -eq "tsm-ssh"} | Stop-VMHostService -Confirm:$false  
  }   

Warning: All the testings are performed in lab environment.

No comments: