An Accidental Truth- Invoke-VMScript

An Accidental Truth- Invoke-VMScript

The powercli command to my rescue!

I feel every command used in the computers has a story behind its usage. I was scratching my nerves deep to resolve a situation. This post is my honest attempt in documenting it.

Challenge

I love to use command line to complete mundane tasks however there are times doing things manually would solve the issue quickly than identifying to perform it the command line way. This challenge was a different one, it is a daunting task and required enormous preparation. Its time to wear my command line undies and save the world yet again!

My challenge was 300 windows 10 OS based systems which are hosted on VMware platform, and wanted to identify the free space in each of the disks present in those systems and generate a report including the name of the system and its hard disk volume details.

Journey

Firstly, All the nerds and the experienced wizards out there, I know its a cakewalk to solve the above challenge however the joy of experiencing the Eureka moment individually is a priceless moment. My moment had arrived too! For the readers who are unaware of what the author is referring, let me explain it to you, We manage many windows 10 OS based systems and as it involves 300 and more systems, logging into each system and obtaining the disk space details is a time-consuming exercise. A smart way to handle it is using command line. The unanswered questions are

  • How do we login to each system remotely ?
  • What tool should I be using to perform it ?
  • How do I gather the details in a report ?

Adventure

The age old wisdom passed on to me from the Wizards of programming is Always break the problem into small pieces, Attack them and Celebrate the joy of winning This is the first principle which I always deploy, launched my Powershell core from my laptop , ran as administrator and observed the blinking cursor for few minutes,

I first prepared a text file with the list of computer names. We now need to use this file and obtain each of these system hard disk details including free space and save it to a text file. The command Get-Content is my choice and it could be used as follows

Get-content "c:\data\diskextend.txt"

Now as we are using it in a program, we assign the value of the command to a variable so that we could use the variable to refer the list of computers defined in the file . The final command is

$vms=Get-content "c:\data\diskextend.txt"

Its time to address the second part, how do we go through each of the system collect the hard disk details and then display it, this is where the real fight was, I know foreach should be used to go through each of the systems however how to collect the hostname and the disk details, in search of a solution , I travelled to various sites online, spent hours in searching the right answer, even stayed away from the screen and bravely attempted meditation too.. Finally, ray of hope shined on the blinking screen, it was Invoke-VmScript command from powercli.

foreach ($vm in $vms) {Invoke-VMScript -VM $vm -ScriptType "powershell" -Scripttext "get-volume"}

The above command won't solve the challenge fully as it only obtains the details of the disk space and does not provide me the hostname of the system. How on earth we use Invoke-VMScript and perform multiple operations on each of these systems. It was a real tough fight, spent hours in finding the right way to solve my challenge. This is where endurance is required and never give up attitude

Finally, I was inspired and got a way around to resolve it. The command that worked for me is

PS C:\Windows\System32> $script =@'
>> $env:COMPUTERNAME
>> get-volume
>> '@

Using the variable concept we assigned the commands that we need to run on all these systems .

The desired output ran on the powershell console . It was a moment of joy, the celebration is almost there.

If you have come to this line, I first want to congratulate your patience for reading all the way here, the final piece of this puzzle was to send the output of the script to a text file than display on the screen for effective documentation. It was accomplished with the help of Out-File command.

I now used the variable concept ( reminded me about Christopher Nolan's Inception here ( dream within a dream)) and setup a variable to capture the result of the foreach command and the moment of celebration is here.

 $output = foreach ($vm in $vms) {Invoke-VMScript -VM $vm -ScriptType "powershell" -ScriptText  $script}  $output | Out-File C:\Reports\vdidisk071321.txt

Credits

My Eureka Moment had arrived. However , Its not just my victory, I was inspired ( is an understatement) by Luc , Jason and Adam for their wonderful articles related to this issue. My heartfelt thanks to them.

Final Thoughts

Invoke-VMScript is a wonderful command which utilizes the vmtools on the virtual machine and provides a gateway to communicate with the guest operating system. A wonderful command to use and lets have it in our arsenal.