Public/Get-GuestVMAndHypervisorInfo.ps1
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 |
<#
.SYNOPSIS This function is meant to be used in situations where Docker-For-Windows (Docker CE) is being installed on a Windows Guest VM (as opposed to Bare Metal). It gathers a lot of information about the Windows Guest VM, as well as its Hyper-V hypervisor (if appropriate credentials are provided). The function can be run from anywhere as long as it's pointed at a Guest VM (via the -TargetHostNameOrIP or -TargetVM parameters). If you do not have access to the Guest VM's hypervisor or if the hypervisor is not Hyper-V, make sure to use the -TryWithoutHypervisorInfo switch. .DESCRIPTION See .SYNOPSIS .PARAMETER TargetHostNameOrIP This parameter is OPTIONAL. This parameter takes a string that represents the IP Address, DNS-Resolvable HostName, or FQDN of the Guest VM that you would like to gather info from. If it is NOT used (and if the -TargetVMName parameter is not used), the function will assume that the localhost is the Guest VM that you would like to gather information about. .PARAMETER TargetVMName This parameter is MANDATORY (for its parameter set). This parameter takes a string that represents the name of the Hyper-V VM that you would like to gather info from. Using this parameter requires that you use the -HypervisorFQDNOrIP and -HypervisorCreds parameters, unless the localhost IS the Hyper-V hypervisor. .PARAMETER HypervisorFQDNOrIP This parameter is OPTIONAL. This parameter takes a string that represents the IP, DNS-Resolvable HostName, or FQDN of the Hyper-V hypervisor that is managing the Guest VM that you would like to gather info from. If the localhost is NOT the Hyper-V hypervisor and if you are NOT using the -TryWithoutHypervisorInfo switch, then this parameter becomes MANDATORY. .PARAMETER TargetHostNameCreds This parameter is OPTIONAL. This parameter takes a pscredential object that contains credentials with permission to access the Guest VM that you would like to gather info from. If the localhost is the Guest VM target, or if you are logged in as a user that already has access to the Guest VM, then you do NOT need to use this parameter. .PARAMETER HypervisorCreds This parameter is OPTIONAL. This parameter takes a pscredential object that contains credentials with permission to access the Hyper-V hypervisor that is managing the Guest VM that you would like to gather info from. If the localhost IS the Hyper-V hypervisor, or if you are logged in as a user that already has access to the Hyper-V hypervisor, then you do NOT need to use this parameter. .PARAMETER TryWithoutHypervisorInfo This parameter is OPTIONAL. This parameter is a switch. If used, this function will not attempt to gather any information about the hypervisor managing the target Guest VM. Be sure to use this switch if you do not have access to the hypervisor or if the hypervisor is not Hyper-V. .PARAMETER AllowRestarts This parameter is OPTIONAL. This parameter is a switch. By default, this function installs Hyper-V on the target Guest VM is it isn't already. This is part of a test that is used to determine if an External vSwitch attached to a Nested VM on the Guest VM can actually reach an outside network. If Hyper-V has not already been installed on the target Guest VM, then a restart will be required. You can use this switch to allow the Guest VM to restart. The function will remain in a holding pattern until the Guest VM comes back online, so you will NOT need to run this function twice. .PARAMETER NoMacAddressSpoofing This parameter is OPTIONAL. This parameter is a switch. If used, this function will NOT conduct the test to determine if an External vSwitch attached to a Nested VM on the Guest VM can reach an outside network. In other words, the assumption will be that all Nested VMs will be on a Hyper-V Internal network on the Guest VM. HOWEVER, since it is still possible to configure networking such that Nested VMs on an Internal Hyper-V network can reach an outside network, a test will still be conducted to determine if an Internal vSwitch attached to a Nested VM can reach an outside network via NAT. .PARAMETER SkipHyperVInstallCheck This parameter is OPTIONAL. This parameter is a switch. If used, this function will assume that Hyper-V is already installed on the target Guest VM and not attempt any verification or installation. .PARAMETER SkipExternalvSwitchCheck This parameter is OPTIONAL. This parameter is a switch. If used, this function will NOT conduct the test to determine if an External vSwitch attached to a Nested VM on the Guest VM can reach an outside network. In other words, the assumption will be that all Nested VMs will be on a Hyper-V Internal network on the Guest VM. .EXAMPLE # Open an elevated PowerShell Session, import the module, and - PS C:\Users\zeroadmin> Get-GuestVMAndHypervisorInfo #> function Get-GuestVMAndHypervisorInfo { [CmdletBinding(DefaultParameterSetName='Default')] Param( [Parameter( Mandatory = $False, ParameterSetName = 'Default' )] [string]$TargetHostNameOrIP, [Parameter( Mandatory=$True, ParameterSetName = 'UsingVMName' )] [string]$TargetVMName, [Parameter(Mandatory=$False)] [string]$HypervisorFQDNOrIP, [Parameter(Mandatory=$False)] $TargetHostNameCreds, [Parameter(Mandatory=$False)] $HypervisorCreds, [Parameter(Mandatory=$False)] [switch]$TryWithoutHypervisorInfo, [Parameter(Mandatory=$False)] [switch]$AllowRestarts, # -NoMacAddressSpoofing WILL result in creating a Local NAT with an Internal vSwitch on the # Target Machine (assuming it's a Guest VM). Maybe change this parameter to 'CreateNAT' instead # of 'NoMacAddressSpoofing' [Parameter(Mandatory=$False)] [switch]$NoMacAddressSpoofing, [Parameter(Mandatory=$False)] [switch]$SkipHyperVInstallCheck, [Parameter(Mandatory=$False)] [switch]$SkipExternalvSwitchCheck ) if ($PSBoundParameters['TargetHostNameCreds']) { if ($TargetHostNameCreds.GetType().FullName -ne "System.Management.Automation.PSCredential") { Write-Error "The object provided to the -TargetHostNameCreds parameter must be a System.Management.Automation.PSCredential! Halting!" $global:FunctionResult = "1" return } } if ($PSBoundParameters['HypervisorCreds']) { if ($HypervisorCreds.GetType().FullName -ne "System.Management.Automation.PSCredential") { Write-Error "The object provided to the -HypervisorCreds parameter must be a System.Management.Automation.PSCredential! Halting!" $global:FunctionResult = "1" return } } if (!$TargetHostNameOrIP -and !$TargetVMName) { $TargetHostNameOrIP = $env:ComputerName } if ($TargetVMName) { if ($TryWithoutHypervisorInfo) { $ErrMsg = "Using the -TargetVMName parameter requires that we gather information from " + "the hypervisor, but the -TryWithoutHypervisorInfo switch was also supplied. Impossible situation. Halting!" Write-Error $ErrMsg $global:FunctionResult = "1" return } if (!$HypervisorFQDNOrIP) { # Assume that $env:ComputerName is the hypervisor $HypervisorFQDNOrIP = $env:ComputerName if ($(Get-Module -ListAvailable).Name -notcontains "Hyper-V" -and $(Get-Module).Name -notcontains "Hyper-V") { Write-Warning "The localhost $env:ComputerName does not appear to be a hypervisor!" $HypervisorFQDNOrIP = Read-Host -Prompt "Please enter the FQDN or IP of the hypervisor that manages $TargetVMName" } } } # We only REALLY need to resolve the Hypervisor's network location if we are using $TargetVMName if ($HypervisorFQDNOrIP) { try { $HypervisorNetworkInfo = ResolveHost -HostNameOrIP $HypervisorFQDNOrIP -ErrorAction Stop } catch { if ($TargetVMName) { Write-Error "Unable to resolve $HypervisorFQDNOrIP! Halting!" $global:FunctionResult = "1" return } else { Write-Warning "Unable to resolve $HypervisorFQDNOrIP!" # In which case, we need to TryWithoutHypervisorInfo $TryWithoutHypervisorInfo = $True } } } ## BEGIN $TargetVMName adjudication ## if ($TargetVMName) { # If $TargetVMName is provided (as opposed to $TargetHostNameOrIP), it is MANDATORY that we get # Hyper-V Hypervisor Info. If we can't for whatever reason, we need to HALT. # Make sure the $TargetVMName exists on the hypervisor and get some info about it from the Hypervisor's perspective if ($HypervisorNetworkInfo.HostName -ne $env:ComputerName) { $InvokeCommandSB = { try { $TargetVMInfoFromHyperV = Get-VM -Name $using:TargetVMName -ErrorAction Stop $VMProcessorInfo = Get-VMProcessor -VMName $using:TargetVMName $VMNetworkAdapterInfo = Get-VmNetworkAdapter -VmName $using:TargetVMName $VMMemoryInfo = Get-VMMemory -VmName $using:TargetVMName } catch { Write-Error "Unable to find $using:TargetVMName on $($using:HypervisorNetworkInfo.HostName)!" return } # Need to Get $HostNameNetworkInfo via Network Adapter IP $GuestVMIPAddresses = $TargetVMInfoFromHyperV.NetworkAdapters.IPAddresses [pscustomobject]@{ HypervisorComputerInfo = Get-CimInstance Win32_ComputerSystem HypervisorOSInfo = Get-CimInstance Win32_OperatingSystem TargetVMInfoFromHyperV = $TargetVMInfoFromHyperV VMProcessorInfo = $VMProcessorInfo VMNetworkAdapterInfo = $VMNetworkAdapterInfo VMMemoryInfo = $VMMemoryInfo GuestVMIPAddresses = $GuestVMIPAddresses } } $GetWorkingCredsSplatParams = @{ RemoteHostNameOrIP = $HypervisorNetworkInfo.FQDN ErrorAction = "Stop" } if ($HypervisorCreds) { $GetWorkingCredsSplatParams.Add("AltCredentials",$HypervisorCreds) } try { $GetHypervisorCredsInfo = GetWorkingCredentials @GetWorkingCredsSplatParams if (!$GetHypervisorCredsInfo.DeterminedCredsThatWorkedOnRemoteHost) {throw "Can't determine working credentials for $($HypervisorNetworkInfo.FQDN)!"} if ($GetHypervisorCredsInfo.CurrentLoggedInUserCredsWorked -eq $True) { $HypervisorCreds = $null } $HypervisorInvCmdLocation = $GetHypervisorCredsInfo.RemoteHostWorkingLocation } catch { Write-Error $_ if ($PSBoundParameters['HypervisorCreds']) { Write-Error "The GetWorkingCredentials function failed! Check the credentials provided to the -HypervisorCreds parameter! Halting!" } else { Write-Error "The GetWorkingCredentials function failed! Try using the -HypervisorCreds parameter! Halting!" } $global:FunctionResult = "1" return } $InvCmdSplatParams = @{ ComputerName = $HypervisorInvCmdLocation ScriptBlock = $InvokeCommandSB ErrorAction = "Stop" } if ($HypervisorCreds) { $InvCmdSplatParams.Add("Credential",$HypervisorCreds) } try { $InvokeCommandOutput = Invoke-Command @InvCmdSplatParams $HypervisorComputerInfo = $InvokeCommandOutput.HypervisorComputerInfo $HypervisorOSInfo = $InvokeCommandOutput.HypervisorOSInfo $TargetVMInfoFromHyperV = $InvokeCommandOutput.TargetVMInfoFromHyperV $VMProcessorInfo = $InvokeCommandOutput.VMProcessorInfo $VMNetworkAdapterInfo = $InvokeCommandOutput.VMNetworkAdapterInfo $VMMemoryInfo = $InvokeCommandOutput.VMMemoryInfo $GuestVMIPAddresses = $InvokeCommandOutput.GuestVMIPAddresses } catch { Write-Error $_ Write-Error "The Get-GuestVMAndHypervisorInfo function was unable to gather information (i.e. `$HypervisorComputerInfo etc) about the Hyper-V host! Halting!" $global:FunctionResult = "1" return } } else { # Guest VM Info from Hypervisor Perspective try { $TargetVMInfoFromHyperV = Get-VM -Name $TargetVMName -ErrorAction Stop $VMProcessorInfo = Get-VMProcessor -VMName $TargetVMName $VMNetworkAdapterInfo = Get-VmNetworkAdapter -VmName $TargetVMName $VMMemoryInfo = Get-VMMemory -VmName $TargetVMName $HypervisorComputerInfo = Get-CimInstance Win32_ComputerSystem $HypervisorOSInfo = Get-CimInstance Win32_OperatingSystem $GuestVMIPAddresses = $TargetVMInfoFromHyperV.NetworkAdapters.IPAddresses } catch { Write-Error $_ Write-Error "The Get-GuestVMAndHypervisorInfo function was unable to gather information (i.e. `$HypervisorComputerInfo etc) about the Hyper-V host! Halting!" $global:FunctionResult = "1" return } } # Now, we need to get $HostNameOSInfo and $HostNameComputerInfo [System.Collections.ArrayList]$ResolvedIPs = @() foreach ($IPAddr in $GuestVMIPAddresses) { try { $HostNameNetworkInfoPrep = ResolveHost -HostNameOrIP $IPAddr -ErrorAction Stop $null = $ResolvedIPs.Add($HostNameNetworkInfoPrep) } catch { Write-Verbose "Unable to resolve $IPAddr" } } # If we didn't resolve any additional info beyond just IP Address, HALT if ($ResolvedIP.Count -eq 0 -or ![bool]$($ResolvedIP.HostName -match "[\w]")) { Write-Error "Unable to resolve any network information regarding $TargetVMName! Halting!" $global:FunctionResult = "1" return } if ($ResolvedIPs.Count -gt 1) { # Choose NetworkInfo that is on the same domain as our workstation $NTDomainInfo = Get-CimInstance Win32_NTDomain foreach ($ResolvedIP in $ResolvedIPs) { if ($ResolvedIP.Domain -eq $NTDomainInfo.DnsForestName) { $HostNameNetworkInfo = $ResolvedIP } } # If we still don't have one, pick one that is on the same subnet as our workstation's primary IP if (!$HostNameNetworkInfo) { $PrimaryIfIndex = $(Get-CimInstance Win32_IP4RouteTable | Where-Object { $_.Destination -eq '0.0.0.0' -and $_.Mask -eq '0.0.0.0' } | Sort-Object Metric1)[0].InterfaceIndex $NicInfo = Get-CimInstance Win32_NetworkAdapterConfiguration | Where-Object {$_.InterfaceIndex -eq $PrimaryIfIndex} $PrimaryIP = $NicInfo.IPAddress | Where-Object {TestIsValidIPAddress -IPAddress $_} $Mask = $NicInfo.IPSubnet | Where-Object {TestIsValidIPAddress -IPAddress $_} $IPRange = GetIPRange -ip $PrimaryIP -mask $Mask foreach ($ResolvedIP in $ResolvedIPs) { if ($IPRange -contains $ResolvedIP.IPAddressList[0]) { $HostNameNetworkInfo = $ResolvedIP } } } # If we still don't have one, pick one that we can reach if (!$HostNameNetworkInfo) { foreach ($ResolvedIP in $ResolvedIPs) { $Ping = [System.Net.NetworkInformation.Ping]::new() $PingResult =$Ping.Send($($ResolvedIP.IPAddressList[0]),1000) if ($PingResult.Status -eq "Success") { $HostNameNetworkInfo = $ResolvedIP } } } # If we still don't have one, just pick one if (!$HostNameNetworkInfo) { $HostNameNetworkInfo = $ResolvedIPs[0] } } else { $HostNameNetworkInfo = $ResolvedIPs[0] } # Now we need to get some info about the Guest VM $InvokeCommandSB = { [pscustomobject]@{ HostNameComputerInfo = Get-CimInstance Win32_ComputerSystem HostNameOSInfo = Get-CimInstance Win32_OperatingSystem HostNameProcessorInfo = Get-CimInstance Win32_Processor HostNameBIOSInfo = Get-CimInstance Win32_BIOS } } $GetWorkingCredsSplatParams = @{ RemoteHostNameOrIP = $HostNameNetworkInfo.FQDN ErrorAction = "Stop" } if ($TargetHostNameCreds) { $GetWorkingCredsSplatParams.Add("AltCredentials",$TargetHostNameCreds) } try { $GetTargetHostCredsInfo = GetWorkingCredentials @GetWorkingCredsSplatParams if (!$GetTargetHostCredsInfo.DeterminedCredsThatWorkedOnRemoteHost) {throw "Can't determine working credentials for $($HostNameNetworkInfo.FQDN)!"} if ($GetTargetHostCredsInfo.CurrentLoggedInUserCredsWorked -eq $True) { $TargetHostNameCreds = $null } $TargetHostInvCmdLocation = $GetTargetHostCredsInfo.RemoteHostWorkingLocation } catch { Write-Error $_ if ($PSBoundParameters['TargetHostNameCreds']) { Write-Error "The GetWorkingCredentials function failed! Check the credentials provided to the -TargetHostNameCreds parameter! Halting!" } else { Write-Error "The GetWorkingCredentials function failed! Try using the -TargetHostNameCreds parameter! Halting!" } $global:FunctionResult = "1" return } $InvCmdSplatParams = @{ ComputerName = $TargetHostInvCmdLocation ScriptBlock = $InvokeCommandSB ErrorAction = "Stop" } if ($TargetHostNameCreds) { $InvCmdSplatParams.Add("Credential",$TargetHostNameCreds) } try { $InvokeCommandOutput = Invoke-Command @InvCmdSplatParams #$HostNameVirtualStatusInfo = Get-ComputerVirtualStatus -ComputerName $HostNameNetworkInfo.FQDN -WarningAction SilentlyContinue -ErrorAction Stop $HostNameComputerInfo = $InvokeCommandOutput.HostNameComputerInfo $HostNameOSInfo = $InvokeCommandOutput.HostNameOSInfo $HostNameProcessorInfo = $InvokeCommandOutput.HostNameProcessorInfo $HostNameBIOSInfo = $InvokeCommandOutput.HostNameBIOSInfo } catch { Write-Error $_ Write-Error "The Get-GuestVMAndHypervisorInfo function was unable to gather information (i.e. `$HostNameComputerInfo, etc) about the Target Guest VM! Halting!" $global:FunctionResult = "1" return } # Now we have $HypervisorNetworkInfo, $HypervisorComputerInfo, $HypervisorOSInfo, $TargetVMInfoFromHyperV, # $HostNameNetworkInfo, $HostNameComputerInfo, $HostNameOSInfo, and $HostNameBIOSInfo } ## END $TargetVMName adjudication ## ## BEGIN $TargetHostNameOrIP adjudication ## if ($TargetHostNameOrIP) { # If $TargetHostNameOrIP is provided (as opposed to $TargetVMName), we's LIKE TO # get information from the Hyper-V Hypervisor, but it's not strictly necessary. # However, like with $TargetVMName it is still MANDATORY that we get info about # the Target Host. If we can't for whatever reason, we need to HALT # We need to be able to get Network Info about the Target Host regardless of whether or not # our workstation is actually the Target Host. So if we can't get the info, HALT try { $HostNameNetworkInfo = ResolveHost -HostNameOrIP $TargetHostNameOrIP -ErrorAction Stop } catch { Write-Error "Unable to resolve $TargetHostNameOrIP! Halting!" $global:FunctionResult = "1" return } # BEGIN Get Guest VM Info # if ($HostNameNetworkInfo.HostName -ne $env:ComputerName) { $InvokeCommandSB = { $IntegrationServicesRegistryPath = "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters" $HostNameBiosInfoSB = Get-CimInstance Win32_BIOS $HostNameComputerInfoSB = Get-CimInstance Win32_ComputerSystem $HostNameOSInfoSB = Get-CimInstance Win32_OperatingSystem $HostNameProcessorInfoSB = Get-CimInstance Win32_Processor $HostNameIntegrationServicesPresentSB = Test-Path $IntegrationServicesRegistryPath if ($HostNameIntegrationServicesPresentSB) { $HostNameGuestVMInfoSB = Get-ItemProperty $IntegrationServicesRegistryPath } else { $HostNameGuestVMInfoSB = "IntegrationServices_Not_Installed" } [pscustomobject]@{ HostNameComputerInfo = $HostNameComputerInfoSB HostNameOSInfo = $HostNameOSInfoSB HostNameProcessorInfo = $HostNameProcessorInfoSB HostNameBIOSInfo = $HostNameBiosInfoSB HostNameGuestVMInfo = $HostNameGuestVMInfoSB } } $GetWorkingCredsSplatParams = @{ RemoteHostNameOrIP = $HostNameNetworkInfo.FQDN ErrorAction = "Stop" } if ($TargetHostNameCreds) { $GetWorkingCredsSplatParams.Add("AltCredentials",$TargetHostNameCreds) } try { $GetTargetHostCredsInfo = GetWorkingCredentials @GetWorkingCredsSplatParams if (!$GetTargetHostCredsInfo.DeterminedCredsThatWorkedOnRemoteHost) {throw "Can't determine working credentials for $($HostNameNetworkInfo.FQDN)!"} if ($GetTargetHostCredsInfo.CurrentLoggedInUserCredsWorked -eq $True) { $TargetHostNameCreds = $null } $TargetHostInvCmdLocation = $GetTargetHostCredsInfo.RemoteHostWorkingLocation } catch { Write-Error $_ if ($PSBoundParameters['TargetHostNameCreds']) { Write-Error "The GetWorkingCredentials function failed! Check the credentials provided to the -TargetHostNameCreds parameter! Halting!" } else { Write-Error "The GetWorkingCredentials function failed! Try using the -TargetHostNameCreds parameter! Halting!" } $global:FunctionResult = "1" return } $InvCmdSplatParams = @{ ComputerName = $TargetHostInvCmdLocation ScriptBlock = $InvokeCommandSB ErrorAction = "Stop" } if ($TargetHostNameCreds) { $InvCmdSplatParams.Add("Credential",$TargetHostNameCreds) } try { $InvokeCommandOutput = Invoke-Command @InvCmdSplatParams #$HostNameVirtualStatusInfo = Get-ComputerVirtualStatus -ComputerName $HostNameNetworkInfo.FQDN -WarningAction SilentlyContinue -ErrorAction Stop $HostNameComputerInfo = $InvokeCommandOutput.HostNameComputerInfo $HostNameOSInfo = $InvokeCommandOutput.HostNameOSInfo $HostNameProcessorInfo = $InvokeCommandOutput.HostNameProcessorInfo $HostNameBIOSInfo = $InvokeCommandOutput.HostNameBIOSInfo $HostNameGuestVMInfo = $InvokeCommandOutput.HostNameGuestVMInfo } catch { Write-Error $_ Write-Error "The Get-GuestVMAndHypervisorInfo function was unable to gather information about $TargetHostInvCmdLocation (i.e. `$HostNameComputerInfo, etc)! Halting!" $global:FunctionResult = "1" return } } else { $IntegrationServicesRegistryPath = "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters" $HostNameBiosInfo = Get-CimInstance Win32_BIOS $HostNameIntegrationServicesPresent = Test-Path $IntegrationServicesRegistryPath if ($HostNameIntegrationServicesPresent) { $HostNameGuestVMInfo = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters" } else { $HostNameGuestVMInfo = "IntegrationServices_Not_Installed" } $HostNameComputerInfo = Get-CimInstance Win32_ComputerSystem $HostNameOSInfo = Get-CimInstance Win32_OperatingSystem $HostNameProcessorInfo = Get-CimInstance Win32_Processor $HostNameBIOSInfo = Get-CimInstance Win32_BIOS $TargetHostInvCmdLocation = $env:ComputerName } if ($HostNameBIOSInfo.SMBIOSBIOSVersion -match "Hyper-V|VirtualBox|VMWare|Xen" -or $HostNameBIOSInfo.Manufacturer -match "Hyper-V|VirtualBox|VMWare|Xen" -or $HostNameBIOSInfo.Name -match "Hyper-V|VirtualBox|VMWare|Xen" -or $HostNameBIOSInfo.SerialNumber -match "Hyper-V|VirtualBox|VMWare|Xen" -or $HostNameBIOSInfo.Version -match "Hyper-V|VirtualBox|VMWare|Xen|VRTUAL" -or $HostNameIntegrationServicesPresent) { Add-Member -InputObject $HostNameBIOSInfo NoteProperty -Name "IsVirtual" -Value $True } else { Add-Member -InputObject $HostNameBIOSInfo NoteProperty -Name "IsVirtual" -Value $False } if (!$HostNameBIOSInfo.IsVirtual) { Write-Error "This function is meant to determine if a Guest VM is capable of Nested Virtualization, but $TargetHostNameOrIP is a physical machine! Halting!" $global:FunctionResult = "1" return } if (!$($HostNameBIOSInfo.SMBIOSBIOSVersion -match "Hyper-V" -or $HostNameBIOSInfo.Name -match "Hyper-V")) { Write-Warning "The hypervisor for $($HostNameNetworkInfo.FQDN) is NOT Microsoft's Hyper-V. Unable to get additional information about the hypervisor!" $HypervisorIsHyperV = $False $TryWithoutHypervisorInfo = $True } else { $HypervisorIsHyperV = $True } # END Get Guest VM Info # # BEGIN Get Hypervisor Info # if ($HypervisorIsHyperV -and !$TryWithoutHypervisorInfo) { # Now we need to try and get some info about the hypervisor if ($HostNameGuestVMInfo -eq "IntegrationServices_Not_Installed") { # Still need the FQDN/Location of the hypervisor if (!$HypervisorFQDNOrIP -and !$TryWithoutHypervisorInfo) { while (!$HypervisorFQDNOrIP) { Write-Warning "The localhost $env:ComputerName does not appear to be a hypervisor!" $HypervisorFQDNOrIP = Read-Host -Prompt "Please enter the FQDN or IP of the hypervisor that manages $TargetHostInvCmdLocation" } } } if ($HostNameGuestVMInfo.PhysicalHostNameFullyQualified) { $HypervisorFQDNOrIPToResolve = $HostNameGuestVMInfo.PhysicalHostNameFullyQualified } elseif ($HypervisorFQDNOrIP) { $HypervisorFQDNOrIPToResolve = $HypervisorFQDNOrIP } # Now we need the FQDN of the hypervisor... try { $HypervisorNetworkInfo = ResolveHost -HostNameOrIP $HypervisorFQDNOrIPToResolve -ErrorAction Stop } catch { Write-Warning "Unable to resolve $HypervisorFQDNOrIPToResolve! Trying without hypervisor info..." $TryWithoutHypervisorInfo = $True } try { # Still need the name of the Guest VM according the the hypervisor if ($HypervisorNetworkInfo.HostName -ne $env:ComputerName) { try { $InvokeCommandSB = { # We an determine the $TargetVMName by finding the Guest VM Network Adapter with an IP that matches # $HostNameNetworkInfo.IPAddressList $TargetVMName = $(Get-VM | Where-Object {$_.NetworkAdapters.IPAddresses -contains $using:HostNameNetworkInfo.IPAddressList[0]}).Name try { $TargetVMInfoFromHyperV = Get-VM -Name $TargetVMName -ErrorAction Stop $VMProcessorInfo = Get-VMProcessor -VMName $TargetVMName $VMNetworkAdapterInfo = Get-VmNetworkAdapter -VmName $TargetVMName $VMMemoryInfo = Get-VMMemory -VmName $TargetVMName } catch { $TargetVMInfoFromHyperV = "Unable_to_find_VM" } [pscustomobject]@{ HypervisorComputerInfo = Get-CimInstance Win32_ComputerSystem HypervisorOSInfo = Get-CimInstance Win32_OperatingSystem TargetVMInfoFromHyperV = $TargetVMInfoFromHyperV VMProcessorInfo = $VMProcessorInfo VMNetworkAdapterInfo = $VMNetworkAdapterInfo VMMemoryInfo = $VMMemoryInfo } } $GetWorkingCredsSplatParams = @{ RemoteHostNameOrIP = $HypervisorNetworkInfo.FQDN ErrorAction = "Stop" } if ($HypervisorCreds) { $GetWorkingCredsSplatParams.Add("AltCredentials",$HypervisorCreds) } try { $GetHypervisorCredsInfo = GetWorkingCredentials @GetWorkingCredsSplatParams if (!$GetHypervisorCredsInfo.DeterminedCredsThatWorkedOnRemoteHost) {throw "Can't determine working credentials for $($HypervisorNetworkInfo.FQDN)!"} if ($GetHypervisorCredsInfo.CurrentLoggedInUserCredsWorked -eq $True) { $HypervisorCreds = $null } $HypervisorInvCmdLocation = $GetHypervisorCredsInfo.RemoteHostWorkingLocation } catch { if ($PSBoundParameters['HypervisorCreds']) { throw "The GetWorkingCredentials function failed! Check the credentials provided to the -HypervisorCreds parameter! Halting!" } else { throw "The GetWorkingCredentials function failed! Try using the -HypervisorCreds parameter! Halting!" } } $InvCmdSplatParams = @{ ComputerName = $HypervisorInvCmdLocation ScriptBlock = $InvokeCommandSB ErrorAction = "Stop" } if ($HypervisorCreds) { $InvCmdSplatParams.Add("Credential",$HypervisorCreds) } try { $InvokeCommandOutput = Invoke-Command @InvCmdSplatParams $HypervisorComputerInfo = $InvokeCommandOutput.HypervisorComputerInfo $HypervisorOSInfo = $InvokeCommandOutput.HypervisorOSInfo $TargetVMInfoFromHyperV = $InvokeCommandOutput.TargetVMInfoFromHyperV $VMProcessorInfo = $InvokeCommandOutput.VMProcessorInfo $VMNetworkAdapterInfo = $InvokeCommandOutput.VMNetworkAdapterInfo $VMMemoryInfo = $InvokeCommandOutput.VMMemoryInfo } catch { throw "The Get-GuestVMAndHypervisorInfo function was not able to gather information (i.e. `$HypervisorComputerInfo, etc) about the Hyper-V Host and the Target Guest VM by remoting into the Hyper-V Host! Halting!" } } catch { if (!$TryWithoutHypervisorInfo) { Write-Error $_ Write-Error "Unable to get Hyper-V Hypervisor info! Halting!" $global:FunctionResult = "1" return } else { Write-Verbose "Unabel to get info about the hypervisor. Trying without hypervisor info..." } } } else { # We an determine the $TargetVMName by finding the Guest VM Network Adapter with an IP that matches # $HostNameNetworkInfo.IPAddressList $TargetVMName = $(Get-VMNetworkAdapter -All | Where-Object {$_.IPAddresses -contains $HostNameNetworkInfo.IPAddressList[0]}).VMName #$TargetVMName = $(Get-VM | Where-Object {$_.NetworkAdapters.IPAddresses -contains $HostNameNetworkInfo.IPAddressList[0]}).Name try { $TargetVMInfoFromHyperV = Get-VM -Name $TargetVMName -ErrorAction Stop $VMProcessorInfo = Get-VMProcessor -VMName $TargetVMName $VMNetworkAdapterInfo = Get-VmNetworkAdapter -VmName $TargetVMName $VMMemoryInfo = Get-VMMemory -VmName $TargetVMName } catch { $TargetVMInfoFromHyperV = "Unable_to_find_VM" } $HypervisorComputerInfo = Get-CimInstance Win32_ComputerSystem $HypervisorOSInfo = Get-CimInstance Win32_OperatingSystem } } catch { if (!$TryWithoutHypervisorInfo) { Write-Error $_ Write-Error "Unable to get Hyper-V Hypervisor info! Halting!" $global:FunctionResult = "1" return } else { Write-Verbose "Unable to get info about the hypervisor. Trying without hypervisor info..." } } if ($TargetVMInfoFromHyperV -eq "Unable_to_find_VM") { Write-Error "Unable to find VM $TargetVMName on the specified hypervisor $($HypervisorNetworkInfo.FQDN)! Halting!" $global:FunctionResult = "1" return } } elseif (!$HypervisorIsHyperV) { $TryWithoutHypervisorInfo = $True } elseif ((!$HypervisorIsHyperV -and !$TryWithoutHypervisorInfo)) { $ErrMsg = "Unable to get info about $($HostNameNetworkInfo.FQDN) from the hypervisor! " + "If you would like to try without hypervisor information, use the -TryWithoutHypervisorInfo switch. Halting!" Write-Error $ErrMsg $global:FunctionResult = "1" return } # Now we have $HypervisorNetworkInfo, $HypervisorComputerInfo, $HypervisorOSInfo, $TargetVMInfoFromHyperV, # $HostNameGuestVMInfo, $HostNameNetworkInfo, $HostNameComputerInfo, and $HostNameOSInfo, $HostNameBIOSInfo, # and $HostNameProcessorInfo # End Get Hypervisor Info # } ## END $TargetHostNameOrIP adjudication ## # NOTE: $TryWithoutHypervisorInfo should never be $True if -TargetVMName parameter is used if ($($TryWithoutHypervisorInfo -or $NoMacAddressSpoofing) -and !$Hypervisorcreds -and !$GuestVMAndHVInfo.HypervisorCreds -and !$SkipExternalvSwitchCheck ) { if ($HostNameNetworkInfo.HostName -ne $env:ComputerName) { $InvokeCommandSB = {Get-Module -ListAvailable -Name Hyper-V} $InvCmdSplatParams = @{ ComputerName = $TargetHostInvCmdLocation ScriptBlock = $InvokeCommandSB ErrorAction = "SilentlyContinue" } if ($TargetHostNameCreds) { $InvCmdSplatParams.Add("Credential",$TargetHostNameCreds) } $HyperVInstallCheck = [bool]$(Invoke-Command @InvCmdSplatParams) $FunctionsForRemoteUse = @( ${Function:GetElevation}.Ast.Extent.Text ${Function:TestIsValidIPAddress}.Ast.Extent.Text ${Function:Get-VagrantBoxManualDownload}.Ast.Extent.Text ${Function:NewUniqueString}.Ast.Extent.Text ${Function:GetvSwitchAllRelatedInfo}.Ast.Extent.Text ${Function:FixNTVirtualMachinesPerms}.Ast.Extent.Text ${Function:Manage-HyperVVM}.Ast.Extent.Text ${Function:Deploy-HyperVVagrantBoxManually}.Ast.Extent.Text ${Function:FixVagrantPrivateKeyPerms}.Ast.Extent.Text ${Function:InstallFeatureDism}.Ast.Extent.Text ${Function:InstallHyperVFeatures}.Ast.Extent.Text ${Function:TestHyperVExternalvSwitch}.Ast.Extent.Text 'Install-Module ProgramManagement; Import-Module ProgramManagement' ${Function:GetPendingReboot}.Ast.Extent.Text ) $InvokeCommandSB = { # Load the functions we packed up: $using:FunctionsForRemoteUse | foreach { Invoke-Expression $_ } # Check for pre-existing PendingReboot if ($PSVersionTable.PSEdition -eq "Core") { $GetPendingRebootAsString = ${Function:GetPendingReboot}.Ast.Extent.Text $RebootPendingCheck = Invoke-WinCommand -ComputerName localhost -ScriptBlock { Invoke-Expression $args[0] $(GetPendingReboot).RebootPending } -ArgumentList $GetPendingRebootAsString $RebootPendingFileCheck = Invoke-WinCommand -ComputerName localhost -ScriptBlock { Invoke-Expression $args[0] $(GetPendingReboot).PendFileRenVal } -ArgumentList $GetPendingRebootAsString } else { $RebootPendingCheck = $(GetPendingReboot).RebootPending $RebootPendingFileCheck = $(GetPendingReboot).PendFileRenVal } if (!$RebootPendingCheck -or $RebootPendingFileCheck -eq $null) { $TestHyperVExternalSwitchSplatParams = @{} if ($using:SkipHyperVInstallCheck) { $TestHyperVExternalSwitchSplatParams.Add("SkipHyperVInstallCheck",$True) } if ($using:AllowRestarts) { $TestHyperVExternalSwitchSplatParams.Add("AllowRestarts",$True) } $TestHyperVExternalSwitchResults = TestHyperVExternalvSwitch @TestHyperVExternalSwitchSplatParams $TestHyperVExternalSwitchResults } else { [pscustomobject]@{RestartNeeded = $True} return } } $InvCmdSplatParams = @{ ComputerName = $TargetHostInvCmdLocation ScriptBlock = $InvokeCommandSB ErrorAction = "SilentlyContinue" } if ($TargetHostNameCreds) { $InvCmdSplatParams.Add("Credential",$TargetHostNameCreds) } try { $TestHyperVExternalSwitchResults = Invoke-Command @InvCmdSplatParams -ErrorAction SilentlyContinue -ErrorVariable ICTHVSErr if (!$TestHyperVExternalSwitchResults) {throw "The Invoke-Command TestHyperVExternalvSwitch function failed!"} } catch { Write-Error $_ if ($($ICTHVSErr | Out-String) -match "The operation has timed out") { Write-Error "There was a problem downloading the Vagrant Box to be used to test the External vSwitch! Halting!" $global:FunctionResult = "1" return } else { Write-Host "Errors for Invoke-Command TestHyperVExternalvSwitch function are as follows:" Write-Error $($ICTHVSErr | Out-String) $global:FunctionResult = "1" return } } if ($TestHyperVExternalSwitchResults -eq "RestartNeeded" -or $TestHyperVExternalSwitchResults.RestartNeeded) { Write-Warning "You must restart $TargetHostInvCmdLocation before the Get-GuestVMAndHypervisorInfo function can proceed! Halting!" [pscustomobject]@{RestartNeeded = $True} return } } else { $HyperVInstallCheck = [bool]$(Get-Module -ListAvailable -Name Hyper-V) try { # Check for pre-existing PendingReboot if ($PSVersionTable.PSEdition -eq "Core") { $GetPendingRebootAsString = ${Function:GetPendingReboot}.Ast.Extent.Text $RebootPendingCheck = Invoke-WinCommand -ComputerName localhost -ScriptBlock { Invoke-Expression $args[0] $(GetPendingReboot).RebootPending } -ArgumentList $GetPendingRebootAsString $RebootPendingFileCheck = Invoke-WinCommand -ComputerName localhost -ScriptBlock { Invoke-Expression $args[0] $(GetPendingReboot).PendFileRenVal } -ArgumentList $GetPendingRebootAsString } else { $RebootPendingCheck = $(GetPendingReboot).RebootPending $RebootPendingFileCheck = $(GetPendingReboot).PendFileRenVal } if (!$RebootPendingCheck -or $RebootPendingFileCheck -eq $null) { # Use the TestHyperVExternalvSwitch function here $TestHyperVExternalSwitchSplatParams = @{ ErrorAction = "SilentlyContinue" ErrorVariable = "THVSErr" } if ($HyperVInstallCheck) { $TestHyperVExternalSwitchSplatParams.Add("SkipHyperVInstallCheck",$True) } $TestHyperVExternalSwitchResults = TestHyperVExternalvSwitch @TestHyperVExternalSwitchSplatParams if (!$TestHyperVExternalSwitchResults) {throw "The TestHyperVExternalvSwitch function failed!"} if ($TestHyperVExternalSwitchResults -eq "RestartNeeded") { Write-Warning "You must restart $env:ComputerName before the Get-GuestVMAndHypervisorInfo function can proceed! Halting!" if ($AllowRestarts) { Restart-Computer -Confirm:$False -Force } [pscustomobject]@{RestartNeeded = $True} return } } else { Write-Warning "There is currently a reboot pending. Please restart $env:ComputerName before proceeding. Halting!" [pscustomobject]@{RestartNeeded = $True} return } } catch { Write-Error $_ if ($($THVSErr | Out-String) -match "The operation has timed out") { Write-Error "There was a problem downloading the Vagrant Box to be used to test the External vSwitch! Halting!" $global:FunctionResult = "1" return } else { Write-Host "Errors for the TestHyperVExternalvSwitch function are as follows:" Write-Error $($THVSErr | Out-String) $global:FunctionResult = "1" return } } } if ($TestHyperVExternalSwitchResults -eq $null) { Write-Error "There was a problem with the TestHyperVExternalvSwitch function! This usually has to do with AWS or Vagrant websites throttling traffic. Try starting a fresh PowerShell Session. Halting!" $global:FunctionResult = "1" return } } if ($HypervisorCreds -eq $null -and $GetHypervisorCredsInfo.DeterminedCredsThatWorkedOnRemoteHost) { $FinalHypervisorCreds = whoami } else { $FinalHypervisorCreds = $HypervisorCreds } $Output = [ordered]@{ HypervisorNetworkInfo = $HypervisorNetworkInfo HypervisorInvCmdLocation = $HypervisorInvCmdLocation HypervisorComputerInfo = $HypervisorComputerInfo HypervisorOSInfo = $HypervisorOSInfo TargetVMInfoFromHyperV = $TargetVMInfoFromHyperV VMProcessorInfo = $VMProcessorInfo VMNetworkAdapterInfo = $VMNetworkAdapterInfo VMMemoryInfo = $VMMemoryInfo HypervisorCreds = $FinalHypervisorCreds HostNameNetworkInfo = $HostNameNetworkInfo TargetHostInvCmdLocation = $TargetHostInvCmdLocation HostNameComputerInfo = $HostNameComputerInfo HostNameOSInfo = $HostNameOSInfo HostNameProcessorInfo = $HostNameProcessorInfo HostNameBIOSInfo = $HostNameBIOSInfo TargetHostNameCreds = if ($TargetHostNameCreds -eq $null) {whoami} else {$TargetHostNameCreds} } if ($TryWithoutHypervisorInfo) { if ($HyperVInstallCheck -eq $False) { $RestartNeeded = $True if ($AllowRestarts) { $RestartOccurred = $True } else { $RestartOccurred = $False } } else { $RestartNeeded = $False $RestartOccurred = $False } $Output.Add("RestartNeeded",$RestartNeeded) $Output.Add("RestartOccurred",$RestartOccurred) if (!$SkipExternalvSwitchCheck) { if ($TestHyperVExternalSwitchResults.VirtualizationExtensionsExposed -eq $null) { $Output.Add("VirtualizationExtensionsExposed",$False) } else { $Output.Add("VirtualizationExtensionsExposed",$TestHyperVExternalSwitchResults.VirtualizationExtensionsExposed) } if ($TestHyperVExternalSwitchResults.MacAddressSpoofingEnabled -eq $null) { $Output.Add("MacAddressSpoofingEnabled","Unknown") } else { $Output.Add("MacAddressSpoofingEnabled",$TestHyperVExternalSwitchResults.MacAddressSpoofingEnabled) } } else { if ($(Confirm-AWSVM -EA SilentlyContinue) -or $(Confirm-AzureVM -EA SilentlyContinue) -or $(Confirm-GoogleComputeVM -EA SilentlyContinue) ) { $Output.Add("VirtualizationExtensionsExposed",$True) $Output.Add("MacAddressSpoofingEnabled",$False) } else { # No other choice but to assume both are $True... $Output.Add("VirtualizationExtensionsExposed",$True) $Output.Add("MacAddressSpoofingEnabled",$True) } } } else { $Output.Add("VirtualizationExtensionsExposed",$VMProcessorInfo.ExposeVirtualizationExtensions) $Output.Add("MacAddressSpoofingEnabled",$($VMNetworkAdapterInfo.MacAddressSpoofing -contains "On")) } [pscustomobject]$Output } |