StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

PowerShell ROBO

  • January 17, 2017
  • 17 min read
Toronto VMUG Co-Leader, VCP4/5, VCAP5-DCA, vExpert 2011-2014, Top 50 Viirtualization Blogger
Toronto VMUG Co-Leader, VCP4/5, VCAP5-DCA, vExpert 2011-2014, Top 50 Viirtualization Blogger

Picture this – you are a systems administrator working at a major banking institution. The security team walks into your office and lets you know that a major update needs to be applied to all of the servers within the institution – not a problem for most organizations, but in the case of a bank, you could have hundreds if not thousands of remote and branch offices.  Sure, we can write scripts to copy out the update files and even execute them remotely from our head office – but the problem most ROBO scenarios are ever-changing – with new offices being created and others closing down all the time.  Keeping track of server names, IP schemes, etc. can be quite a time-consuming process.  Naturally, we want the same updates, patches, and fixes to be deployed everywhere, in the same manner, in order to provide consistency – so having this up to date list available when we need it is key to driving success within our environment.

So what does this have to do with PowerShell?

Since PowerShell’s first birth into the enterprise back in 2006, we have seen many third party and windows applications adopt the simple scripting language.  With such a wide range of support it may make sense for you to keep this “location” listing stored within a PowerShell module – allowing you to access the information you need about locations and simply “pipe” other commands to its metadata – allowing us to essentially convert our branch locations into PowerShell objects that we can retrieve from a PowerShell module.  Sometimes it’s easier to simply see how this works before it makes sense, so let’s take a look at a simple example…

If we think back to our banking example we can imagine that we may have a lot of branch offices – and offices of different types as well; think of a fully-fledged bank in a city, small bank kiosks in grocery stores, etc.  Our first goal will be to take all of the information about our banking locations, their location codes, server names, IP schemes, bank types, etc. and build a cmdlet which will allow us to quickly retrieve this information from a PowerShell Module.  To do so we will first need some sort of database or file containing this information.  Today I’ll simply use a CSV file, however, you could use any type of data store you like (MSSQL, Txt, etc.) so long as you change the code below in order to get the data out of the file.  Below you can see my CSV file and how I’ve structured this data – try to envision your ROBO environment within this structure…

Location ID Name Type City Province Country Domain Controller Name Domain Controller IP Transaction Server Name Transaction Server IP ESX Host Name ESX Host IP
100 Belleville-Main1 Full Belleville Ontario Canada 100-DC 100-TS 100-ESXi01
101 Belleville-Kiosk1 Kiosk Belleville Ontario Canada 101-DC 101-TS 101-ESXi01
110 Toronto-Main1 Full Toronto Ontario Canada 110-DC 110-TS 110-ESXi01
111 Toronto-Main2 Full Toronto Ontario Canada 111-DC 111-TS 111-ESXi01
112 Toronto-Main3 Full Toronto Ontario Canada 112-DC 112-TS 112-ESXi01
113 Toronto-Kiosk1 Kiosk Toronto Ontario Canada 113-DC 113-TS 113-ESXi01
114 Toronto-Kiosk2 Kiosk Toronto Ontario Canada 114-DC 114-TS 114-ESXi01
115 Toronto-Kiosk3 Kiosk Toronto Ontario Canada 115-DC 115-TS 115-ESXi01
120 Ottawa-Main1 Full Ottawa Ontario Canada 120-DC 120-TS 120-ESXi01
121 Ottawa-Kiosk1 Kiosk Ottawa Ontario Canada 121-DC 121-TS 121-ESXi01
122 Ottawa-Kiosk2 Kiosk Ottawa Ontario Canada 122-DC 122-TS 122-ESXi01
130 Montreal-Main1 Full Montreal Quebec Canada 130-DC 130-TS 130-ESXi01
131 Montreal-Main2 Full Montreal Quebec Canada 131-DC 131-TS 131-ESXi01
132 Montreal-Kiosk1 Kiosk Montreal Quebec Canada 132-DC 132-TS 132-ESXi01
133 Montreal-Kiosk2 Kiosk Montreal Quebec Canada 133-DC 133-TS 133-ESXi01
140 Vancouver-Main1 Full Vancouver Britiish Columbia Canada 140-DC 140-TS 140-ESXi01
141 Vancouver-Kiosk1 Kiosk Vancouver Britiish Columbia Canada 141-DC 141-TS 141-ESXi01
142 Vancouver-Kiosk2 Kiosk Vancouver Britiish Columbia Canada 142-DC 142-TS 142-ESXi01
150 NewYork-Main1 Full New York New York United States 150-DC 150-TS 150-ESXi01
151 NewYork-Kiosk1 Kiosk New York New York United States 151-DC 151-TS 151-ESXi01
160 LosAngeles-Main1 Full Los Angeles California United States 160-DC 160-TS 160-ESXi01
170 Dallas-Main1 Full Dallas Texas United States 170-DC 170-TS 170-ESXi01
171 Dallas-Kiosk1 Kiosk Dallas Texas United States 171-DC 171-TS 171-ESXi01

As you can see above I have a lot of metadata built around all of the different bank locations, however this data as it is isn’t that useful to me when trying to run other PowerShell cmdlets against it– so let’s take this data and create PowerShell module around it, with a Get-BranchLocation cmdlet to return the data to us…



Today we will just be using a simple script module, however, you could use any PowerShell module type you wish – but to get up and running quickly simply create the folder structure shown inside of your C:\Windows\System32\WindowsPowerShell\v1.0\Modules directory, complete with our PSM1 file (the module file) and our locations.csv location listing.

Once the structure is ready it’s time to get started on the code.  Our example for today only contains one function (Get-Bank Line 2 through 32) but will give us the foundation for building other types of cmdlets within our module.  For simplicity sake, we can see all of the code below – with the line by line explanations below it…

Line 4 through 9 – These are our function/cmdlet parameters – meaning we define these in order to allow our users to pass information into our cmdlet.  Here we have 5; LocationID, Type, City, Province, and Country – all matching with column headers in our CSV file.  It’s the values of these parameters that can be passed in order to filter our result set.

Line 12 – This simply imports our CSV file of our branch locations into the banks variable.

Line 14 – Here we establish the beginning of our filtering sequence.  Since the parameters of this function are not required, meaning we can pass a city value, a province value, a city and province value, or in fact no parameters at all, we have to dynamically build our filter.  By beginning our filter with an always true “1 –eq 1” sequence we can make the remaining code much easier to write.

Line 16 – This line is used to gather all of the parameters which were passed to the cmdlet.

Line 18 through 31 – This loops through the passed parameters and if it finds a parameter with a value it is appended to our filter string on Line 26.

Line 32 – This takes the command which we would like to run and appends the filter string to it.

Line 33 – Here we complete our function and return the evaluated expression of our command string.

Line 36 – In order to use our Get-Bank cmdlet outside of this module, we need to ensure we export it as a module member.

Now if we head into our PowerShell console and run our new cmdlet, you should see some similar output as that below…

cmdlet run


So by now, you might be thinking “How can such a small little function help me with managing my branch locations?”  Well, take for instance our example of copying out a patch to all of the domain controllers –  In order to do this utilizing our new cmdlet we could do the following

Get-Bank | foreach-object { Copy-Item c:\super-patch.exe \\$($_.DomainControllerName\share$\ }

Need to enable ssh on your ESXi hosts which are located only in kiosk branches – no problem

Get-Bank –Type Kiosk | foreach-object { Get-VMHost –Name $($_.ESXHostName) –User username –Password password | Start-VMHostService –HostService ($_ | Get-VMHostService | Where {$_.Key –eq “TSM-SSH”} }

How about restarting all of the Transaction Services located in Quebec?

Get-Bank –Province Quebec | foreach-object { Restart-Computer –ComputerName $($_.TransactionServerName) }

By now we can really see the efficiencies and consistencies that bundling up our remote/branch offices into a cmdlet with a PowerShell module can bring – and this is just with one simple cmdlet.  Certainly, this opens the doors for much more.  If we take more of the mundane, repetitive tasks we perform at each location, script them into PowerShell, and simply add them into a function/cmdlet into our PowerShell module we not only make our lives easier by automating our work but down the road when things go wrong, we should be left with a more consistent environment – which we all know makes troubleshooting easier!  For now, give PowerShell a shot at managing those remote locations for you, stop wasting time looking up hostnames and IPs, and just Get-Scripting!

Related materials:

PowerShell Modules – Why bother?

5 tips to help you explore the world of PowerShell scripting


Found Mike’s article helpful? Looking for a reliable, high-performance, and cost-effective shared storage solution for your production cluster?
Dmytro Malynka
Dmytro Malynka StarWind Virtual SAN Product Manager
We’ve got you covered! StarWind Virtual SAN (VSAN) is specifically designed to provide highly-available shared storage for Hyper-V, vSphere, and KVM clusters. With StarWind VSAN, simplicity is key: utilize the local disks of your hypervisor hosts and create shared HA storage for your VMs. Interested in learning more? Book a short StarWind VSAN demo now and see it in action!