Friday, May 27, 2016

PowerCLI: Automatically Power on VMs from the Saved CSV file

It's the Powershell script using PowerCLI to power on a bunch of VMs when multiple servers go offline or during disaster recovery. Not using any third-party tools, I got the idea to do it in PowerCLI to backup the current Powerstate of the VMs to csv file and compare it when powering on VMs, so that you do not mistakenly turn on VMs that are initially powered off. I added the progress bar to get the progress state of powering on VMs.

What this script will do:
1) It will backup the current Power State of VMs to csv file. It will create the new csv file if not already created.
2) If the file is already created, it will check the current power state of VMs by comparing with the csv file.
3) If the Powered On server in the list is found as powered off in vCenter or esxi, it will power on all VMs.

Please note that you will need to connect the vCenter/Esxi before running the script. Also, you will need to disconnect the vCenter/Esxi connection once the job is finished.

Use the the following command to connect to the host.
Connect-VIServer -Server yourserver -Credential (get-credential)

Use the following command to disconnect from host.
Disconnect-VIServer -Server yourserver -confirm:$false



Sunday, May 22, 2016

PowerCLI: Get the Vmnic CDP Information of Esxi Hosts

In these days, I do have to backup the CDP info of our current esxi hosts and fortunately I found script this blog and official VMware site to discover information. Only I made little changes to the script to suite my environment. You need Powercli (PowerCLI 5.5 is here) already installed, and must be connected to vCenter of specific Esxi host before running the script.

If you’re not connected to vCenter, you can use the command:
Connect-VIServer -Server yourServerNameOrIP -Credential (Get-Credential)

After you have finished running the script, you can disconnect the vCenter Server by using the following command.
Disconnect-VIServer -Server yourServerNameOrIP

Note: You can also add the esxi hostnames in the hostlist.txt to get the CDP info for specific hosts only. If the hostlist.txt file doesn’t exit, then it will gather information for all Esxi hosts.



Monday, May 16, 2016

Powershell Debugging Made Easy: A good start tutorial



Scripting is a gift or a painful thing for system admins, it sometimes becomes frustrating when your script encounter errors. There are times when you spot an error yet can’t find where it is located or the lines when it occurred, only then you've to go for line-by-line debugging. Powershell has built-in debugging tools for this. Thanks Powershell !
There I found 2 methods for debugging. You can use anything based on your preference.
  1. Write-Debug, where you need to manually suspend/continue the running script to inspect.
  2. Set-BreakPoint, which automatically enters into the pre-defined breakpoints, based on Variables or Line number, you defined in the current powershell session.
Since I want to show you how to deals with simple debugging methods, I’m not going with the complex script and conditional debugging. So, let’s start with the simple script. Here, we will add two values 2 and 5. Needlessly to say, the output is 7.  But, it gives out 25 in the result which is unexpected. This is where we should give a try on powershell debugging mode.

Sunday, May 8, 2016

Powershell: Automatically Check and Correct multiple NTP Clients

These days I have been digging in with Powershell to write utility scripts that helps admins to save time. And just recently, I finished up the script that give me serveral days of huge brainwork, messy about 400 lines of codes. The script will automatically check the multiple NTP clients(can be the server also, that use the time from NTP. For this to work, you need to enable Remote Powershell on client computers. If remote powershell is not enabled on each of the servers to be checked, you can find my post here to enable it.

What this script will do:
This scipt will,

1) check the necessary ports (5985 or 5986), if winRM is enabled for Remote Powershell.
2) Check the current NTP values.in registry with the pre-defined values in script. If not matched, you can correct instantly.
3) Select the standard TimeZone of servers by the occurrence of mostly used values.

Thursday, May 5, 2016

Powershell: Check the Internet Accessibility for Multiple Computers

Sometimes, you need to make sure all your servers have internet access or not, especially after network change or for monthly auditing purpose. With powershell, you can achieve this by using .Net call to sockets. And, I found the script on this site to work as a baseline and use Mr. stevethethread's code to colorize the output.  You will need to save the list of servers in Server.txt in the same directory as script, and change the port number in the script as needed.

Note: Powershell Remoting must already be enabled on those servers and if not, I have written a blog post here on how to enable it.

Fig-1: Checking Internet Connection on multiple servers