I've been playing around some more with Nano server, but I didn't want to create yet another VM, so I decided to use one of my existing Nano servers. But it had an IP set already, and was set to an Internal switch (so no internet connectivity possible). So let's PowerShell-fix this!
First let's change the adapter of my VM called nano1:
There, that didn't hurt so much! Good, now let's change the settings inside the nano1 VM, to set the IP's back to DHCP. First, this very cool thing in Hyper-V called PowerShell Direct, allows me to enter the VM without needing any IP connectivity. All you need is the credentials of the VM, and to select the "-VMName" option with Enter-PSSession:
$cred = Get-Credential
Enter-PSSession -VMName nano1 -Credential $cred
This is what it looks like:
PS C:\> $cred = Get-Credential
Enter-PSSession -VMName nano1 -Credential $cred
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
[nano1]: PS C:\Users\administrator\Documents>
As Montgomery Burns says: "Excellent...": Did you notice the [nano1] in front of the last prompt? We're inside the VM! BTW, you don't need to add the credential, it will ask you for the credentials if you don't add them, but if you go in and out more than once, this is nicer.
Now that we're in a session, let's change the IP address and DNS back to DHCP enabled:
I've allways been a fan of VMware Workstation, but I've been spending a bit of time with Hyper-V on Windows 10 now too to see how the cookies are on the other side. One thing I thought I missed was creating what is called linked clones in VMware Workstation.
Linked clones allow a user to create multiple VM's that are referenced off of only one VM. The changes compared to the first VM are stored in a so called delta file under the covers. This way, you can build a large lab while needing a lot less space. You will however need access to the original VM files.
It turns out Hyper-V has the same functionality, but the steps to create a linked clone are a bit more hidden, and the operations are a bit more manual. The steps are as follows:
You need a template VM to reference your linked clones from. No different from VMware Workstation. The difference though, is that with VMware Workstation, you can keep working with this VM. With Hyper-V, this template is going to be left as an image on your harddrive, but you can't use it.
Sysprep the template VM. Theoretically you should do that in VMware Workstation too, but if you have some quick test you want to do and don't require domain functionality this could be omitted.
Turn off the VM, and mark the vhdx file of the VM as read-only. You can even remove that VM out of Hyper-V, so you don't accidentally turn it back on again
In Hyper-V, go to Action->New->Hard Disk. Select "Differencing Disk" as harddisk type, set the destination location and select the vhdx from the VM you created earlier.
Finally, create a new VM and select the newly created differenced disk instead of a new harddisk.
A lot of steps for what is relatively simple wizard in VMware Workstation. I think we can do better than that ;-) Powershell to the rescue! In the case below I've been lazy, and created a function that has paths and memory and such hardcoded inside, but the idea is what counts:
If you quickly want to create a bunch of VM's for your testlab, Powershell and Hyper-V are a good alternative to VMware workstation, and in fact, since VMware Workstation doesn't support Powershell, Hyper-V might even be better.. I know, I know. blasphemy.
I've been playing around with Hyper-V these past few weeks, and it seems to work rather well (VMware needs to start worrying).
One of the features I've been playing with is nested virtualization. Unlike VMware Workstation where you can select Hyper-V as one of the install options, in Hyper-V on Windows 10 and Windows 2016, you can turn it on on a per VM basis, with the following command:
Installing Windows 2016 Nano server isn't simply adding the DVD and clicking next-next-finish, but it needs some preparation. The base nano image wim file is only 168MB, but that has no functionality in itself. More needs to be added through features, drivers and packages.
From a management workstation, mount the Windows 2016 ISO (i.e. doubleclick it, take note of the driveletter). On the ISO file you will see a NanoServer directory:
That directory has the NanoServer.wim file, Packages folder and a NanoServerImageGenerator folder. That last folder contains a PowerShell Module, which you need to import in an Administrator PowerShell window (which in my case is on the E: drive):
Now you should have a bunch of extra commands available:
PS C:\Users\Akos> get-command -Module *nano* | select Name
The New-NanoServerImage cmdlet is the one you will need to create a fully functional image. The options are a slew of information. I've created a splatted variable set below, otherwise the line becomes far too long for the blog ;-)
This example creates a VM called NanoWeb01, with a password of "Pa$$w0rd!", and has IIS installed. The switch "-EnableRemoteManagementPort" does what it says. You can then connect to it via PSRemoting. The switch "-Storage" allows the Nano server to have filesharing available. If you don't need that, you can let it go, but for testing it's nice to have. This VM is ready for Hyper-V, and is about 560MB as it is configured like this.
There are several packages you can choose from, which can be found by the Get-NanoServerPackage cmdlet:
Next to this there are other switches too, of which a complete list and more info can be found on Microsoft's site. In all, Nano server takes a little work to get it running, but once it does, it starts in seconds.
Edit: You may have noticed the -Storage and -EnableRemoteManagementPort switches in my New-NanoServerImage command. I just saw in a different article, you can put them in the splatted options as well, by doing "Storage = $true" and the same for EnableRemoteManagementPort. You learn something new every day..
I've mentioned in my vCPU upgrade article that one of the requirements of me being allowed to shut down the VM was to be sure that there were no connections on the F5 loadbalancer for the webserver.
So I needed to script against the F5 loadbalancer. The first thing you need to do, is to get the F5 PowerShell Snapin from the F5 site. An account can be made for free, then you can login and download the plugin and follow the instructions to get it installed.
Once installed, you can add the snapin with:
Now you have commands to manage the F5. Great, now it's time to login. First you need to add some credentials to a variable, and I've mentioned this in a previous article too. After that, you can set up the connection to the loadbalancer:
Great, now you have the power to enable and disable nodes within the loadbalancer pool with actions like:
Disable-F5.LTMNodeAddress -Node "10.0.20.3"
Enable-F5.LTMNodeAddress -Node "10.0.20.3"
Now disabling and enabling is fine, but knowing if there's really no connections left is what you want to have. Luckily the Internet is a great place for info, and someone on Stackoverflow created the following excellent code, which I adapted a little to suit my needs. These two excellent functions do exactly what I want it to do, namely wait around for connections to drop (with a max time, don't want to spend waiting forever), and a supporting function to get the number of connections:
In my previous article, I created a Nano server image. This one is about adding the Nano server to a domain. Adding Nano servers to a domain isn't a pretty little single cmdlet like many others are, but consists of a list of things to do:
Configure DNS on the Nano server
Create what's called a blob file on a DC (or a machine that is joined to the domain, and a user that has rights to add a computer to the domain or as a Domain admin user)
Copy the file over to the Nano server
Use the blob file to add the server to the domain
I've found the following code on petri.com site, with an ingenious way to copy the blob file over. If you have the Storage package installed, you can of course just copy it using a share like \\nanosrv1\c$
#Set dns for Nano server
netsh interface ip set dnsservers name="Ethernet" static 10.0.0.4 primary
I am lazy. Well, not lazy, I just don't like working at ungodly hours. Here's the story: I got this request to upgrade 2 customer VM's from 3 vCPU's to 4 vcpu's. No biggy, I thought. But he wanted it done after 1 AM.. Wait, that's not during office hours!
OK, I could fix that with my trusty Powershell toolkit, and a scheduled task. But then I talked to the customer who went like: "Sure that's fine with me to script it, but make sure the sessions are drained from the webserver".. Hmm, F5 loadbalancers. I've not worked too much with those before, really. I get the general ideas within loadbalancing, but let's try and script against it.
So I had a number of things I wanted to happen:
Log in to the F5 loadbalancer, preferably not with a script filled with credentials hardcoded into it.
Wait an X amount of time for connections to drop from the specific pool
Shut down the machine, and wait for it to be properly turned off.
Set the number of CPU's and memory. This had an extra challenge, namely that the person who built the VM put in 1 vCPU with 3 cores, instead of 3 vCPU's.
Send an email with the results, so I can see what happened from my phone.
I'll spend a few posts on those different things.
Firstly the vCPU upgrade, which is the basis of this entire story:
Normally, you can turn off a VM, upgrade vCPU's, then start a VM again, through this simple set of commands:
However, since someone used 1 vCPU with multiple cores instead of multiple vCPU sockets, things work a little differently. If you would try this command, the VM would be shut down, but no upgrade would happen, and start back up with the same amount as before.
There I was, banging my head against the table because I thought I was perfectly able to select stuff from a CSV file, but I could not figure out why my selections weren't showing the stuff I wanted. It turns out my CSV file entries were full of trailing spaces!
Googling around, I saw this in a forumpost somewhere, but to prevent me from losing it, I've put the script below:
It's nice for on the spot use, but in a script that might not work. There are ways of doing that plaintext, but I saw a webpage that unfortunately I don't have the link for anymore, but if I find that reference I will add, but you can store those credentials securely too: