Public/Install-Docker.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 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 |
<#
.SYNOPSIS This function adds an IP or hostname/fqdn to "WSMan:\localhost\Client\TrustedHosts". It also ensures that the WSMan Client is configured to allow for remoting. .DESCRIPTION See .SYNOPSIS .NOTES .PARAMETER InstallEnvironment This parameter is OPTIONAL (but highly recommended). This parameter takes a string with either the value "BareMetal" or "GuestVM". Use 'BareMetal' if you are installing Docker on a baremetal machine. Use "GuestVM" if you are installing Docker on a Guest VM. If you do not use this parameter, the function will attempt to determine if the target machine is baremetal or if it is a Guest VM, however, the logic it uses to make this determination is not bulletproof, which is why this parameter should be used if at all possible. .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 install Docker-For-Windows (Docker CE) on. If it is NOT used (and if the -TargetVMName parameter is not used), the function will assume that you would like to install Docker on the localhost. .PARAMETER TargetVMName This parameter is MANDATORY (for its parameter set). This parameter should only be used if Docker is being installed on a Guest VM. This parameter takes a string that represents the name of the Hyper-V VM that you would like to install Docker on. 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 should only be used if Docker is being installed on a Guest VM. 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 install Docker on. If you are installing Docker on a Guest VM and 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 Remote Host that you would like to install Docker on. If you are installing Docker on the localhost, or if you are logged in as a user that already has access to the target machine then you should NOT use this parameter. .PARAMETER HypervisorCreds This parameter is OPTIONAL. This parameter should only be used if Docker is being installed on a Guest VM. 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 install Docker on. 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 GuestVMandHVInfo This parameter is OPTIONAL. This parameter should only be used if Docker is being installed on a Guest VM. This parameter takes a pscustomobject that represents that output of the Get-GuestVMandHypervisorInfo function. If you intend to install Docker on a Guest VM, you can use this parameter to forego using the following parameters: -InstallEnvironment -TargetHostNameIP -TargetVMName -HypervisorFQDNOrIP -TargetHostNameCreds -HypervisorCreds .PARAMETER GuestVMMemoryInGB This parameter is OPTIONAL. This parameter should only be used if Docker is being installed on a Guest VM. This parameter takes an integer that represents the amount of memory (in GB) that you would like the Guest VM (that you are installing Docker on) to have. Using this parameter assumes you can access to the Hyper-V hypervisor that is managing the target Guest VM (whether it is via the -HypervisorCreds parameter on because you are logged into the localhost as a user that has access to the hypervisor). .PARAMETER AllowRestarts This parameter is OPTIONAL. This parameter is a switch. If used, if the target machine needs to be restarted as a result of the Docker install (most likely because Hyper-V was installed for the first time), then it will be restarted. .PARAMETER MobyLinuxVMMemoryInGB This parameter is OPTIONAL, however, it has a default value of 2. This parameter takes an integer (even numbers only) that represents the amount of Memory in GB that you would like to allocate to the MobyLinux VM that is created to run Linux Containers on Docker-For-Windows (Docker CE). .PARAMETER TryWithoutHypervisorInfo This parameter is OPTIONAL. This parameter should only be used if Docker is being installed on a Guest VM. This parameter is a switch. If used, then this function will attempt to install Docker on the target Guest VM without gathering any information about the Hyper-V hypervisor that is managing the Guest VM. Use this parameter if you do not have access to the hypervisor managing the target Guest VM. .PARAMETER NoMacAddressSpoofing This parameter is OPTIONAL. This parameter should only be used if Docker is being installed on a Guest VM. 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 machine and not attempt any verification or installation prior to installing Docker. .PARAMETER SkipEnableNestedVM This parameter is OPTIONAL. This parameter should only be used if Docker is being installed on a Guest VM. This parameter is a switch. If used, this function will NOT attempt to make any configuration changes to the Hyper-V hypervisor or target Guest VM in order to make Nested Virtualization possible. .PARAMETER RecreateMobyLinuxVM This parameter is OPTIONAL. This parameter is a switch. If Docker was installed at one point but has since been uninstalled, and the MobyLinux VM from the previous installation still exists, use this parameter to recreate it for use with this new Docker installation. .PARAMETER NATIP This parameter is OPTIONAL. This parameter takes a string that represents the IPv4 addess that you would like the DocketNAT vSwitch to use. It defaults to 10.0.75.1. .PARAMETER NATNetworkMask This parameter is OPTIONAL. This parameter takes an integer from 24 to 31 inclusive that represents the CIDR notation of the network mask that you would like to use for the DockerNAT. This parameter must be used in conjunction with the -NATIP parameter. .PARAMETER NATName This parameter is OPTIONAL. This parameter takes a string that represents the name of the Hyper-V vSwitch that will be used for DockerNAT. This parameter must be used in conjunction with the -NATIP and -NATNetworkMask parameters. .PARAMETER PreRelease This parameter is OPTIONAL. This parameter is a switch. If used, Docker-For-Windows (Docker CE) will be installed from the Edge (as opposed to Stable) branch. .PARAMETER AllowLogout This parameter is OPTIONAL. This parameter is a switch. If used, and if Docker is being installed on the localhost in an interactive PowerShell session, then the logged in user will be logged out at the end of installation (which is required as a normal part of installing Docker-For-Windows/Docker CE). .EXAMPLE # Open an elevated PowerShell Session, import the module, and - PS C:\Users\zeroadmin> Install-Docker #> function Install-Docker { [CmdletBinding(DefaultParameterSetName='Default')] Param( [Parameter(Mandatory = $False)] [ValidateSet("BareMetal","GuestVM")] [string]$InstallEnvironment, [Parameter( Mandatory = $False, ParameterSetName = 'Default' )] [string]$TargetHostNameOrIP, [Parameter( Mandatory=$False, ParameterSetName = 'UsingVMName' )] [string]$TargetVMName, [Parameter(Mandatory=$False)] [string]$HypervisorFQDNOrIP, [Parameter(Mandatory=$False)] $TargetHostNameCreds, [Parameter(Mandatory=$False)] $HypervisorCreds, [Parameter( Mandatory=$True, ParameterSetName = 'InfoAlreadyCollected' )] $GuestVMAndHVInfo, [Parameter(Mandatory=$False)] [ValidateScript({ $(($_ % 4) -eq 0) -and $($_ -ge 4) })] [int]$GuestVMMemoryInGB, # Use this parameter if you are installing Docker on a GuestVM and want to increase that GuestVM's memory [Parameter(Mandatory=$False)] [switch]$AllowRestarts, [Parameter(Mandatory=$False)] [ValidateScript({ $(($_ % 2) -eq 0) -and $($_ -ge 2) })] [int]$MobyLinuxVMMemoryInGB = 2, [Parameter(Mandatory=$False)] [switch]$TryWithoutHypervisorInfo, # -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]$SkipEnableNestedVM, [Parameter(Mandatory=$False)] [switch]$RecreateMobyLinuxVM, [Parameter(Mandatory=$False)] [string]$NATIP, [Parameter(Mandatory=$False)] [ValidateRange(24,31)] [string]$NATNetworkMask, [Parameter(Mandatory=$False)] [string]$NATName, [Parameter(Mandatory=$False)] [switch]$PreRelease, [Parameter(Mandatory=$False)] [switch]$AllowLogout ) ##### BEGIN Variable/Parameter Transforms and PreRun Prep ##### 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 (!$GuestVMAndHVInfo) { if (!$TargetHostNameOrIP -and !$TargetVMName) { $TargetHostNameOrIP = $env:ComputerName } try { $HostNameNetworkInfo = ResolveHost -HostNameOrIP $TargetHostNameOrIP } catch { Write-Error $_ $global:FunctionResult = "1" return } $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 } } # Determine the $InstallEnvironment (either "BareMetal" or "GuestVM") if (!$InstallEnvironment) { if ($GuestVMAndHVInfo -or $TargetVMName) { $InstallEnvironment = "GuestVM" } if (!$GuestVMAndHVInfo -and !$TargetVMName) { if ($TargetHostInvCmdLocation -match $env:ComputerName) { $IntegrationServicesRegistryPath = "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters" $HostNameBiosInfo = Get-CimInstance Win32_BIOS $HostNameIntegrationServicesPresent = Test-Path $IntegrationServicesRegistryPath if ($HostNameIntegrationServicesPresent) { $HostNameGuestVMInfo = Get-ItemProperty $IntegrationServicesRegistryPath } } else { # Determine if $TargetHostNameOrIP is Physical or Virtual try { $EnvProbeSB = { $IntegrationServicesRegistryPath = "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters" $HostNameBiosInfoSB = Get-CimInstance Win32_BIOS $HostNameIntegrationServicesPresentSB = Test-Path $IntegrationServicesRegistryPath $Output = [ordered]@{ HostNameBIOSInfo = $HostNameBiosInfoSB HostNameIntegrationServicesPresent = $HostNameIntegrationServicesPresentSB } if ($HostNameIntegrationServicesPresentSB) { $HostNameGuestVMInfoSB = Get-ItemProperty $IntegrationServicesRegistryPath $Output.Add("HostNameGuestVMInfo",$HostNameGuestVMInfoSB) } [pscustomobject]$Output } $EnvProbeSplatParams = @{ ComputerName = $TargetHostInvCmdLocation ScriptBlock = $EnvProbeSB ErrorAction = "Stop" } if ($TargetHostNameCreds -ne $null) { $EnvProbeSplatParams.Add("Credential",$TargetHostNameCreds) } try { $InvokeCommandOutput = Invoke-Command @EnvProbeSplatParams $HostNameBiosInfo = $InvokeCommandOutput.HostNameBIOSInfo $HostNameIntegrationServicesPresent = $InvokeCommandOutput.HostNameIntegrationServicesPresent if ($HostNameIntegrationServicesPresent) { $HostNameGuestVMInfo = $InvokeCommandOutput.HostNameGuestVMInfo } } catch { Write-Error $_ Write-Error "Probing the Target Machine failed to determine if it is Physical or Virtual failed! Halting!" $global:FunctionResult = "1" return } } catch { Write-Error $_ Write-Error "Unable to get `$HostNameBIOSInfo or `$HostnameIntegrationServicesPresent! Halting!" $global:FunctionResult = "1" return } } 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 -eq $True ) { Add-Member -InputObject $HostNameBIOSInfo NoteProperty -Name "IsVirtual" -Value $True } else { Add-Member -InputObject $HostNameBIOSInfo NoteProperty -Name "IsVirtual" -Value $False } if ($HostNameBIOSInfo.IsVirtual) { $InstallEnvironment = "GuestVM" } else { $InstallEnvironment = "BareMetal" } } } if ($InstallEnvironment -eq "BareMetal" -and $GuestVMMemoryInGB) { $GuestVMMemErrMsg = "The -GuestVMMemoryInGB parameter should only be used if Docker is going to be installed" + "on a Guest VM and you would like to increase the amount of memory available to that Guest VM! Halting!" Write-Error $GuestVMMemErrMsg $global:FunctionResult = "1" return } if ([bool]$PSBoundParameters['MobyLinuxVMMemoryInGB']) { $MobyLinuxVMMemoryInMB = [Math]::Round($MobyLinuxVMMemoryInGB * 1KB) } # Constants #$DockerToolsUrl = "https://download.docker.com/win/stable/DockerToolbox.exe" #$DockerCEUrl = "https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.exe" # Gather Information about the target install environment. If it's a GuestVM, then changes will be made # to the Hyper-V hypervisor and Guest VM as needed to allow for Nested Virtualization, which may or may not # involve restarting the Guest VM. Otherwise, these 'if' blocks are just responsible for defining # $Locale, i.e. the machine that is running this function - either the "Hypervisor", "GuestVM", # "BareMetalTarget", or "Elsewhere" if ($InstallEnvironment -eq "GuestVM") { # We might need credentials for the hypervisor... if ($HostNameGuestVMInfo) { $HyperVLocationToResolve = $HostNameGuestVMInfo.PhysicalHostNameFullyQualified } elseif ($HypervisorFQDNOrIP) { $HyperVLocationToResolve = $HypervisorFQDNOrIP } else { $HyperVLocationToResolve = Read-Host -Prompt "Please enter the IP, FQDN, or DNS-Resolvable hostname of the Hyper-V machine hosting the Guest VM." } try { try { $HypervisorNetworkInfo = ResolveHost -HostNameOrIP $HyperVLocationToResolve -ErrorAction Stop } catch { throw "Unable to resolve $HyperVLocationToResolve! Halting!" } $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!" } } } catch { if ($TryWithoutHypervisorInfo) { $HypervisorNetworkInfo = $null $HypervisorComputerInfo = $null $HypervisorOSInfo = $null } else { Write-Error $_ Write-Error "Unable to get Hyper-V Hypervisor info! Halting!" $global:FunctionResult = "1" return } } if (![bool]$PSBoundParameters['GuestVMAndHVInfo']) { $GetVMAndHVSplatParams = @{} if ($($TargetHostNameOrIP -or $TargetHostInvCmdLocation) -and $GetVMAndHVSplatParams.Keys -notcontains "TargetHostNameOrIP") { if ($TargetHostInvCmdLocation) { $GetVMAndHVSplatParams.Add("TargetHostNameOrIP",$TargetHostInvCmdLocation) } elseif ($TargetHostNameOrIP) { $GetVMAndHVSplatParams.Add("TargetHostNameOrIP",$TargetHostNameOrIP) } } elseif ($TargetVMName -and $GetVMAndHVSplatParams.Keys -notcontains "TargetVMName") { $GetVMAndHVSplatParams.Add("TargetVMName",$TargetVMName) } if ($TargetHostNameCreds -and $GetVMAndHVSplatParams.Keys -notcontains "TargetHostNameCreds") { $GetVMAndHVSplatParams.Add("TargetHostNameCreds",$TargetHostNameCreds) } if ($($HypervisorFQDNOrIP -or $HypervisorInvCmdLocation) -and $GetVMAndHVSplatParams.Keys -notcontains "HypervisorFQDNOrIP") { if ($HypervisorInvCmdLocation) { $GetVMAndHVSplatParams.Add("HypervisorFQDNOrIP",$HypervisorInvCmdLocation) } elseif ($HypervisorFQDNOrIP) { $GetVMAndHVSplatParams.Add("HypervisorFQDNOrIP",$HypervisorFQDNOrIP) } } if ($HypervisorCreds -and $GetVMAndHVSplatParams.Keys -notcontains "HypervisorCreds") { $GetVMAndHVSplatParams.Add("HypervisorCreds",$HypervisorCreds) } if ($($TryWithoutHypervisorInfo -and $GetVMAndHVSplatParams.Keys -notcontains "TryWithoutHypervisorInfo") -or $($(ConfirmAWSVM -EA SilentlyContinue) -or $(ConfirmAzureVM -EA SilentlyContinue) -or $(ConfirmGoogleComputeVM -EA SilentlyContinue)) ) { $GetVMAndHVSplatParams.Add("TryWithoutHypervisorInfo",$True) } if ($AllowRestarts -and $GetVMAndHVSplatParams.Keys -notcontains "AllowRestarts") { $GetVMAndHVSplatParams.Add("AllowRestarts",$True) } if ($NoMacAddressSpoofing -and $GetVMAndHVSplatParams.Keys -notcontains "NoMacAddressSpoofing") { $GetVMAndHVSplatParams.Add("NoMacAddressSpoofing",$True) } if ($SkipHyperVInstallCheck -and $GetVMAndHVSplatParams.Keys -notcontains "SkipHyperVInstallCheck") { $GetVMAndHVSplatParams.Add("SkipHyperVInstallCheck",$True) } if ($SkipExternalvSwitchCheck -and $GetVMAndHVSplatParams.Keys -notcontains "SkipExternalvSwitchCheck") { $GetVMAndHVSplatParams.Add("SkipExternalvSwitchCheck",$True) } try { $GuestVMAndHVInfo = Get-GuestVMAndHypervisorInfo @GetVMAndHVSplatParams -ErrorAction SilentlyContinue -ErrorVariable GGIErr if (!$GuestVMAndHVInfo) {throw "The Get-GuestVMAndHypervisorInfo function failed! Halting!"} 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 ($GuestVMAndHVInfo.RestartNeeded -and !$AllowRestarts) { Write-Verbose "A restart might be necessary before Docker CE can be instelled on $env:ComputerName..." } } catch { Write-Error $_ if ($($GGIErr | 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 Get-GuestVMAndHypervisorInfo function are as follows:" Write-Error $($GGIErr | Out-String) $global:FunctionResult = "1" return } } } else { $ValidGuestVMAndHVInfoNoteProperties = @( "HypervisorNetworkInfo" "HypervisorInvCmdLocation" "HypervisorComputerInfo" "HypervisorOSInfo" "TargetVMInfoFromHyperV" "VMProcessorInfo" "VMNetworkAdapterInfo" "VMMemoryInfo" "HypervisorCreds" "HostNameNetworkInfo" "TargetHostInvCmdLocation" "HostNameComputerInfo" "HostNameOSInfo" "HostNameProcessorInfo" "HostNameBIOSInfo" "TargetHostNameCreds" "RestartNeeded" "RestartOccurred" "VirtualizationExtensionsExposed" "MacAddressSpoofingEnabled" ) [System.Collections.ArrayList]$FoundIssueWithGuestVMAndHVInfo = @() $ParamObjMembers = $($GuestVMAndHVInfo | Get-Member -MemberType NoteProperty).Name foreach ($noteProp in $ParamObjMembers) { if ($ValidGuestVMAndHVInfoNoteProperties -notcontains $noteProp) { $null = $FoundIssueWithGuestVMAndHVInfo.Add($noteProp) } } if ($FoundIssueWithGuestVMAndHVInfo.Count -gt 3) { $ParamObjMembers Write-Error "The object provided to the -GuestVMAndHVInfo parameter is invalid! It must be output from the Get-GuestVMAndHypervisorInfo function! Halting!" $global:FunctionResult = "1" return } } if (!$GuestVMAndHVInfo) { Write-Error "There was a problem with the Get-GuestVMandHypervisorInfo function! Halting!" $global:FunctionResult = "1" return } if ($GuestVMAndHVInfo.RestartOccurred -eq $True) { Write-Error "$($GuestVMAndHVInfo.HostNameNetworkInfo.FQDN) was restarted! Please re-run the Install-Docker function after $($GuestVMAndHVInfo.FQDN) has finished restarting (approximately 5 minutes)." $global:FunctionResult = "1" return } if ($GuestVMAndHVInfo.HypervisorCreds -ne $null) { $HypervisorCreds = $GuestVMAndHVInfo.HypervisorCreds } if ($GuestVMAndHVInfo.TargetHostNameCreds -ne $null) { $TargetHostNameCreds = $GuestVMAndHVInfo.TargetHostNameCreds if ($TargetHostNameCreds -eq $(whoami)) { $TargetHostNameCreds = $null } } if ($($GuestVMAndHVInfo.VirtualizationExtensionsExposed -eq $False -or $GuestVMAndHVInfo.VirtualizationExtensionsExposed -match "Unknown") -and $TryWithoutHypervisorInfo) { Write-Error "The Guest VM $($GuestVMAndHVInfo.HostNameNetworkInfo.FQDN) is not capable of running 64-bit Nested VMs! Since DockerCE depends on the MobyLinux VM, DockerCE will NOT be installed! Try the EnableNestedVM function for remediation. Halting!" $global:FunctionResult = "1" return } # Determine where this function is being run if ($env:ComputerName -eq $GuestVMAndHVInfo.HypervisorNetworkInfo.HostName) { $Locale = "Hypervisor" } elseif ($env:ComputerName -eq $GuestVMAndHVInfo.HostNameNetworkInfo.HostName) { $Locale = "GuestVM" } else { $Locale = "Elsewhere" } if (!$SkipEnableNestedVM) { $EnableNestedVMSplatParams = @{ GuestVMAndHVInfo = $GuestVMAndHVInfo SkipPrompt = $True ErrorAction = "SilentlyContinue" ErrorVariable = "ENVErr" WarningAction = "SilentlyContinue" } if ($AllowRestarts) { if ($env:ComputerName -eq $GuestVMAndHVInfo.HostNameNetworkInfo.HostName) { $DefaultRestartWarning = "If any restarts occur, Vagrant will NOT have been installed " + "(only prerequisites will have been configured). To complete Vagrant installation after restart, " + "you must re-run the Install-Vagrant function AFTER restarting!" Write-Warning $DefaultRestartWarning } $EnableNestedVMSplatParams.Add("AllowRestarts",$True) } if ($TryWithoutHypervisorInfo) { $EnableNestedVMSplatParams.Add("TryWithoutHypervisorInfo",$True) } if ($NoMacAddressSpoofing) { $EnableNestedVMSplatParams.Add("NoMacAddressSpoofing",$True) } if ($SkipHyperVInstallCheck) { $EnableNestedVMSplatParams.Add("SkipHyperVInstallCheck",$True) } if ($NATIP) { $EnableNestedVMSplatParams.Add("NATIP",$NATIP) } if ($NATNetworkMask) { $EnableNestedVMSplatParams.Add("NATNetworkMask",$NATNetworkMask) } if ($NATName) { $EnableNestedVMSplatParams.Add("NATName",$NATName) } try { $EnableNestedVMResults = EnableNestedVM @EnableNestedVMSplatParams if (!$EnableNestedVMResults) {throw "The EnableNestedVM function failed! Halting!"} } catch { Write-Error $_ Write-Host "Errors for the EnableNestedVM function are as follows:" Write-Error $($ENVErr | Out-String) $global:FunctionResult = "1" return } if ($EnableNestedVMResults.RestartOccurred) { if ($EnableNestedVMResults.GuestVMSettingsThatWereChanged -contains "HyperVInstall") { Write-Host "Sleeping for 300 seconds to wait for restart after Hyper-V install..." Start-Sleep -Seconds 300 $GetVMAndHVSplatParams = @{} if ($($TargetHostNameOrIP -or $TargetHostInvCmdLocation) -and $GetVMAndHVSplatParams.Keys -notcontains "TargetHostNameOrIP") { if ($TargetHostInvCmdLocation) { $GetVMAndHVSplatParams.Add("TargetHostNameOrIP",$TargetHostInvCmdLocation) } elseif ($TargetHostNameOrIP) { $GetVMAndHVSplatParams.Add("TargetHostNameOrIP",$TargetHostNameOrIP) } } elseif ($TargetVMName -and $GetVMAndHVSplatParams.Keys -notcontains "TargetVMName") { $GetVMAndHVSplatParams.Add("TargetVMName",$TargetVMName) } if ($TargetHostNameCreds -and $GetVMAndHVSplatParams.Keys -notcontains "TargetHostNameCreds") { $GetVMAndHVSplatParams.Add("TargetHostNameCreds",$TargetHostNameCreds) } if ($($HypervisorFQDNOrIP -or $HypervisorInvCmdLocation) -and $GetVMAndHVSplatParams.Keys -notcontains "HypervisorFQDNOrIP") { if ($HypervisorInvCmdLocation) { $GetVMAndHVSplatParams.Add("HypervisorFQDNOrIP",$HypervisorInvCmdLocation) } elseif ($HypervisorFQDNOrIP) { $GetVMAndHVSplatParams.Add("HypervisorFQDNOrIP",$HypervisorFQDNOrIP) } } if ($HypervisorCreds -and $GetVMAndHVSplatParams.Keys -notcontains "HypervisorCreds") { $GetVMAndHVSplatParams.Add("HypervisorCreds",$HypervisorCreds) } if ($($TryWithoutHypervisorInfo -and $GetVMAndHVSplatParams.Keys -notcontains "TryWithoutHypervisorInfo") -or $($(ConfirmAWSVM -EA SilentlyContinue) -or $(ConfirmAzureVM -EA SilentlyContinue) -or $(ConfirmGoogleComputeVM -EA SilentlyContinue)) ) { $GetVMAndHVSplatParams.Add("TryWithoutHypervisorInfo",$True) } if ($AllowRestarts -and $GetVMAndHVSplatParams.Keys -notcontains "AllowRestarts") { $GetVMAndHVSplatParams.Add("AllowRestarts",$True) } if ($NoMacAddressSpoofing -and $GetVMAndHVSplatParams.Keys -notcontains "NoMacAddressSpoofing") { $GetVMAndHVSplatParams.Add("NoMacAddressSpoofing",$True) } if ($SkipHyperVInstallCheck -and $GetVMAndHVSplatParams.Keys -notcontains "SkipHyperVInstallCheck") { $GetVMAndHVSplatParams.Add("SkipHyperVInstallCheck",$True) } if ($SkipExternalvSwitchCheck -and $GetVMAndHVSplatParams.Keys -notcontains "SkipExternalvSwitchCheck") { $GetVMAndHVSplatParams.Add("SkipExternalvSwitchCheck",$True) } try { $GuestVMAndHVInfo = Get-GuestVMAndHypervisorInfo @GetVMAndHVSplatParams -ErrorAction SilentlyContinue -ErrorVariable GGIErr if (!$GuestVMAndHVInfo) {throw "The Get-GuestVMAndHypervisorInfo function failed! Halting!"} } catch { Write-Error $_ if ($($GGIErr | 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 Get-GuestVMAndHypervisorInfo function are as follows:" Write-Error $($GGIErr | Out-String) $global:FunctionResult = "1" return } } $EnableNestedVMSplatParams = @{ GuestVMAndHVInfo = $GuestVMAndHVInfo SkipPrompt = $True ErrorAction = "SilentlyContinue" ErrorVariable = "ENVErr" WarningAction = "SilentlyContinue" SkipHyperVInstallCheck = $True } if ($TryWithoutHypervisorInfo) { $EnableNestedVMSplatParams.Add("TryWithoutHypervisorInfo",$True) } if ($NoMacAddressSpoofing) { $EnableNestedVMSplatParams.Add("NoMacAddressSpoofing",$True) } if ($NATIP) { $EnableNestedVMSplatParams.Add("NATIP",$NATIP) } if ($NATNetworkMask) { $EnableNestedVMSplatParams.Add("NATNetworkMask",$NATNetworkMask) } if ($NATName) { $EnableNestedVMSplatParams.Add("NATName",$NATName) } try { $EnableNestedVMResults = EnableNestedVM @EnableNestedVMSplatParams if (!$EnableNestedVMResults) {throw "The EnableNestedVM function failed! Halting!"} } catch { Write-Error $_ Write-Host "Errors for the EnableNestedVM function are as follows:" Write-Error $($ENVErr | Out-String) $global:FunctionResult = "1" return } } else { Write-Host "Sleeping for 180 seconds to wait for Guest VM to be ready..." Start-Sleep -Seconds 180 } } if ($EnableNestedVMResults.UnsatisfiedChanges.Count -gt 1 -or $($EnableNestedVMResults.UnsatisfiedChanges.Count -eq 1 -and $EnableNestedVMResults.UnsatisfiedChanges[0] -ne "None") ) { $GuestVMReady = $False } else { $GuestVMReady = $True } if (!$GuestVMReady) { $EnableNestedVMResults Write-Error "The EnableNestedVM function was not able to make all necessary changes to the Guest VM $($GuestVMAndHVInfo.HostNameNetworkInfo.HostName)! Halting!" $global:FunctionResult = "1" return } } } if ($InstallEnvironment -eq "BareMetal") { # Determine where this function is being run try { $HostNameNetworkInfo = ResolveHost -HostNameOrIP $TargetHostNameOrIP } catch { Write-Error $_ $global:FunctionResult = "1" return } if ($env:ComputerName -match "$($HostNameNetworkInfo.HostName)|$($HostNameNetworkInfo.FQDN)") { $Locale = "BareMetalTarget" } else { $Locale = "Elsewhere" } if ($AllowRestarts) { if ($env:ComputerName -eq $HostNameNetworkInfo.HostName) { $DefaultRestartWarning = "If any restarts occur, Docker will NOT have been installed " + "(only prerequisites will have been configured). To complete Docker installation after restart, " + "you must re-run the Install-Docker function AFTER restarting!" Write-Warning $DefaultRestartWarning } } } ##### END Variable/Parameter Transforms and PreRun Prep ##### ##### BEGIN Main Body ##### if ($InstallEnvironment -eq "GuestVM") { Write-Host "Attempting to install DockerCE on Guest VM..." } if ($InstallEnvironment -eq "BareMetal") { if ($PSBoundParameters['NoMacAddressSpoofing'] -or $PSBoundParameters['GuestVMAndHVInfo'] -or $PSBoundParameters['SkipEnableNestedVM'] -or $PSBoundParameters['GuestVMMemoryInGB'] -or $PSBoundParameters['HypervisorFQDNOrIP'] -or $PSBoundParameters['HypervisorCreds'] ) { $ErrMsg = "The parameters -NoMacAddressSpoofing, -GuestVMAndHVInfo, -SkipEnableNestedVM, -GuestVMMemoryInGB, " + "-HypervisorFQDNOrIP, and -HypervisorCreds are only meant to be used when installing Docer CE on a " + "Guest VM, but the install environment was determined to be Bare Metal! Halting!" Write-Error $ErrMsg $global:FunctionResult = "1" return } Write-Host "Attempting to install DockerCE on Baremetal..." } if ($Locale -match "GuestVM|BareMetalTarget") { $DoDockerInstallSplatParams = @{} if ($MobyLinuxVMMemoryInGB) { $DoDockerInstallSplatParams.Add("MobyLinuxVMMemoryInGB",$MobyLinuxVMMemoryInGB) } if ($AllowRestarts) { $DoDockerInstallSplatParams.Add("AllowRestarts",$True) } if ($SkipHyperVInstallCheck) { $DoDockerInstallSplatParams.Add("SkipHyperVInstallCheck",$True) } if ($RecreateMobyLinuxVM) { $DoDockerInstallSplatParams.Add("RecreateMobyLinuxVM",$RecreateMobyLinuxVM) } if ($PreRelease) { $DoDockerInstallSplatParams.Add("PreRelease",$True) } if ($AllowLogout) { $DoDockerInstallSplatParams.Add("AllowLogout",$True) } try { DoDockerInstall @DoDockerInstallSplatParams } catch { Write-Error $_ Write-Error "The DoDockerInstall function failed! Halting!" $global:FunctionResult = "1" return } } if ($Locale -match "Hypervisor|Elsewhere") { # Solution to define local function in remote Invoke-Command ScriptBlock from: # https://www.reddit.com/r/PowerShell/comments/7oux5q/invokecommandas_on_remote_machine_using/ $FunctionsForRemoteUse = @( ${Function:Update-PackageManagement}.Ast.Extent.Text ${Function:Manual-PSGalleryModuleInstall}.Ast.Extent.Text ${Function:GetElevation}.Ast.Extent.Text ${Function:Install-Program}.Ast.Extent.Text ${Function:Install-ChocolateyCmdLine}.Ast.Extent.Text ${Function:Refresh-ChocolateyEnv}.Ast.Extent.Text ${Function:InstallFeatureDism}.Ast.Extent.Text ${Function:InstallHyperVFeatures}.Ast.Extent.Text ${Function:TestIsValidIPAddress}.Ast.Extent.Text ${Function:GetvSwitchAllRelatedInfo}.Ast.Extent.Text ${Function:DoDockerInstall}.Ast.Extent.Text ${Function:GetFileLockProcess}.Ast.Extent.Text ) $RunDockerInstallSB = { # Load the functions we packed up: $using:FunctionsForRemoteUse | foreach { Invoke-Expression $_ } $DoDockerInstallSplatParams = @{} if ($using:MobyLinuxVMMemoryInGB) { $DoDockerInstallSplatParams.Add("MobyLinuxVMMemoryInGB",$using:MobyLinuxVMMemoryInGB) } if ($using:AllowRestarts) { $DoDockerInstallSplatParams.Add("AllowRestarts",$True) } if ($using:SkipHyperVInstallCheck) { $DoDockerInstallSplatParams.Add("SkipHyperVInstallCheck",$True) } if ($using:RecreateMobyLinuxVM) { $DoDockerInstallSplatParams.Add("RecreateMobyLinuxVM",$True) } if ($using:PreRelease) { $DoDockerInstallSplatParams.Add("PreRelease",$True) } DoDockerInstall @DoDockerInstallSplatParams } if ($GuestVMAndHVInfo) { # The fact that $GuestVMAndHVInfo has TargetHostNameCreds means that they work because the # Get-GuestVMAndHypervisorInfo function figured that out for us. $TargetHostNameCreds = $GuestVMAndHVInfo.TargetHostNameCreds if ($TargetHostNameCreds -eq $(whoami)) { $TargetHostNameCreds = $null } $TargetHostInvCmdLocation = $GuestVMAndHVInfo.TargetHostInvCmdLocation } $DockerInstallSplatParams = @{ ComputerName = $TargetHostInvCmdLocation ScriptBlock = $RunDockerInstallSB } if ($TargetHostNameCreds) { $DockerInstallSplatParams.Add("Credential",$TargetHostNameCreds) } try { $InvokeCommandOutput = Invoke-Command @DockerInstallSplatParams -ErrorAction SilentlyContinue -ErrorVariable DoDockerErr if ($InvokeCommandOutput -eq "Restarting") { Write-Host "$($TargetHostInvCmdLocation) is restarting..." } elseif ($InvokeCommandOutput -eq $null) { throw "The DoDockerInstall function failed!" } elseif ($InvokeCommandOutput -ne $null) { $InvokeCommandOutput return } } catch { if ($($_ | Out-String) -match "The I/O operation has been aborted") { $CaughtRestarting = $True } else { Write-Error $_ Write-Host "Errors for the DoDockerInstall function are as follows:" Write-Error $($DoDockerErr | Out-String) $global:FunctionResult = "1" return } } # Run the DoDockerInstall function again on the Target Guest VM if ($InvokeCommandOutput -eq "Restarting" -or $CaughtRestarting) { # IMPORTANT NOTE: Depending on the Hyper-V features that needed to be Enabled, in the first run # of DoDocker install, the Guest VM might restart TWICE before it's ready. # With each restart, the OS takes about 90 seconds to boot, and feature installation # operations take about 60 seconds per reboot, so we're looking at 300 seconds, ande we'll # add another 60 seconds just to make sure, making it 360 seconds Write-Host "Sleeping for 360 seconds..." Start-Sleep -Seconds 360 Write-Host "Running DoDockerInstall post-restart..." try { $InvokeCommandOutput = Invoke-Command @DockerInstallSplatParams -ErrorAction SilentlyContinue -ErrorVariable DoDockerErr if ($InvokeCommandOutput -eq "Restarting") { Write-Host "$($TargetHostInvCmdLocation) is restarting..." } elseif ($InvokeCommandOutput -eq $null) { throw "The DoDockerInstall function failed!" } elseif ($InvokeCommandOutput -ne $null) { $InvokeCommandOutput return } } catch { Write-Error $_ Write-Host "Errors for the DoDockerInstall function are as follows:" Write-Error $($DoDockerErr | Out-String) $global:FunctionResult = "1" return } } } ##### END Main Body ##### } |