Saturday, November 14, 2015

Version 2 VM disk space detail send via email Powercli

1 year back I had written this script, and it became very much popular, Collect disk space detail and send it in email Powercli since then I started receiving emails for some more additions in the existing script. I am not a HTML guy but still tried to create one, Many of the people found they don't have to login each server to collect disk information, They just need to login vCenter (Powercli) which saves lots of time. Also to work this script correctly make sure all your VMs has VMWare Tools installed and running. Also my earlier script was pulling reports only for Windows and C drive.
I have improvised it little bit and added below information.
  • Information for all vmware vm disk usage,
  • Any disk has below 20% will be in Red.

This is the screenshot how collected information will looks like in the email.

Once you execute script you will see below results on the screen which can tell you from what vm and disk it is collecting information. I am running this one Windows server 2012 R2, Powercli version 5.5, vsphere 5.5.
Below is the code information
  #####################################    
  ## http://kunaludapi.blogspot.com    
  ## Version: 2    
  ## Tested this script on    
  ## 1) Powershell v4    
  ## 2) Powercli v5.5    
  ## 3) Vsphere 5.x    
  ##   Find and input your information in below lines in the script  
      ############################################################  
  ##   $vCenterServer = "vCenterserver"   
  ##   $vcenteruser = "domain\user"   
  ##   $vcenterpasswd = "Password"   
  ##       
  ##   From = "SpaceAlert@email.com"   
  ##   To = "Employee@email.com"   
  ##   SmtpServer = "smtp.exchange.com"   
  #####################################   
 begin {  
 Add-PSSnapin vmware.vimautomation.core   
 #vCenter username password   
 $vCenterServer = "vCenterserver"   
 $vcenteruser = "domain\user"   
 $vcenterpasswd = "Password"   
 #connect vcneter server    
 Connect-VIServer -Server $vCenterServer -User $vcenteruser -Password $vcenterpasswd   
  $FinalResult = @()   
  $Allvms = Get-View -ViewType “VirtualMachine” -filter @{”Guest.GuestState”=”running”}   
 } #begin  
 Process {  
 foreach ($vm in $Allvms) {   
     $Drives = $vm.Guest.Disk    
     Write-host "Collecting Information from $($vm.name)" -ForegroundColor Green  
     Foreach ($DriveC in $drives) {    
     #Calculations   
     $Freespace = [math]::Round($DriveC.FreeSpace / 1MB)   
     $Capacity = [math]::Round($DriveC.Capacity / 1MB)   
     $SpaceOverview = "$Freespace" + "/" + "$capacity"   
     $PercentFree = [math]::Round(($FreeSpace)/ ($Capacity) * 100)  
     $Disk = $DriveC.DiskPath.Substring(0,2)  
     #Report for all vms   
     if ($PercentFree -lt 20) {  
       $vmnameRD = "<tr><td bgcolor='#ff704d'>{0}</td>" -f $vm.name  
       $DiskRD = "<td bgcolor='#ff704d'>{0}</td>" -f $Disk  
       $FreesspaceRD = "<td bgcolor='#ff704d'>{0}</td>" -f $Freespace  
       $capacityRD= "<td bgcolor='#ff704d'>{0}</td>" -f $capacity  
       #$SpaceOverviewRD = "<td bgcolor='#ff704d'>{0}</td>" -f $SpaceOverview  
       $PercentFreeRD = "<td bgcolor='#ff704d'>{0}</td></tr>" -f $PercentFree  
       $finalResult += $vmnameRD  
       $finalResult += $DiskRD  
       $finalResult += $FreesspaceRD  
       $finalResult += $capacityRD        
       #$finalResult += $SpaceOverviewRD  
       $finalResult += $PercentFreeRD  
       Write-Host "`t `tCollected from $($DriveC.DiskPath) on $($vm.name)" -ForegroundColor Yellow  
     }#if  
     else {  
       $vmnameNL = "<tr><td>{0}</td>" -f $vm.name  
       $DiskNL = "<td>{0}</td>" -f $Disk  
       $FreesspaceNL = "<td>{0}</td>" -f $Freespace  
       $capacityNL= "<td>{0}</td>" -f $capacity  
       #$SpaceOverviewNL = "<td>{0}</td>" -f $SpaceOverview  
       $PercentFreeNL = "<td>{0}</td></tr>" -f $PercentFree  
       $finalResult += $vmnameNL  
       $finalResult += $DiskNL  
       $finalResult += $FreesspaceNL  
       $finalResult += $capacityNL  
       #$finalResult += $SpaceOverviewNL  
       $finalResult += $PercentFreeNL  
       Write-Host "`t `tCollected from $($DriveC.DiskPath) on $($vm.name)" -ForegroundColor Yellow  
     }#else  
   } #foreach drive  
 } #foreach vm  
 #region body  
 $Body = @"  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head>  
 <style>BODY{background-color:#ffffff;}  
 TABLE{border-width: 1px;border-style: solid;border-color: #808080;border-collapse: collapse;}  
 TH{border-width: 1px;padding: 0px;border-style: solid;border-color: #808080;background-color:#808080;color: white;}  
 TD{border-width: 1px;padding: 0px;border-style: solid;border-color: #808080;}</style>  
 </head><body>  
 <H2>VMs DiskSpace usage report</H2>  
 <table><colgroup><col/><col/><col/><col/><col/></colgroup>  
 <tr><th>VMName</th>  
 <th>Disk</th>  
 <th>Free(MB)</th>  
 <th>Total(MB)</th>  
 <th>%Free</th></tr>  
 $finalResult   
 </table>  
 </body></html>  
 "@  
 #endregion body  
 }#process   
 end {    
  #Send Email   
  $messageParameters = @{   
  Subject = "Alert! VMs with less than 20% on diskspace marked Red"  
  Body = $Body  
  From = "SpaceAlert@email.com"   
  To = "Employee@email.com"   
  SmtpServer = "smtp.exchange.com"   
  }  
 Send-MailMessage @MessageParameters -BodyAsHtml
 }  

Check earlier blog:
Collect disk space detail and send it in email Powercli

vSphere Datastores inventory report powercli - Volume 2

Thursday, November 12, 2015

Powershell add A resource records in DNS Domain oneliner


This is a small article on how add A resource record in DNS server. Also in the last I am going to show how to add Resource record inside domain using powershell. Here I will be demostrating below 3 cmdlets. 
Add-DnsServerResourceRecordA
Add-DnsServerResourceRecord
Get-DnsServerResourceRecord

My DNS server is running on windows server 2012 R2. In this tutorial I have downloaded and  installed RSAT remote server administration tools on my Windows 8.1 desktop, so I can configure dnsserver remotely, once RSAT is installed go to Control Panel >> Programs >> Turn windows Features On and off  >> Remote Server administration tools,  install DNS administration tools,  now I can manage my DNS server remotely, specifically I have installed DNSSERVER powershell module. (Whenever first time I run any DNS related command to DNSServer module it loads it automatically, I dont need to import it explicitly on windows 2012 and 8)

Checkout my another Article: Powershell find missing associated PTR resource records in DNS Server
 
As above screenshot I have added DNS record entry for TestMachine01 - 192.168.33.150. Open  Powershell, and fire up below command 
Add-DnsServerResourceRecordA -Name TestMachine01 -IPv4Address 192.168.33.150 -ZoneName vcloud.lab -ComputerName Ad001 -CreatePtr

Note: To view the changes in DNS manager you will need to right click and refresh the zone, if you have already opened DNS manager.

I have break down command in as below .
Add-DnsserverResourceRecordA = This is the CMDLET used to add A resource record only.
-Name TestMachine01 = -Name is parameter and TestMachine01 is computer name of A resource record name.
-IPv4Address 192.168.33.150 = This self explanatory.
-ZoneName vCloud.lab = vCloud.lab is my zonename.
-ComputerName AD001 = -ComputerName is parameter for Dnsserver, AD001 is my DNSserver.
-CreatePtr = This is optional, if you want to create ptr (Reverse lookup record entry).

This part is to add only single DNS host entry, what if you want to add multiple resource record from excel file, Here is below step by step tutorial. I have saved excel file as csv. (and it is saved in c:\temp location).

When I open CSV in notepad, data looks like this
Now in the powershell fire below command 
Import-Csv C:\Temp\DnsEntries.csv | ForEach-Object { Add-DnsServerResourceRecordA -Name $_.Name -IPv4Address $_.IPv4Address -ZoneName vcloud.lab -ComputerName Ad001 -CreatePtr}

This will import csv file and it will piped into foreach-object loop, then foreach row record is added (value of $_ is always get changed row changes)
Now we have added these records how do I verify it. use Get-DnsServerResourceRecord command
For single computer:
Get-DnsServerResourceRecord -Name TestMachine01 -ZoneName vCloud.lab -ComputerName Ad001 

For mulitple computer we will use the same CSV file format. (IPv4Address column is not required)
Import-csv C:\temp\DnsEntries.csv | ForEach-Object {Get-DnsServerResourceRecord -Name $_.Name -ZoneName vCloud.lab -ComputerName Ad001}

This tutorial ends here, and now I want to show how to add resource record entry in Domain created under DNS Zone. I have one Dns zone name local and under it there are mulitple Domains. and I will be using Mylab domain as a demo.
Earlier I used command Add-DnsServerResourceRecordA as a demo in this I am showing another Powershell command Add-DnsServerResourceRecord. differance between both is there A missing in the last of Add-DnsServerResourceRecordand only capable of creating any record, in contrary Add-DnsServerResourceRecordA shown earlier can only create A resource record. Command can create all type of record but the resource type need to be mentioned, as i done below -A

Add-DnsServerResourceRecord -Name Lab002.MyLab -IPv4Address 192.168.32.152 -ZoneName Local -ComputerName Ad001 -A
 
To breakdown above command I have suffixed .Mylab domainName in the -Name and additional parameter is -A to declare it is host A record.

For adding multiple host A record entries use below one-liner.
Import-Csv C:\temp\DnsEntries.csv | ForEach-Object {Add-DnsServerResourceRecord -Name $_.Name -IPv4Address $_.IPv4Address -ZoneName Local -ComputerName Ad001 -A}

And in the last to collect and verify added records by using below oneliners
For single computer
Get-DnsServerResourceRecord -Name Lab002.MyLab -ZoneName Local -ComputerName Ad001

For multiple computer
Import-Csv C:\temp\DnsEntries.csv | ForEach-Object {Get-DnsServerResourceRecord -Name $_.Name -ZoneName Local -ComputerName Ad001}

Sunday, November 1, 2015

Poor man's inventory website using Powershell

I am always fascinated by HTML Inventory webpages scripts written by Don Jones's EnhancedHtml module and Alan Renouf's vCheck report. And always wanted to write same kind of my own script which will generate information data in the Web pages and they are linked to each other, Now I would like to proudly present my own version of script which creates complete website for given computer. 
It took me around 3-4 days to write this script, For all the Powershell writing it took me 1 hour, But combining and calibrating HTML and CSS coding with PowerShell took me all those 3-4 days, As this was my first time to write WebPage was great experience for me. (I don't have any knowledge on HTML, I written all these stuff by reading tutorial on http://www.w3schools.com, They have some great serious tutorials on HTML, JavaScript, CSS, Angular and so on... I went through almost all the tutorial on the site) You can download Web site creation script below. File name is ComInvScript.zip. All my testing done is on Windows 2012 R2, Windows 7, they are working fine, and collecting all required reports. This is just for demo purpose and I am still working on this script to make it compact and understandable.
Download Script
Download Script files

Here is this cool video how to run this script and how the collected and generated wesite looks like. Work is in still progress.

As of now this is just a demo script I have downloaded to get feedback from you which pulls some good information using WMI., Computer information pages are all I have designed and thinking what information I can put and thinking more on web pages designing, I tried to make Computer Information pages as fancy as possible and tried to add more data, now looking feedback from you.

This is a step by step demo how you can use the given script to build your own website hosted on either IIS or Apache.

Download the zip file, (I downloaded it on c:\scripts). and extracted it in same folder with same zip file name.
Once you extract zip file you will find there are 3 contents (1 folder and 2 files).
1) Images - Folder contains all the images required for your website
2) Get-ComputerInventory.ps1 - This script file will be executed to collect information from serverslist.txt. and create linked webpages for each server.
3) Serverlist.txt - You will have to put your server names in This text file.

Note: Do not to rename or change any file of folder names, and keep them in same folder.
Once you are all set with server list names. Open powershell (make sure you have script execution policy set to remotesigned at list. checkout my earlier blog to set-executepolicy), Next change (cd) the directory where my script and other supported files are located and execute that ps1 file.

make sure you are prefixing ./ the ps1 file.
./Get-ComputerInventory.ps1
and hit enter.


When the ps1 is executed completely, It will open inventory.html file, and also you can check the all the generated web pages under C:\Temp\WebSiteData. Here I am attaching those HTML files here to view how this website looks like. extract it anywhere you want and open InventoryList.html in browser. First page InventoryList.html is not well designed and I am still working on it.
WebSiteDataDemo
WebSiteDataDemo.Zip

Stay tune for more updates.
I have put this project on GitHub.