Extending PowerShell with Custom Property Sets

If you’ve been following along on the blog recently you’ve read about my use of PowerShell type extensions. This is a way of adding new properties to things I use all the time. The goal is to save typing and get what I need with minimal effort. You can also take this a step further by creating your own property sets.

Continue reading

Extending Hyper-V with PowerShell

Lately I’ve been writing about my use of PowerShell type extensions as a way to get more done quickly. Or at least give me the information I want with minimal effort. I use Hyper-V a great deal and the Hyper-V cmdlets are invaluable. And while a command like Get-VM provides a lot of information, I always seem to want more so I thought I’d share with you my Hyper-V related type extensions. Even if you don’t need or use Hyper-V, you might find my techniques useful.

Continue reading

Adding Some Power to Hyper-V VM Notes

Since I work at home, I rely a great deal on my Hyper-V environment. I’m assuming if you are using Hyper-V at work the same is true for you.  Because I do a lot of testing, it is difficult sometimes to remember what is running on a given VM. Did I update that box to PowerShell v5? Is that VM running Windows Server 2016 TP 3 or TP4?

Hyper-V virtual machines have a setting where you can keep notes which seems like the ideal place to store system information.  Since most of my Windows machines are on my public network and the virtual machine name is the same as the computer name, I can easily use PowerShell remoting to connect to each virtual machine, get some system information, and update the corresponding note.

I can run a command like this to get the system information I need.

I get back a result like this:

System Information

system information

In my code, I want to include the computer name just in case it is different. If the server has the Resolve-DNSName cmdlet, I invoke it otherwise I use the .NET Framework to resolve the name.

To set the the Notes property , I can use Set-VM.

Be aware that this behavior will replace any existing notes. In my final code, I take that into account and by default I append the system information. But there is a parameter to replace the note contents if you wish.

My final code also includes a parameter to use the VM’s IP address instead of it’s name. I have a few VMs that are not part of my test domain, but I register their names in my DNS.

Here’s the complete script.

Note that this is a script and not a function.  I can now easily update my virtual machines.

setting VM note

getting system information for the VM note

And here’s the result:

viewing the note

The new note

Of course you can modify the script to include any information you want in the note.

If you find this useful I hope you’ll let me know.

Enjoy!

UPDATE JANUARY 11, 2016

I have updated the script and turned it into a function. You can now pipe virtual machines into the function. I also included a Passthru parameter so you can see the Note information. The function is hosted on GitHub at https://gist.github.com/jdhitsolutions/6f17c1d901ff870ff7a3.

What Are You?

Here’s a quick way to tell whether a given machine is real or not: check the Win32_Baseboard class. You can use either Get-WmiObject or Get-CimInstance. Notice the results from a few physical machines.

Now see the result when querying a Hyper-V virtual machine:

I don’t have any VMware available so I don’t know what kind of result that would show. I also haven’t done extensive testing with items like a Microsoft Surface. I threw together this simple function you could use.

Have fun.

Hyper-V Memory Utilization with PowerShell

I really push the limits of my Hyper-V setup. I know I am constrained by memory and am hoping to expand my network before the end of the year. But in the meantime I have to keep close tabs on memory. I thought I’d share a few commands with you. I am assuming you have the Hyper-V module installed locally. You don’t have to be running a hypervisor in order to use the PowerShell commands to manage a remote server. Or you can take my commands and run them remotely via a PSSession or Invoke-Command.

First off, I only need to get virtual machines that are currently running.

This command is using the newer Where-Object syntax. Here’s a sample result.


Memory usage for running VMs

Or I can use the newer Where() method in v4 which performs better. I’ll get the same result.

The memory values are in bytes which I’m never good at reformatting in my head, so I’ll PowerShell do the work.


Formatted values

You know what? I want to take this a step further, which is usually my inclination. I think it would be useful to also see what percentage of assigned memory is being demanded. I can calculate this percentage and round to 2 decimal places.


Memory utilization with percentage

That should give me all of the data I need. The last step is to format the results into an easy to read report.


Formatted VM memory report

I can take this code and turn it into a script or function to save some typing. Perhaps even parameterize for the computername. I have some other thoughts as well which I hope I can get to at some point. But for now clearly I have some issues on my Hyper-V server, CHI-HVR2, to attend to.

Enjoy!