So, if you are in the Fabric node of SCVMM 2012 SP1 you might happen to right click on a Hyper-V Server and notice the option “Run Script Command”.
This is great, it lest me push commands out to my Hyper-V Servers and run them, just like I do with Application Scripts in Service Templates.
I recently ran into a situation where I needed to enable the Network Virtualization binding on all of my Hyper-V Servers (seven of them). And I had no desire to open a PSSession to each and run my script block that enables it.
Since I discovered this Script Command option in SCVMM, why not use it, I am here.
Like any good SCVMM admin I defined the command and then looked at the PowerShell script behind it.
(The big problem here is that I don’t see a way to save these Script Commands and run them again. Big fail there.)
I then took that script and made it a bit more useful to me by getting all of my Hyper-V hosts and running my command on all of them at the same time.
And I figured that ‘why not’ this is the one step that all the SCVMM Network Virtualization setup is missing. Frankly, I would expect SCVMM just to do this for me, but alas SP1 does not.
Here is my execution:
import-module virtualmachinemanager
$scriptSetting = New-SCScriptCommandSetting
Set-SCScriptCommandSetting -ScriptCommandSetting $scriptSetting -WorkingDirectory "" -PersistStandardOutputPath "" -PersistStandardErrorPath "" -MatchStandardOutput "" -MatchStandardError ".+" -MatchExitCode "[1-9][0-9]*" -FailOnMatch -RestartOnRetry $false -AlwaysReboot $false -MatchRebootExitCode "" -RestartScriptOnExitCodeReboot $false
$RunAsAccount = Get-SCRunAsAccount -Name "DomainAdmin"
$VMHosts = Get-SCVMHost | where { $_.VirtualizationPlatform -eq "HyperV" }
$scriptBlock = {
$vSwitch = Get-VMSwitch -SwitchType External
ForEach-Object -InputObject $vSwitch {
if ((Get-NetAdapterBinding -ComponentID "ms_netwnv" -InterfaceDescription $_.NetAdapterInterfaceDescription).Enabled -eq $false){
Enable-NetAdapterBinding -InterfaceDescription $_.NetAdapterInterfaceDescription -ComponentID "ms_netwnv"
}
}
}
ForEach ($VMHost in $VMHosts) {
Invoke-SCScriptCommand -Executable "%WINDIR%\System32\WindowsPowerShell\v1.0\PowerShell.exe" -TimeoutSeconds 120 -CommandParameters "-ExecutionPolicy RemoteSigned -command $scriptBlock" -VMHost $VMHost -ScriptCommandSetting $scriptSetting -RunAsAccount $RunAsAccount
}
Be sure to have the SCVMM Console installed, and modify your RunAs account name.
That Set-SCScriptCommandSetting line is all about the error handling for the script. The only non-default setting I have in here is making sure that my hypervisors were not rebooted, no matter what happened.