Tag Archives: MimbolovePowershell Web Access

Remotely Powering On and Off My Entire Home Lab

Written by Tao Yang

RemoteMy home lab consists of 3 PCs running Hyper-V and a HP Proliant Microserver N54L running SCVMM. I have previously blogged the lab setup in a 2-part blog posts (Part 1, Part 2). These 2 blog articles was written back in October 2012, although there are few changes in the current setup (new hardware, etc), but the overall setup is pretty much the same.

All 4 machines in my lab have been constantly running 24×7, except when we go on holidays or there’s a power outage (which doesn’t happen very often). This is largely because I just can’t be bothered to spend time start and shutdown all the physicals and virtuals every time I use the lab, not to mention I often access my lab when I’m in the office via RDP using my Surface Pro 2 with an external monitor. Because all of the computers are PC grade hardware, there are no out-of-band management cards (i.e. iLo, DRAC, etc.) on these boxes, I had no way to remotely start them when I was in the office.

In order to reduce the “carbon footprint”, and more importantly, my electricity bill, I have been wanting to automate the the start and shutdown process of the entire lab for a while. Last weekend, I finally got around to it, and accomplished it by using only Wake On LAN (WOL) and PowerShell (with PowerShell Web Access, WinRM and CredSSP).

Because one of the PCs in my lab is my main desktop (running Windows 8.1 with Hyper-V role enabled), this PC is always running. my solution is to use this desktop (called “Study”) to interact with other physical computers in the lab. I’ll now go through the steps I took to archive this goal:

1. confirm / configure Wake-On-LAN on all physical computers in the lab.

I installed a freeware called AquilaWOL on my “Study” PC, made sure I can WOL all other computers.

image

During my testing, the HP Microserver and one of the Hyper-V box (HyperV01, the one with the Intel motherboard) had no problem at all. However, the other HyperV box HyperV02, would not WOL. after some research, it seemed like a known issue with the motherboard that only be able to WOL when the computer is at sleep, not when it’s powered off. Luckily other than the on-board Marvell NIC, I also have a dual port Intel GB NIC and a single port Intel desktop GB NIC on this computer. the dual port NIC also wouldn’t work. but the desktop NIC worked 🙂

2. Installed a Windows Server 2012 R2 virtual machine on my “Study” PC.

I named this VM “JUMP01” because I intend to use this as a jump box. I connected this VM to the virtual switch  which is on the same subnet as all physical computers – so I don’t rely on switch/routers to relay WOL packets. I also need my AD environment to be available when running the script, but because I have a domain controller already running as a VM on my Study PC, no additional VM’s are required.

3. Installed and configured PowerShell Web Access on the Jump server.

So I don’t have to RDP to the jump server to run the scripts. This also enables me to power on / off the lab from any mobile devices with a browser. I have followed my previous blog post to install and configure PSWA. I also wrote a PowerShell module to resize PowerShell console size to make PSWA more user-friendly for mobile devices.

4. Developed PowerShell scripts to power on and power off the physical and virtuals.

I wrote 2 scripts: start-lab.ps1 and stop-lab.ps1. both scripts read required information from a XML file (labconfig.xml). this XML file contains all required information for my lab environment.

SNAGHTML6bd3b2

The PowerShell scripts utilise WinRM and CredSSP to interact with each physical computers (using PSSessions and Invoke-Command).

Below is a list of steps each script performs:

Start-Lab.ps1:

  1. Read XML, get information for all computers that are a member of the lab
  2. ping each lab member, send WOL magic packet if ping failed
  3. wait for 90 seconds (configurable via XML)
  4. Check OS readiness (configurable via XML)
    • Minimum up time
    • all required services are running
  5. Once the OS is ready on the Hyper-V hosts, start VMs in groups hardcoded in the script (based on my naming standard in the lab).
    • firstly start the CentOS VM configured as routers (they are configured to auto start with the host, but just in case they did not start).
    • Then start the domain controller
    • Then start all VMs hosting SQL databases (OpsMgr DB, ConfigMgr site servers, Service Manager DB, etc).
    • Then start all other VMs except OpsMgr management servers
    • Lastly, start all OpsMgr management servers (they must be started at the end so I don’t get any alerts).

Stop-Lab.ps1:

  1. Read XML, get information for all computers that are a member of the lab
  2. ping each lab member, ignore the lab member if it does not respond to ping
  3. shutdown VM’s in order (which is the reverse order as the start-lab.ps1)
  4. double check if all VM’s are completely shutdown, if not, forcibly turn them off
  5. shutdown Hyper-V servers (and non-hyper-v physicals).

Note: the WOL function in my script is taken from here: http://gallery.technet.microsoft.com/scriptcenter/Send-WOL-packet-using-0638be7b

5. Created a simple PowerShell module to execute the 2 steps I wrote.

On the jump server, I created a powershell module called “LabAdmin”, which contains 2 functions that simply execute the powershell script:

image

6. Configured port forwarding on my ADSL router to allow me to access the PSWA site from the Internet

This allows me to manage my lab even when I’m not home.

i.e. starting the lab via my mobile phone (over 4G):

image

Here are the live demos for both scripts:

Start-Lab:

Stop-Lab:

For your reference, the scripts can be downloaded HERE.

Conclusion

I live in Australia, one of the countries with the highest electricity prices. It is time for me to do something to cut down the running cost of my home lab – especially when my colleagues told me their average electricity bills are only half of mine.

Now, I can remotely start my entire lab anywhere via my mobile phone, and it only takes me a single command to shut down the lab, I won’t need to have them running 24×7. So I’m hoping my implemented this new feature in my lab, I should be able to see some noticeable reductions in my next power bill. 🙂

PowerShell Module to resize console – Updated

Written by Tao Yang

PSWAIcon Few days ago I wrote a PowerShell module that contains 1 cmdlet / function to resize the PowerShell console windows.

It was a quick job that I did less than half an hour. I wrote it primarily to make PowerShell Web Access (PSWA) more user friendly.

Over the last couple of days, I spent a bit more time on this module, and add a lot more functionality to it. The original module had 107 lines of code, and the updated one has 591 lines.

Here’s a list of new features:

Additional cmdlets

This module now contains the following cmdlets:

  • Resize-Console
  • Get-CurrentConsoleSize
  • Save-ConsoleProfile
  • Remove-ConsoleProfile
  • Get-ConsoleProfile
  • Update-ConsoleProfile

I’ll go through each cmdlets later

Aliases for all cmdlets and parameters

Since I wrote this module primarily for PSWA, and I intend to access PSWA primarily from mobile devices such as phones and tablets, I need to make the module easier to use. I am sure I’m not the only person who’s suffering from fat finger syndrome. I HATE typing on tablets and phones. Even with the Type Cover 2 for my Surface Pro 2, I found the keys are too small. So less is more, aliases really help when I use mobile devices because I don’t have to type as much.

Buffer width is always same as window width when re-sizing

When working on any kind of command prompts (cmd or PowerShell), I really don’t like the horizontal scroll bar. by making the buffer width always the same as window width, I don’t have to see the horizontal bars anymore.

Resize-Console –max switch

Or “rsc –m” if use aliases. The –max switch will move the window to the top left corner of the PRIMARY monitor and maximize the window size. It also set the buffer height to the maximum value of 9999. – This is equivalent to maximizing a normal window.

Every time I get on to a box (most likely via RDP) for the first time, I always had to manually set the PowerShell console size to suit my needs. then next time I RDP in from another computer with different display resolutions, I often had to set it again.

With resize-console –max, it will always set the console to the maximum size and occupies the entire screen. It will make my life so much easier. Not that I have OCD(Obsessive Compulsive Disorder), but for those ones who do, this function would make you much happier I suppose :). I’ll demonstrate this in the Youtube video at the end of this article.

Note: for this functionality, I used some of the code from Richard Siddaways’s blog. So thanks to Richard.

Resize-Console –Profile

I have included a XML file (profiles.xml) in this module. We can save pre-defined console dimension (Window width and height) to this XML so we can use them later.

i.e.:

SNAGHTML1aca2b96

The screen size and resolution are different among different computers and mobile devices. I have created different profiles for each type of devices that I own, so when I use a particular device to access PSWA, I can simply apply the appropriate screen size to suit that device.

i.e. the screenshot below is taken from my mobile phone (Samsung Note3):

image

when I applied the profile “Note3”, the PSWA console fits perfectly on the screen.

Or, on my 10.1 inch tablet Samsung Galaxy Tab2:

image

I used aliases, applied the “tab2” profile to fit the screen.

With the introduction to the –profile functions, this module now includes these functions for CRUD operations:

  • Save-ConsoleProfile
  • Remove-ConsoleProfile
  • Get-ConsoleProfile
  • Update-ConsoleProfile

For details of each functions, You can refer to the help information (get-help)

Note: only administrators will be able to modify the profiles.xml because it’s located in a system folder. so if UAC is enabled, admins will need launch powershell console as Administrator in order to use the Remove-ConsoleProfile and Update-ConsoleProfile cmdlet.

Get-CurrentConsoleSize

This one simply display the current window size and buffer size on screen. it’s reading the properties of “$host.ui.rawui”

SNAGHTML1ad77daf[4]

For your reference, here’s a recorded demo that I have updated to Youtube:

Please watch this video in full screen and 720P/1080P or you may not be able to see what’s happening on the powershell console.

You can download the module HERE. simply unzip and copy the whole folder to C:\Windows\System32\WindowsPowerShell\v1.0\Modules

So why am I spending time on this PowerShell project rather than System Center, which is my bread and butter? That would be the topic for my next blog article. 🙂

Please feel free to contact me for any issues or suggestions.

Until next time, happy PowerShelling 🙂

PowerShell Module: Resize-Console

Written by Tao Yang

I’m currently working on a solution that relies on PowerShell Web Access (Hopefully I can finish tonight and blog it in next couple of days).

I have been a bit hesitate to use use PWSA every since I firstly tried it out briefly back in 2012 (and blogged my experience here).

Why am I hesitated? this is why:

image

The interface is just not that user-friendly with such a small window and that much useless space. There is no way to easily resize the window.

In my original blog post, I posted a simple script to increase the size. Today, I spent a little bit more time, wrote a module based on the original code, and of course made it more flexible.

This module has only one function called Resize-Console:


Function Resize-Console
{
<#  .Synopsis Resize PowerShell console window .Description Resize PowerShell console window. Make it bigger, smaller or increase / reduce the width and height by a specified number .Parameter -Bigger Increase the window's both width and height by 10. .Parameter -Smaller Reduce the window's both width and height by 10. .Parameter Width Resize the window's width by passing in an integer. .Parameter Height Resize the window's height by passing in an integer. .Example # Make the window bigger. Resize-Console -bigger  .Example # Make the window smaller. Resize-Console -smaller  .Example # Increase the width by 15. Resize-Console -Width 15  .Example # Reduce the Height by 10. Resize-Console -Height -10  .Example # Reduce the Width by 5 and Increase Height by 10. Resize-Console -Width -5 -Height 10 #>

[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$false,HelpMessage="Increase Width and Height by 10")][Switch] $Bigger,
[Parameter(Mandatory=$false,HelpMessage="Reduce Width and Height by 10")][Switch] $Smaller,
[Parameter(Mandatory=$false,HelpMessage="Increase / Reduce Width" )][Int32] $Width,
[Parameter(Mandatory=$false,HelpMessage="Increase / Reduce Height" )][Int32] $Height
)

#Get Current Buffer Size and Window Size
$bufferSize = $Host.UI.RawUI.BufferSize
$WindowSize = $host.UI.RawUI.WindowSize
If ($Bigger -and $Smaller)
{
Write-Error "Please make up your mind, you can't go bigger and smaller at the same time!"
} else {
if ($Bigger)
{
$NewWindowWidth = $WindowSize.Width + 10
$NewWindowHeight = $WindowSize.Height + 10

#Buffer size cannot be smaller than Window size
If ($bufferSize.Width -lt $NewWindowWidth)
{
$bufferSize.Width = $NewWindowWidth
}
if ($bufferSize.Height -lt $NewWindowHeight)
{
$bufferSize.Height = $NewWindowHeight
}
$WindowSize.Width = $NewWindowWidth
$WindowSize.Height = $NewWindowHeight

} elseif ($Smaller)
{
$NewWindowWidth = $WindowSize.Width - 10
$NewWindowHeight = $WindowSize.Height - 10
$WindowSize.Width = $NewWindowWidth
$WindowSize.Height = $NewWindowHeight
}

if ($Width)
{
#Resize Width
$NewWindowWidth = $WindowSize.Width + $Width
If ($bufferSize.Width -lt $NewWindowWidth)
{
$bufferSize.Width = $NewWindowWidth
}
$WindowSize.Width = $NewWindowWidth
}
if ($Height)
{
#Resize Height
$NewWindowHeight = $WindowSize.Height + $Height
If ($bufferSize.Height -lt $NewWindowHeight)
{
$bufferSize.Height = $NewWindowHeight
}
$WindowSize.Height = $NewWindowHeight

}
#commit resize
$host.UI.RawUI.BufferSize = $buffersize
$host.UI.RawUI.WindowSize = $WindowSize
}

}

image

I have copied the folder containing this module to C:\Windows\System32\PowerShell\v1.0\Modules folder on the server hosting PSWA site so it is available for everyone.

Here’s a screen video capture if you want to see it in action:

Note: Please watch this video in full screen (by double-clicking the video) and choose 720P if you can. Otherwise you might not see much in such a small window.

This module also works on normal PowerShell prompt windows. You can download this PSConsole module HERE. to set it up, simply copy to the folder I mentioned above.

My First Impression on PowerShell Web Access

Written by Tao Yang

I ran up an instance of Windows Server 2012 in my test lab last night so I can play with various new features such as IPAM and PowerShell Web Access, etc.

Today I configured this box as the PowerShell Web Access (PSWA) gateway. I have to say, I am very very impressed! The implementation is easy, took me less than an hour (including time spent reading TechNet articles) and having ability to access PowerShell console on virtually any web browser for all Windows machines in my lab is just fantastic!

Now I can probably get away from using RDP most of the times since I’m pretty comfortable with PowerShell Smile

So, here are the steps I took to setup PSWA:

1. Add the PSWA feature in Server Manager

2. Install PSWA web application using PowerShell:

Install-PswaWebApplication

3. Requested and installed a SSL certificate for the PSWA gateway machine from my Enterprise CA

4. In IIS, configured HTTPS for the default web site and used the SSL certificate I just installed from previous step.

5. Created an AD group called PSWA_Users and added few user IDs into this group.

6. Create PSWA Authorization Rule:

Add-PSWAAuthorizationRule -UserGroupName Corp\PSWA_Users -Computername * -ConfigurationName *

image

7. Since I can’t guarantee that WinRM has been enabled and configured on every machine, I’ve created a GPO to enable WinRM and linked it to the domain root.

Now, PSWA is pretty much ready to go. I launch the web access console on Google Chrome and entered my credential and the computer that I wish to connect to:

image

And I’m in!

image

It’s great to see that Microsoft releases a web-based product that runs on browsers other than IE. I don’t think I’ve seen anything like this before!

Additional Configurations:

I started testing by connecting to a SCOM management server and tried to retrieve all SCOM agents in my management group (Only 11 in total so I’d assume not huge amount of data is returned). I used:

Import-Module OperationsManager

$a = Get-SCOMAgent

Interestingly, it failed and the connection to the management server was closed:

image

Error:

Processing data for a remote command failed with the following error message: The WSMan provider host process did not return a proper response. A provider in the host process may have behaved improperly.

This reminded me the default setting for “Maximum amount of memory in MB per Shell” for WinRM, which I blogged previously in this post. The default setting on Windows Server 2008 R2 and Windows 7 is 150MB. This default setting has increased to 1024MB on Windows Server 2012 and Windows 8.

So to test, since I have 3 management servers in the OM12 management group, I’ve increased this setting to 1024 on another management server. It fixed the error:

image

To further prove this error is actually caused by not having enough memory for the remote shell, I’ve connected PSWA to a Windows 8 machine, which has OM12 console and command shell installed. I used the following commands to connect to the OM12 management group:

Import-Module OperationsManager

New-SCManagementGroupConnection OpsMgrMS03

It prompted an error saying I don’t have sufficient permission:

image

This is by design, when using second hop in CredSSP, the credential has to be explicitly specified. so I changed the command to:

New-SCManagementGroupConnection OpsMgrMS03 –Credential (Get-Credential domain\MyID)

after entering the password, I was successfully connected and I managed to retrieve all SCOM agents by using Get-SCOMAgent Cmdlet without issues.

image

So to fix this issue once for all, I’ve modified the GPO I’ve just created and changed the “Maximum amount of memory in MB per Shell” setting to 1024.

Click here to see settings defined in my WinRM GPO.

I also configured another port forwarding rule on my ADSL router to forward port 443 to the PSWA gateway computer so I can connect when I’m not home.

PSWA on Mobile Devices:

I am able to launch and use PSWA on both my Android tablet (Samsung Galaxy Tab 10.1v running ICS) and my wife’s iPad 3 (running iOS 6) using both built-in browsers and Google Chrome on both devices.

Below are few screenshots from my Galaxy Tab:

image

image

Maybe it’s just me being an Apple noob, when I’m on the iPad, I could not find the Tab key on the keyboard, so I couldn’t use the PowerShell auto completion feature. – One more reason that I’m staying away from that product!

Console Size:

by default, the console size is 120×35, which seems like a waste of space when I’m on a big screen.

So I wrote a simple PowerShell script called Resize-Console.ps1 to resize the window:

$bufferSize = $Host.UI.RawUI.BufferSize
$buffersize.Width = 180
$host.UI.RawUI.BufferSize = $buffersize

$WindowSize = $host.UI.RawUI.WindowSize
$WindowSize.Width = 180
$WindowSize.Height = 40
$host.UI.RawUI.WindowSize = $WindowSize

After I ran this script, the console fits perfectly on my Galaxy tab (resolution 1280×800):

image

This console size also works great on my laptop, which has the resolution of 1366×768. For different resolutions, the width and height need to be adjusted in the script. the only catch is the buffersize cannot be less than the window size (I set the width for both sizes to be the same).

I haven’t managed to work out a automated way to resize the console as when in a PS remote session, there is no $profile so I can’t add scripts into $profile like we normally do on a local console. If I find a way in the future, I’ll post it here.

This is what I found so far. I’ll continue to blog on this topic if I find any other interesting stuff!

By the way, I followed this TechNet article to configure the PSWA: Deploy Windows PowerShell Web Access