Archive by Author | AJ McKean

Set Hyper-V VM Resynchronization Schedule via PowerShell

We were recently working through a multi-site Hyper-V VDI deployment for a customer and needed to set the Hyper-V replica ReSync Schedule for all VM’s and ensure that Auto Resync was enabled.¬† Per Host, this was accomplished by running the following script:

get-vm | Where-Object ReplicationMode -EQ “Primary” | Set-VMReplication -AutoResynchronizeEnabled $True -AutoResynchronizeIntervalStart “00:00:00” -AutoResynchronizeIntervalEnd “23:59:59”

 

For all VM’s, we used the following script:

$HVHOSTS = Get-ADComputer -Filter {Name -like “*host*” -and Name -notlike “*hvhost*”} | Select -ExpandProperty Name #NOTE: Your filter will look different , adjust to suit
foreach ($HVHOST in $HVHOSTS){
$VMs = get-vm -ComputerName $HVHOST | Where-Object ReplicationMode -eq “Primary”
foreach ($VM in $VMs){

Set-VMReplication -ComputerName $HVHOST -VMName $VM.Name -AutoResynchronizeEnabled $True -AutoResynchronizeIntervalStart “00:00:00” -AutoResynchronizeIntervalEnd “23:59:59”

}

}

Disclaimer: As with any script – please review & lab test in your own environment, before rolling out to production! ūüôā

Updates!

I figured after almost a year of no posts, is was about time I added something!¬† It’s been a busy year, which has seen our business expand, our team increase in size¬†and has seen us working¬†through some really cool projects.¬† I’ll try and get a few more posts up here but in the meantime, please check our website over at http://www.synergytech.co.nz.¬† Watch this space!

Service Manager PowerShell Workflow: The operation has timed out

Recently I was working on a PowerShell Workflow for Auto Group Assignment for Service Requests and Incidents for a customer and ran into a snag… Effectively, although the script appeared to be running as it should and the Support Group would auto-assign I received the following error:

System.TimeoutException: The operation has timed out.
at Microsoft.EnterpriseManagement.TaskRuntimeManagement.ExecuteTaskInternal[T](IEnumerable`1 targets, Guid taskId, TaskConfiguration configuration)
at Microsoft.EnterpriseManagement.TaskRuntimeManagement.ExecuteTask[T](IEnumerable`1 targets, ManagementPackTask task, TaskConfiguration configuration)
at Microsoft.ServiceManager.WorkflowAuthoring.ActivityLibrary.TaskExecutor.RunTask(String sdkServerName, Guid taskId, IList`1 taskTargetIds, Dictionary`2 taskArguments, Int32 taskTimeout)
at Microsoft.ServiceManager.WorkflowAuthoring.ActivityLibrary.RunTaskActivity.Execute(ActivityExecutionContext executionContext)
at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime)
at System.Workflow.Runtime.Scheduler.Run()

Multiple re-creations of my workflow using the authoring tool and manual script execution showed the script was working as intended. ¬†It turns out the issue was that the SQL Broker wasn’t enabled for the ServiceManager Database, thankfully an easy fix.

  • Stopped the SQL Server Agent
  • Stopped all Service Manager related services
  • Checked the Broker Enabled Status via SQL Query: SELECT is_broker_enabled from sys.databases WHERE name = ‘ServiceManager’. ¬†This returned 0 (it wasn’t enabled)
  • Ran the Query ALTER DATABASE ServiceManager SET ENABLE_BROKER. ¬†Note: ¬†If it times out due to a lingering connection,¬†“alter database ServiceManager set enable_broker with rollback immediate;” does the trick too.
  • Restarted the SQL Server Agent and all Service Manager services and Workflows now run as expected!

Check disk space remotely with PowerShell

I work with a customer with a large VDI environment.  They had asked for a weekly report to generate with an overview of VDI disk free space.  I have created the script below to do just that.

Effectively it has three parameters (ie Get-DiskSpaceReport.ps1 – To userreceivingthereport@company.com – SMTPServer smtp.company.com -From monitoringserver@company.com). ¬†It runs against the AD Computer filter specified in the script (in my case it’s computers with the name VDI in it). ¬† After the report is ran, you receive an e-mail with the results sorted by % Free:

 

This is the result:

Param (
$To,
$SMTPServer,
$From
)

Import-Module ActiveDirectory
$VDISearch = get-adcomputer -filter * | Where-Object Name -Like *VDI* #Change this filter to suit your needs

$VDIs = $VDISearch.Name
$Results = @()
foreach ($VDI in $VDIs){
$disk = Get-WmiObject win32_logicaldisk -ComputerName $VDI -Filter “Drivetype=3” -ErrorAction SilentlyContinue
$Results += New-Object PSObject -Property @{
‘VDI’ = $Disk.SystemName
‘Drive Letter’ = $Disk.DeviceID + ‘\’
‘Free Space (GB)’ = ((“{0:N1}” -f ($Disk.FreeSpace/1GB)) + ‘ GB’)
‘% Free’ = ((“{0,6:P0}” -f (($Disk.FreeSpace/1GB)/($Disk.Size/1GB)) ))
}
}

$Header = @”
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
“@

$splat = @{
From = $From
To = $To
SMTPServer = $SMTPServer
Subject = “VDI Disk Space Report Generated by $env:computername”

}
$Results = $Results | Select ‘VDI’, ‘Drive Letter’, ‘Free Space (GB)’,’% Free’ | Sort ‘% Free’

$Body = $Results | ConvertTo-Html -Head $Header | Out-String

Send-MailMessage @splat -Body $Body -BodyAsHTML

 

Check registry for key value for remote servers/computers

I’ve had a couple of requests from customers this week to check for the presence of a specific registry key. ¬†One looking for 2008/2008R2 servers: Get-ADComputer -Filter {OperatingSystem -Like “Windows Server* 2008”} -Properties * or others against a security group : Get-ADComputer -filter * -Property * | where {$_.memberof -match ‘YOURGROUPNAMEHERE’}. ¬†Here’s a sample of it to check for all systems with .NET 4.6.2 installed and outputs it to a text file:

$PatchStatus=@{}
$RemoteSystems= Get-ADComputer -filter * -Property *
$RemoteHosts = $RemoteSystems.Name

foreach ($RemoteHost in $RemoteHosts){
$Hive = [Microsoft.Win32.RegistryHive]”LocalMachine”;
$RegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive,$RemoteHost);
$Ref = $RegKey.OpenSubKey(“SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\”);
$RefValue = $Ref.GetValue(“Release”)
if ($RefValue -ge “394806”){$PatchStatus.Add($RemoteHost,”Patched”)}
else {$PatchStatus.Add($RemoteHost,”Needs .NET”)}
}

$PatchStatus | Out-File “C:\Temp\Needs-DotNET4.6.2.txt”

PowerShell Script to Move Hyper-V VM’s

Quick easy PowerShell script to move VM’s from one Hyper-V host to another:

Get-VM -computer SOURCESERVERNAME | Out-GridView -Title “Select one or more VMs to Live Migrate” -PassThru | Move-VM -DestinationHost DESTINATIONSERVERNAME -DestinationStoragePath J:\Hyper-V (Or whatever your destination path is)

Updates (or the lack)…

Apologies for the lack of updates – it’s been a crazy few months! ¬†Have just moved back to New Zealand after 14 months in the U.S. and have started my own business. ¬†I’ll try and do a bit better with updating this blog moving forward. Appreciate the patience!

“Storage migration for virtual machine ‘servername’ failed with error ‘General access denied error’ (0x80070005).

I was recently moving storage for a customer’s Hyper-V VM from one CSV to another new storage tier. On moving the VHD, I selected the root of the storage folder as the destination. After¬†starting the storage move, I received the following error:
“Storage migration for virtual machine ‘servername’ failed with error ‘General access denied error’ (0x80070005). Migration did not succeed. Could not start mirror operation for the VHD file C:\ClusterStorage\Volume5\servername\disk-2.vhdx to C:\ClusterStorage\Volume2\disk-2.vhdx’: General access denied error’ (0x80070005).

2016-09-27_9-40-37

As generally Hyper-V will create a folder name for the VM when selecting the root folder, I hadn’t given it much thought however after creating a folder with the name of the server and selecting it as the destination it completed successfully.

2016-09-27_9-46-52

HP 3PAR with SCOM “Unable to connect to the remote server”

I was recently configuring the HP 3PAR monitoring for System Center Operations Manager 2012R2 for a customer. ¬†After installing the HP SCOM Storage Monitoring Application and adding my local management server, I proceeded to add the HP 3PAR Storage System¬†via the¬†HP Storage Management Pack User Configuration Tool and received the error “Unable to connect to the remote server” whenever I attempted to connect.

2016-09-26_17-28-13

I confirmed my username/password was correct – even setup a new account, all with the same result. ¬†It turns out that the CIM service was not running on the 3PAR, which is a requirement for the application/management pack to function. ¬†I connected to the storage system via SSH, typed “showcim” and confirmed that it was not functional. ¬†Running the command “startcim” started the CIM service and after a few seconds, returned to the HP Storage System dialog and re-connect with success!

2016-09-26_17-41-08

2016-09-26_17-28-50

Finally, after enabling discovery in SCOM for the Management Server and waiting a couple of minutes the storage system appeared in SCOM and reported as it should.

2016-09-27_9-26-23

 

Hyper-V 2012R2/VMM 2012R2 VM “Missing” status/Stuck “Starting”

I recently had an issue with a customer with a missing VM after it was shutdown for maintenance.  As soon as it was started in Virtual Machine Manager, it immediately went to a Failed state and was missing from all Hyper-V hosts and stuck in a Failed state in Failover Cluster Manager.

2016-09-08_15-39-132016-09-08_15-38-27

The unusual thing was VMM and Failover Cluster manager thought they were on different hosts(follow steps below for the host that Cluster Manager says it should be on). The issue was due to the missing symbolic link to the VM on the Host it was missing from.

The fix was to re-create the symbolic link on the Host Failover Cluster Manager thinks it’s missing from. ¬†First, find the GUID for your¬†VM from the source location for the specific¬†VM config:

2016-09-08_16-01-23

Then browse to¬†C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines on the host in question. ¬†Run CMD prompt as Administrator from the folder¬†and create the link with the following command:¬†Mklink FA060046-2C40-4D8E-A6EA-2A4587A84B3B.xml “C:\ClusterStorage\Volume4\YOURVMNAMEHERE\Virtual Machines\FA060046-2C40-4D8E-A6EA-2A4587A84B3B.xml”

Then grant permissions:

icacls “%SYSTEMDRIVE%\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines\FA060046-2C40-4D8E-A6EA-2A4587A84B3B.xml” /grant “NT VIRTUAL MACHINE\FA060046-2C40-4D8E-A6EA-2A4587A84B3B”:F /l

2016-09-08_16-09-08

Finally restart the Hyper-V Virtual Machine Service and all should be back online!

2016-09-08_15-44-13

2016-09-08_15-44-43