Monday, April 4, 2016

PowerShell Script to Email List of VMware Snapshots

     During scheduled maintenance is a good time to delete any snapshots in vCenter.  VMware recommends only keeping snapshots for up to 72 hours. ("Use no single snapshot for more than 24-72 hours." From: https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1025279

     Here is a PowerShell script that will gather all snapshots in vCenter and email a list to recipients:

Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer vcenter.server.name

$style = "<style>"
$style += "BODY{font-family: Arial; font-size: 10pt;}"
$style += "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style += "TH{border: 1px solid black; background: #dddddd; padding: 5px;}"
$style += "TD{border: 1px solid black; padding: 5px;}"
$style += "</style>"

$SendEmail = @{
SMTPServer = "IP or Name of SMTP Server"
From = Do_Not_Reply@domain.com
To = "user1@domain.com","user2@domain.com"
 Subject = "Current Snapshots"
}

$EmailBody = Get-VM | %{Get-Snapshot $_} | select @{N='Server Name';E={$_.VM}},@{N='Snapshot Name';E={$_.Name}},@{N='Snapshot Creation Date';E={$_.Created}} |
sort 'Server Name' | ConvertTo-Html -Head $style -Body "<H2>Testing script to email current list of snapshots that should be deleted.</H2>" | Out-String

Send-MailMessage @SendEmail -Body $EmailBody -BodyAsHtml

     The $style options included in the script make the email look better.  Here is a snippet of what an email will look like:


     As you can see there a couple snapshots hanging out longer than the 72 hour recommendation.  Those two machines are typically not running and only used for creating AppV packages.  The appropriate administrator will take care of those during the upcoming maintenance night...hopefully.

     This script could be set up to run as a scheduled task.  That way it will run without user intervention which will reduce steps required prior to maintenance night. 

-IT Bits




Wednesday, December 9, 2015

PowerShell - Delete Desktop.ini Files

Ever pull up file explorer to browse for a user's home directory only to find a bunch of folders listed as "Documents"?
    

Good luck trying to figure out which "Documents" folder is the one you want.  You can still get to the user's folder if you know the full name, though.  For instance, say the home folder is called UsernameOfPerson and the file path is \\server\path. Typing in \\server\path\UsernameOfPerson will open the folder.  But if you try and browse to the folder by clicking through them, you are met with a bunch of generic folders.  This occurs due to the creation of desktop.ini files.  Deleting the desktop.ini files will revert the folders back to their original folder names.  

But how do you quickly delete a bunch of files at once? PowerShell works great. Here is a PowerShell script that will delete all desktop.ini files located in the E: drive: 

GCI -File -Path E:\ -Include desktop.ini -Recurse -Force | foreach {$_.Delete()}

The path can be changed to any path you prefer, even UNC paths.  The "-File" option tells PowerShell to only look at files, not folders.  The "-Include desktop.ini" tells PowerShell to only look for files specifically named desktop.ini.  Setting these two requirements limits the damage that can be done when deleting files with a script.  This script could easily be used in a scheduled task if needed.  As always, test the script first before implementing in production.

            Have another way to remove or limit/prevent the creation of desktop.ini files?  Let us know in the comments.

-IT Bits


Monday, December 7, 2015

StatusCode 503 with vCenter Server Appliance

After checking on the status of systems with a few people in the IT department, I decided I could start working on a couple projects that need attention.  Not five minutes later I received an email informing me that vCenter was nonfunctional.  Mondays.  Ugh.

I forgot to get a screenshot of the actual error message, but both the web and client versions of our vCenter 6 server were throwing an error stating StatusCode 503, the server is unavailable.  After double-checking the SAN, network, and Internet, everything seemed to be functioning fine except vCenter itself.

I logged on to the host the vCenter server (VCSA6.domain.local) was running on and opened a console to the vCenter server.  The server seemed fine and opened without issue.  You may have noticed by the server name that our vCenter was deployed using the server appliance from VMware which means it's a Linux server.

After a reboot I logged into vCenter without issue and thought everything was good to go.  It was...for about two minutes until it crashed again.  I did some Google searching and found a couple bits about IT that pointed me in the proper direction.  For instance, I did not know their is a management URL for a vCenter Server Appliance by changing the port to 5480.

To get to management URL for the VCSA6 server, change port to 5480 and log in with root account:




There it was.  An error message about a disk with no more space available.

To verify, I logged into our VCSA6 server using SSH (Putty).

Enabled shell (Type: "shell --enabled True" and press Enter).

And listed the server's drives (Type: "df -h" and press Enter).

The following information was displayed:




Full disk verified.  Looks like the VHD needs expanded.  Then another problem:  The VM had a snapshot which needed removed before the VHD could be expanded.  With vCenter already unavailable I decided to shut down the VM and remove the snapshot.
While the VM was still powered off the VHD was increased to 150GB.  An error occurred with the following info:

"Reconfigure virtual machine VCSA6.domain.local Access to resource settings on the host is restricted to the server that is managing it: 'IP Address of VCSA Server'"

I decided to boot up the VM and expand the VHD during the two minutes vCenter would work before crashing.

After the VM was turned on the VHD increase did take place even though the error occurred.  Cool.  Looks like vCenter works again.

*Ten minutes later*

Yup.  Still working.  Time to send an email letting people know vCenter works again.


Additional info:

The SEAT VHD is used for Stats, Events and Tasks (SEAT) directory for the VMware PostgreSQL database. You can limit the size of the DB by setting VMware to retain task and event info for less time.