1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<#Hi Guys Sharing with you all a script which i made to check Time Synchronization Status from within a VM for a Hyper-V Host#> Function Get-VMTimeSync { $VMhost = Get-ItemProperty "HKLM:SOFTWAREMicrosoftVirtual MachineGuestParameters" | select -ExpandProperty physicalhostname $VirtualMachineName = Get-ItemProperty "HKLM:SOFTWAREMicrosoftVirtual MachineGuestParameters" | select -ExpandProperty VirtualMachineName $session = New-PSSession -ComputerName $vmhost Invoke-Command -Session $session { param($vmname) $MgmtSvc = gwmi -namespace rootvirtualization MSVM_VirtualSystemManagementService $VM = gwmi -namespace rootvirtualization MSVM_ComputerSystem |?{$_.elementname -match $vmname} $TimeSyncComponent = gwmi -query "associators of {$VM} where ResultClass = Msvm_TimeSyncComponent" -namespace rootvirtualization $TimeSyncSetting = gwmi -query "associators of {$TimeSyncComponent} where ResultClass = Msvm_TimeSyncComponentSettingData" -namespace rootvirtualization # Disable = 3; Enable = 2 if ($TimeSyncSetting.EnabledState -eq 3 ){ Write-Output "Time Synchronzation Disabled" } else { Write-Output "Time Synchronzation Enabled"} } -Args $VirtualMachineName Get-PSSession | Remove-PSSession } |
I Ran the same function From within the Virtual Machine in my test LAB, and saw that the Time Synchronization was in disabled state for my VM :
Just a tip: Using text output for functions is not very "powershell." This does not lend itself well to using composable pipelines. Consider returning a Boolean (i.e. $true or $false) and renaming your command to Test-VMTimeSync. The "Test" verb is associated with Boolean returns. This way, you can use Test-VMTimeSync as a condition in an "if" clause to some other action, like Enable-VMTimeSync (or Disable-*)
Thanks Oisin Let Me Give it a Try