So now i have started digging into the powershell cmdlets that are available for the Hyper-V in Windows 8 Server beta. As you can see on my screendump and also on this link there are some, i would love to see some extensions when the product becomes RTM, I am spoiled with the functionality and extensibility of the PowerCLI 😛
One thing that i like is that with powershell it is easy to actually find out what NIC you want to use in your virtual switches, why Microsoft insists of showing the description instead of name in the Hyper-V virtual switch admin gui? i have no idea which nic is which,
I do have a good name for them in the Network Connections
in the wonderful world of powershell it is now easy to create a virtual switch and actually get the right nic, also with the parameter -AllowManagementOS i make sure that my Host will not share this nic.
Then i can deploy my VMs, I am woundering why the virtualization team is using bytes in their parameters ? -MemoryStartupBytes -NewVHDSizeBytes, fortunately you can use -MemoryStartupBytes 512MB or 1GB and powershell calculates it to bytes. Another thing that I am wondering about is that when i create a VM i cannot set number of vCPU, i have to change that after the VM has been created?!
How do you enable Hyper-V with powershell in the new win 8 Server beta, as you can see when running the Get-WindowsFeature Hyper-V i only see the Hyper-V role, but i also want the administration tools, best practice says that you should run the Hyper-V server as clean as possible and then you might want to enable the management tools on the administration server, but now i only have one server and want to be able to create VMs and configure etc.. In the screenshot i had already enabled the Hyper-V role.
To enable all Hyper-V Role/features i just type like this,
Get-WindowsFeature Hyper-V* | Add-WindowsFeature
I will test some Hyper-V powershell cmdlets after this and that might result in another post 😛
After this week when i have been co-teaching a VMware vSphere ICM course i was going to show the students FT and I was seeking in the powerCLI reference for some cmdlets but could not find any.
I created my own function for enabling this, i got inspiration from Cody Bunch post but extended it with a function and also with my function you can disable/enable/remove and test restart/failover the secondary vm. I also check if the FT is enabled or not before making any changes.
Here is the PowerCLI function, i am using some Get-View to be able to use the FT functions.
Updated: Today (Monday 27/2) i added functionality with a possibility to also send VMHost as argument, but then i realized some errors occuring, my assumtions that the primary VM always was the first object when doing a Get-VM was false, i have corrected the function that it actually checks for the correct VM, Primary or Secondary. If you do not send VMHost parameter the FT process will choose a Host for you, this cannot be the same as the Primary VM resides on.
function Set-VMFaultTolerance{
<#
.SYNOPSIS
Enable FT for the VM sent as parameter
.DESCRIPTION
Use this function to enable or disable Fault Tolerance
.PARAMETER xyz
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-02-27
#>
param (
[Parameter(Position=0,Mandatory=$true,HelpMessage="A virtual machine",
ValueFromPipeline=$True)]
$VM,
$VMHost = $null,
[switch]$enableFT,
[switch]$disableSecondaryFT,
[switch]$removeFT,
[switch]$failOverFT,
[switch]$restartSecondaryFT
)
if ($VM.ExtensionData.Config.FtInfo -ne $null){
$VM = Get-VM $VM | where {$_.Extensiondata.Config.FtInfo.Role -eq 1}
} else {
$VM = Get-VM $VM
}
if ($VMHost -ne $null){
$VMHost = Get-VMHost $VMHost | Get-View
if ($VMHost -ne $null) {
$VMHostobj = New-Object VMware.Vim.ManagedObjectReference
$VMHostobj.type = "HostSystem"
$VMHostobj.value = $VMHost.MoRef.Value
$VMHost = $VMHostobj
}
}
if ($enableFT) {
if ($VM.ExtensionData.Config.FtInfo -eq $null){
$VMview = $VM | Get-View
$VMview.CreateSecondaryVM($VMHost)
} else{
$VMsec = Get-VM $VM.Name | where {$_.Extensiondata.Config.FtInfo.Role -eq 2}| Get-View
$VMobj = New-Object VMware.Vim.ManagedObjectReference
$VMobj.type = "VirtualMachine"
$VMobj.value = $VMsec.MoRef.Value
$VMview = $VM | Get-View
$VMview.EnableSecondaryVM($VMobj, $null)
}
}elseif ($disableSecondaryFT) {
if ($VM.ExtensionData.Config.FtInfo -ne $null){
$VMsec = Get-VM $VM.Name | where {$_.Extensiondata.Config.FtInfo.Role -eq 2 -and $_.PowerState -eq "PoweredOn"}| Get-View
if ($VMsec -ne $null){
$VMobj = New-Object VMware.Vim.ManagedObjectReference
$VMobj.type = "VirtualMachine"
$VMobj.value = $VMsec.MoRef.Value
$VMview = $VM | Get-View
$VMview.DisableSecondaryVM($VMobj)
}else {
Write-Host "The Secondary is already disabled"
}
}else {
Write-Host "This VM is not FT enabled"
}
}elseif ($failOverFT) {
if ($VM.ExtensionData.Config.FtInfo -ne $null){
$VMsec = Get-VM $VM.Name | where {$_.Extensiondata.Config.FtInfo.Role -eq 2 -and $_.PowerState -eq "PoweredOn"}| Get-View
if ($VMsec -ne $null){
$VMobj = New-Object VMware.Vim.ManagedObjectReference
$VMobj.type = "VirtualMachine"
$VMobj.value = $VMsec.MoRef.Value
$VMview = $VM | Get-View
$VMview.MakePrimaryVM($VMobj)
}else {
Write-Host "The Secondary is disabled"
}
}else {
Write-Host "This VM is not FT enabled"
}
}elseif ($restartSecondaryFT) {
if ($VM.ExtensionData.Config.FtInfo -ne $null){
$VMsec = Get-VM $VM.Name | where {$_.Extensiondata.Config.FtInfo.Role -eq 2 -and $_.PowerState -eq "PoweredOn"}| Get-View
if ($VMsec -ne $null){
$VMobj = New-Object VMware.Vim.ManagedObjectReference
$VMobj.type = "VirtualMachine"
$VMobj.value = $VMsec.MoRef.Value
$VMview = $VM | Get-View
$VMview.TerminateFaultTolerantVM($VMobj)
}else {
Write-Host "The Secondary is disabled"
}
}else {
Write-Host "This VM is not FT enabled"
}
}elseif ($removeFT){
if ($VM.ExtensionData.Config.FtInfo -ne $null){
$VMview = Get-VM $VM | where {$_.Extensiondata.Config.FtInfo.Role -eq 1}| Get-View
$VMview.TurnOffFaultToleranceForVM()
} else {
Write-Host "This VM is not FT enabled"
}
}
}
I have made a powershell script function to update information about what VLAN and IP the VM´s have. This for the IT Admin to easily see in the VMM 2012 Console. Of course the function can be customized to populate any other information available for the VM´s, maybe you want to display create date or something else that is not in the standard properties and you do not want to use the pre-made custom# .
I am using the Get-IPAddress from Ben Wilkinson, This requires that the VMM server can resolve the IP of the VM´s from a DNS.
If I do not have an DNS or the VLAN info is not stored, i can via the parameters update the properties by -IP or -VLANid.
As you can see on this screen dump, by default i only have custom#, to add my own i have to click on “Manage Custom Properties”
Here i press “Create” and add a name and optionally a description
Then when i have created the custom properties i want i add them,
Then it looks like this,
Now the powershell function comes into play when i am going to populate these fields with information.
a screendump of my powershell console, as you can see I am using the (Get-SCVMHostCluster).Nodes to get all virtual machines on these, also i am using a foreach (%) to run the population on each VM.
I have made a simple powershell function that can move VM´s from one cluster to another and also set the network configuration, this because the old cluster had one way of setup regarding the virtual machine network and the new another more flexible with promiscuous mode set on the HP nic team so i do not need to create one virtual switch for every vlan. The function also checks if the actual VM has snapshots before moving it. (the SCVMM calls it checkpoints, which is stupid to call it one thing in hyper-v and another in SCVMM!)
Had a plan to do a blog post last week but was so busy studying for the VCP 5 test. I wrote the test on friday and i passed 🙂
I have created a little powershell function, this to get the inventory information about the virtual machines running in hosts or clusters that are connected to a System Center Virtual Machine Manager. This function collects number of configured vCPU´s, Memory, information about the connected virtual harddisks. This kind of information could be handy as in the case with one customer that has not yet implemented a host based backup solution and still use agents in their VM´s, so if they need to recover machines it is good to know what size and configuration it had if it is lost by some reason. This report can also be handy for some info on how much your datastores are filled. As you can se i both report the maximum size and the current (as dynamic disks are not allocating the whole size at creation time)
The function both export a csv and a HTML report
After importing it to excel it can look something like this:
And the html output is very simple but can be put on a internal web for IT production
I have used the Get-SCVirtualDisk to get the Bus Type and LUN id as this information is not available in Get-SCVirtualHarddisk, this information can be good if i lost the virtual machine configuration and want to connect the virtual harddisks to the right controller.
the function looks like this: (if you copy this you have to move the first # up to the <, the blog syntax highlighter does something and move it down :-( )
function Get-VMdata{
<#
.SYNOPSIS
Get the configuration data of the VMs in Hyper-V via SCVMM 2012
.DESCRIPTION
Use this function to get all VMs configuration in case of disaster or just statistics
.PARAMETER xyz
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-02-13
#>
param (
$VMHostGroup = "All Hosts",
[Parameter(ValueFromPipeline=$True)][Alias('ClusterName')]
$VMHostCluster = $null,
$VMHost = $null,
[string]$CSVFile = "VMdata.csv",
[string]$HTMLReport = "VMdata.html"
)
$report = @()
if ($VMHostCluster -eq $null){
$VMHosts = (Get-SCVMHostGroup -Name $VMhostGroup).AllChildHosts
}else{
$VMHosts = (Get-SCVMHostCluster -Name $VMHostCluster).Nodes
}
$VMs = $VMHosts | Get-SCVirtualMachine
foreach ($VM in $VMs) {
$VHDs = $VM | Get-SCVirtualDiskDrive
$i = "1"
foreach ($VHDconf in $VHDs){
if($i -eq "1"){
$data = New-Object PSObject -property @{
VMName=$VM.Name
vCPUs=$VM.CPUCount
MemoryGB= $VM.Memory/1024
VHDName = $VHDconf.VirtualHardDisk.Name
VHDSize = $VHDconf.VirtualHardDisk.MaximumSize/1GB
VHDCurrentSize = [Math]::Round($VHDconf.VirtualHardDisk.Size/1GB)
VHDType = $VHDconf.VirtualHardDisk.VHDType
VHDBusType = $VHDconf.BusType
VHDBus = $VHDconf.Bus
VHDLUN = $VHDconf.Lun
VHDDatastore = $VHDconf.VirtualHardDisk.HostVolume
}
$i= "2"
}else{
$data = New-Object PSObject -property @{
VMName=""
vCPUs=""
MemoryGB= ""
VHDName = $VHDconf.VirtualHardDisk.Name
VHDSize = $VHDconf.VirtualHardDisk.MaximumSize/1GB
VHDCurrentSize = [Math]::Round($VHDconf.VirtualHardDisk.Size/1GB)
VHDType = $VHDconf.VirtualHardDisk.VHDType
VHDBusType = $VHDconf.BusType
VHDBus = $VHDconf.Bus
VHDLUN = $VHDconf.Lun
VHDDatastore = $VHDconf.VirtualHardDisk.HostVolume
}
}
$report +=$data
}
}
$report | Select-Object VMName,vCPUs,MemoryGB,VHDName,VHDSize,VHDCurrentSize,VHDType,VHDBusType,VHDBus,VHDLUN,VHDDatastore | Export-Csv -Path $CSVFile -NoTypeInformation -UseCulture
$report | Select-Object VMName,vCPUs,MemoryGB,VHDName,VHDSize,VHDCurrentSize,VHDType,VHDBusType,VHDBus,VHDLUN,VHDDatastore | ConvertTo-HTML | Out-File $HTMLReport
}
and i can run it in some different ways, as you see in this screenshot, if i do not give any parameters it will use VMHostGroup “All Hosts” as default
After bare metal deployment of the hosts we need to configure the network on the hosts, this can be done from the SCVMM 2012 powershell console.
I have made a function of the creation of first a virtual switch and also adding the logical networks, if i do not add the networks to the particular host nic where the virtual switch is i cannot deploy virtual machines and get them connected to the network.
You can add the logical networks and their associated vlan/subnet before any host is imported into SCVMM and of course i have created a function for that also, this can take a csv file or just the parameters
Here is a screen dump if i run it without a csv file
and the result is the following
And here is the powershell code for the add logical networks function
function Add-SCLogicalNetworks{
<#
.SYNOPSIS
Add logical networks to your SCVMM fabric
.DESCRIPTION
With this function you add logical networks
.PARAMETER CSVNetworks
Path to a CSV file with the logical network info
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-02-02
#>
param (
$CSVNetworks = "",
$VMHostGroup = "All Hosts",
[string]$Name = "",
[string]$IPnet = "",
[string]$Octet = "24",
[int]$VLAN = 0
)
$VMHostGroup = Get-SCVMHostgroup $VMHostGroup
if ($CSVNetworks -ne "") {
$VLANs = Import-Csv $CSVNetworks -Delimiter ";"
foreach ($VLAN in $VLANs){
$LogicalNetwork = New-SCLogicalNetwork -Name $VLAN.Name
$Network = $VLAN.IPnet + "/" + $VLAN.Octet
$SubnetVlan = New-SCSubnetVLan -Subnet $Network -VLanID $VLAN.VLAN
New-SCLogicalNetworkDefinition -Name $VLAN.Name -LogicalNetwork $logicalNetwork -VMHostGroup $VMHostGroup -SubnetVLan $SubnetVlan
}
}else{
$LogicalNetwork = New-SCLogicalNetwork -Name $Name
$Network = $IPnet + "/" + $Octet
$SubnetVlan = New-SCSubnetVLan -Subnet $Network -VLanID $VLAN
New-SCLogicalNetworkDefinition -Name $Name -LogicalNetwork $logicalNetwork -VMHostGroup $VMHostGroup -SubnetVLan $SubnetVlan
}
}
when i have the logical networks i want to configure the host
And the result is the following
And this is also displayed on the physical nic
and here is the powershell code for configuring the host:
function Configure-SCVMhostNetwork{
<#
.SYNOPSIS
Configure the network on the Hyper-V Host
.DESCRIPTION
With this function you set virtual switch and configure logical networks
.PARAMETER VMHost
The new host that is going to be configurered
.PARAMETER refVMHost
if there are different hyper-v clusters and logical networks, use a ref host for the configuration to set it up
.PARAMETER VMHostnic
The nic that is going to be configured with a virtual switch (in this case a NIC team named "VM"
.EXAMPLE
PS C:\> Get-SCVMHost Hyp04 | Configure-SCVMhostNetwork
.EXAMPLE
PS C:\> Configure-SCVMhostNetwork -VMHost Hyp04 -refVMHost Hyp06
.NOTES
Author: Niklas Akerlund / RTS
Date: 2012-02-02
#>
param (
[Parameter(Position=0,Mandatory=$true,HelpMessage="This need to be a Hyper-V Host name or object",
ValueFromPipeline=$True)]
$VMHost,
[string]$refVMHost = "",
[string]$VirtualNetworkName = "VMs",
[string]$VMHostnic = "VM"
)
$VMHost = Get-VMHost $VMHost
if ($VMHost -ne $null) {
if ($refVMHost -eq ""){
$LogicalNetworks = Get-SCLogicalNetwork
}else
{
$LogicalNetworks = Get-SCLogicalNetwork -VMHost $refVMHost
}
$vmHostNetworkAdapter = Get-SCVMHostNetworkAdapter -VMHost $VMHost -Name $VMHostnic
$virtualNetwork = New-SCVirtualNetwork -VMHost $VMHost -Name $VirtualNetworkName -Description "" -BoundToVMHost $false -VMHostNetworkAdapters $vmHostNetworkAdapter
$vmHostNetworkAdapter = Get-SCVMHostNetworkAdapter -Name $VMHostnic -VMHost $VMHost
foreach ($LogicalNet in $LogicalNetworks){
Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $vmHostNetworkAdapter -AddOrSetLogicalNetwork $logicalNet
}
}
}
I have set up an scheduled task in a windows 2003 that run a PowerShell script, this script runs every week and the purpose is to clean out logs from IIS that otherwise fills the volume.
I used the script from Thomas Maurer to check a folder for files to delete and have customized it after excellent input from Jeffery Hicks, he suggested that i put in parameters instead to make the script more flexible and that way it can be run on any folder in the server with -Path and also set number of days to save -DaysBack, the last parameter has a default value of 30 days:
# Delete all Files in specifed parameter Path
# and parameter older than 30 day(s) default or
# set by parameter DaysBack
#
# Parameter delyxe Niklas Akerlund / RTS
param(
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]$Path,
$DaysBack = 30)
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays(-$Daysback)
if($Path -ne ""){
Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
}
I saved it in C:\Scripts\Clean and you must set the execution policy in PowerShell console otherwise you will not be able to run any scripts,
Set-ExecutionPolicy -ExecutionPolicy Remotesigned
then i created the scheduled task, some screen shots follows with the way to set up this, first selesct PowerShell
then select interval that the script is going to be run
I have selected that the script is going to be executed every week at mondays 9 AM
Then set an account and password and click next, the magic comes when going into the advanced properties
As you can see i have added a -File C:\Scripts\Clean\clean.ps1 -Path “C:\windows\system32\LogFiles\W3SVC1” -DaysBack 20 to the PowerShell.exe in the run field, otherwise i will not run any scripts 😉 only the powershell, the -File was also a suggestion from Jeffery Hicks 🙂
Some days ago I have been working on some powershell scripts for a lab environment and now i have edited the handlescript to work better and with more functionality.
First i created a small script for populating some OU with student accounts.
Then i created with help from Jeffery Hicks post a script with a menu to enable or disable accounts in the different OU´s. As i described in a earlier post it is quite powerful to use Active Directory cmdlets and i have tried to take care of that by checking that when i enable/disable accounts i check that i really have something in my searchbase. Below are two screenshots of the script running. Update I have now updated the script with a check for the searchbase that actually works and also an extra menu option with the ability to set password on the accounts i enable!
Here is the script that creates this, i have not yet come up with a better and more dynamic way to create the switch, maybe someone can give me some help there..