Wednesday, July 24, 2013

Power shell Errors


Error 1:
BUG: "Old format or invalid type library" error when automating Excel

Solution : Change the regional settings to English . 




Wednesday, April 10, 2013

Add Notes / Description

Following one liner can be used to add notes to a virtual guest.


Get-VM "Server Name"  | Set-VM -Notes "Description" 

Please use this on your own risk. This command will over-write the Description .

To add notes to multiple VMs 

Import-Csv "c:\Temp\vmlist.csv" | %{Set-VM  $_.VMName -Notes $_.Note -Confirm:$false}

The CSV should have vmname and the relevant notes.

Wednesday, April 3, 2013

Event Log Monitoring

Following script can be used to pull the report which has all the errors reported from start date to end date.



get-vievent -MaxSamples 3 -Start 03/01/2013 -Finish 04/03/2013 -Types error | Select CreatedTime , UserName , FullFormattedMessage | Export-Csv c:\Temp\error.csv

Monday, February 18, 2013

Snapshot reporting


Following script can be used to generate a report on snapshots on the virtual platform . Report will export Virtual Machine Name , snapshot size , the name given while creating the snapshot and the created date . 




Before running the script we need to connect to the VC server using Connect-VIServer.

Result of get-vm



PowerState              : PoweredOn
Version                 : v9
Description             : Test machine
Notes                   : Test machine
Guest                   : TestingServer:Microsoft Windows Server 2003 Standard (32-bit)
NumCpu                  : 1
MemoryMB                : 2048
MemoryGB                : 2
HardDisks               : {Hard disk 1, Hard disk 2}
NetworkAdapters         : {Network adapter 1}
UsbDevices              : {}
CDDrives                : {CD/DVD drive 1}
FloppyDrives            : ESXhost.domain.com
HostId                  : HostSystem-host-123
VMHostId                : HostSystem-host-123
VMHost                  : ESXhost.domain.com
VApp                    : 
FolderId                : Folder-group-v9
Folder                  : Discovered virtual machine
ResourcePoolId          : ResourcePool-resgroup-223
ResourcePool            : Development
PersistentId            : 501dee04-20f2-ec80-a40e-4619b33a9dd2
UsedSpaceGB             : 26.416720500215888023376464844
ProvisionedSpaceGB      : 26.416720500215888023376464844
DatastoreIdList         : {Datastore-datastore-230}
HARestartPriority       : ClusterRestartPriority
HAIsolationResponse     : AsSpecifiedByCluster
DrsAutomationLevel      : AsSpecifiedByCluster
VMSwapfilePolicy        : Inherit
VMResourceConfiguration : CpuShares:Normal/1000 MemShares:Normal/10240
Name                    : TestingServer
CustomFields            : {}
ExtensionData           : VMware.Vim.VirtualMachine
Id                      : VirtualMachine-vm-102
Uid                     : /VIServer=adm@vcserver:443/VirtualMachine=VirtualMachine-vm-102/


Result Snapshot size includes - 

  1. .vmdk files - delta files stating the diff between the current state of disk and the state that existed when snapshot was taken.
  2. -delta.vmdk file => Raw data of disk
  3. .vmsd - snapshot information file. Used fir snapshot manager
  4. .vmsn - active mem state while taking snapshot



Get-snapshot retrieves virtual machine snapshot information from vCenter.



Wednesday, February 13, 2013

Virtual Platform Change history

Following script can be used to find the latest changes to the virtual platform and a mail will be sent to the admin ID . 


## Change history 
## HTML Configuration 
$a = ”<style>“
$a += ”body { background-color:#EEEEEE; }”
$a += ”body,table,td,th { font-family:Tahoma; color:Black; Font-Size:10pt }”
$a += ”th { font-weight:bold; background-color:#CCCCCC; }”
$a += ”td { background-color:white; }”
$a += "</style>"


Connect-VIServer VcServer -User admin -Password pwd

$date=Get-Date
$Folder= "c:\Temp"
$FileName= "$Folder\Changehistory{0}{1:d2}{2:d2}.html" -f $date.Year,$date.Month,$date.Day

$Report= @()
ConvertTo-Html –title "ChangeHistory " –body "<H4>Date and time</H4>",$date -head $a  | Out-File $FileName

$Report= Get-VIEvent -maxsamples 20000 | where {$_.Gettype().Name -eq "VmRemovedEvent"} | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage -First 20
$report | ConvertTo-Html -head $a -Body "<H4> VM guests Removed  - last 7 days  </h4>" | Out-File -append $FileName

$Report = Get-VIEvent -MaxSamples 20000 | where {$_.gettype().Name -eq "VmCreatedEvent" } |sort CreatedTime -Descending |select createdTime , Username , FullformattedMessage  -First 20
$report | ConvertTo-Html -head $a -Body "<H4> VM guests created  - last 7 days  </h4>" | Out-File -append  $FileName

$Report= Get-VIEvent -MaxSamples 20000 | where {$_.gettype().Name -eq "VmbeingClonedEvent" } | Sort createdTime -Descending |select createdtime  , username , FullformattedMessage  -first 20
$report | ConvertTo-Html -head $a -Body "<H4> VM cloned Last 7 days</h4>" | Out-File -append  $FileName


$Report= Get-VM | Get-Snapshot | Select-Object vm , SizeMB
$report | ConvertTo-Html -head $a -Body "<H4> SnapShot Details</h4>" | Out-File -append  $FileName

$Report= Get-Datastore | Select-Object name , CapacityMB , FreeSpaceMB 
$report | ConvertTo-Html -head $a -Body "<H4> DataStore details </h4>" | Out-File -append  $FileName

## Email the report 

function SendMail{
$sender = "Recipient@domain.com"
$recipient = "Recipient@domain.com"
$CC = "CCRecipient@domain.com"
$BCC = "BCCRecipient@domain.com"
$mailserver = ""
$subject = "Change History "
 
$body = "Report Attached"
 
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
$attachment = new-object System.Net.Mail.Attachment $FileName
$msg.CC.Add($CC)
Uncomment to send BCC
# $msg.BCC.Add($BCC)
$msg.Attachments.Add($attachment)
$client = new-object System.Net.Mail.SmtpClient $mailserver
$client.Send($msg)
$attachment.Dispose()}
SendMail

Disconnect-VIServer Vcserver -Confirm:$false



Basic Command-lets

Folks , 

This blog is meant for learning power cli scripts . Going forward I will  be amending the scripts which we admins use on day today basis .

Some of the basic command-lets 

  1. Connect and disconnect from VC server

    Connect-VIServer VCserver -User admin -Password pwd 
    Disconnect-VIServer VCserver  -Confirm:$false
  2. Export report to CSV format
    Export-Csv $FileName
  3. Export report to HTML file
    $date=Get-Date
    $Folder= "c:\Temp"
    $FileName= "$Folder\Changehistory{0}{1:d2}{2:d2}.html" -f $date.Year,$date.Month,$date.Day
    $Report= @()
    ConvertTo-Html –title "ChangeHistory " –body "<H4>Date and time</H4>",$date -head $a  | Out-File $FileName
    $Report= Get-VIEvent
    $report | ConvertTo-Html -head $a -Body "<H4> VM guests Removed  - last 7 days  </h4>" | Out-File -append $FileName
  4. Email a report

    function SendMail{
    $sender = "sender@domain.com "
    $recipient = "Recipient@domain.com "
    $CC = "CCRecipient@domain.com"
    $BCC = "BCCRecipient@domain.com"
    $mailserver = "smtp.domain.com"
    $subject = "Change History "
    $body = "Report Attached"
    $msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
    $attachment = new-object System.Net.Mail.Attachment $FileName
    $msg.CC.Add($CC)
    $msg.BCC.Add($BCC)
    $msg.Attachments.Add($attachment)
    $client = new-object System.Net.Mail.SmtpClient $mailserver
    $client.Send($msg)
    $attachment.Dispose()}
    SendMail

  5. Get Command-lets
    As the name say get command-lets are used to get the properties of the object.
  6. Set Command-lets

    These are used to set the attribute of a object