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

PowerCLI Saving VM Power State and Power On VMs
Fig-1: Demo of Saving VM Power State and Power On VMs

PowerCLI Powering on VM in action
Fig-2: Powering On VM in Action

You can download my script from the github.

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.

PowerCLI Get CDP Info
Fig-1: Demo of Getting the CDP Info

You can download my script from the github.

Powershell Debugging Made Easy: A good start tutorial


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. Generally, there are 2 methods for debugging. and 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, 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.
 

First Method

  1. You need to define the breakpoints with Write-Debug cmdlet for every line you want to inspect
  2. When you run the script, you need to use -Debug parameter
  3. For every breakpoints, you’ll be asked to:
  • Continue: to continue the script skipping that particular breakpoint
  • Suspend: stops at the breakpoint and inspect the runtime environment
  • Halt: Stop and exit from the script.

Second Method

  1. You need to pre-defined which variable or line number to setup breakpoint and powershell will stops at every these breakpoints, with the current line number & variable.
  2. Those breakpoints go away when you exit from the current powershell session, so it’s session specific. And you can’t save these breakpoints. You turn on breakpoints for every line you want by pressing F9 in powershell ISE.
  3. The command for example isSet-PSBreakpoint -Script myscript.ps1 –Variable x,y,result –Mode ReadWrite
** -Mode Parameter accepts 3 inputs: Read, Write and ReadWrite. Read is used when you want to stops the script when these variables are read. Write for when these variables are being written. And, ReadWrite for both. I explained the debugging steps in the figure for easy viewing. See the script at the end.
 
Powershell Debugging
Fig-1:Method 1
 
Powershell Debugging
Fig-2: Method 2

After you’ve checked the variables, the error is you’ve defined the variables $first and $second as string types. So, just remove it.

Powershell: Automatically Check and Correct multiple NTP Clients

Here is the script that will help system admins to automatically check the windows NTP settings on multiple computers through registry. 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.
4) If the appropriate time zone not found, your machine time zone will be used as standard time zone. And compare each server with the standard timezone. If not matched, you can prompted to correct.
5) It select the standard time by the occurrence of current time values on each server.(compare up to minutes’ detail)
6) If the appropriate current time is not found, your machine time will be used as standard time. And compare each server with the standard time. If not matched, you are prompted to correct.
7) It will detect the stopped time service and prompted you to start the service.

Glad if you find it useful, Cheers !

Change Windows NTP with Powershell
Fig-1: The demo Run

You can download my script from github.

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. Continue reading “Powershell: Check the Internet Accessibility for Multiple Computers”