functions/public.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 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 |
#region Get-LabSummary Function Test-ISOImage { [cmdletbinding()] [outputtype("ISOTest")] Param() $progParam = @{ Activity = $myinvocation.MyCommand PercentComplete = 0 CurrentOperation = "" } Write-Verbose "Starting $($myinvocation.mycommand)" Try { $LabHost = Lability\Get-LabHostDefault -ErrorAction stop $ISOPath = $LabHost.IsoPath $progParam.Add("Status", "Validating ISO image files in $ISOPath") } Catch { Throw $_ } Write-Verbose "Testing ISO images under $ISOPath" $files = Get-ChildItem -Path $ISOPath -Filter *.iso if ($files.count -gt 0) { #initialize a counter $i = 0 foreach ($file in $files) { $i++ $progParam.PercentComplete = ($i / $files.count) * 100 $progParam.CurrentOperation = $file.name Write-Progress @progParam #construct the checksum file $chkfile = Join-Path -Path $ISOPath -ChildPath "$($file.name).checksum" Write-Verbose "Processing $($file.name)" $hashData = Get-FileHash -Path $file.fullname -Algorithm MD5 if (Test-Path $chkfile) { $chksum = Get-Content -Path $chkfile if ($chksum -eq $hashdata.hash) { $Valid = $True } } else { Write-Warning "Missing checksum file $chkfile" $chksum = "unknown" $Valid = $False } #write a custom object to the pipeline [pscustomobject]@{ PSTypename = "ISOTest" Path = $hashData.Path Valid = $Valid Size = (Get-Item -Path $hashData.path).length Hash = $hashData.Hash Checksum = $chksum Algorithm = $hashData.Algorithm } } #foreach file } #if files else { Write-Warning "Failed to find any ISO files in $ISOPath." } Write-Verbose "Ending $($myinvocation.mycommand)" } Function Get-LabSummary { [cmdletbinding()] [Alias("Setup-Lab")] Param ( [Parameter(Position = 0, ValueFromPipeline, HelpMessage = "The PATH to the lab configuration folder. Normally, you should run all commands from within the configuration folder. Do not include the psd1 file name.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = "." ) Begin { Write-Verbose "Starting $($myinvocation.mycommand)" #create the media lookup table lability\get-labmedia | foreach-object -begin { $media=[ordered]@{}} -Process { $media.Add($_.id,$_.description)} } Process { $Path = Convert-Path $path Write-Verbose "Searching in $path for VMConfigurationData.psd1" $psd1 = $(Join-Path $Path -ChildPath Vmconfigurationdata.psd1) if (Test-Path $psd1) { $labname = Split-Path $Path -Leaf Write-Verbose "Getting summary for $labname" Write-Verbose "Getting node data from $psd1" $import = Import-PowerShellDataFile -Path $psd1 $Nodes = $import.allNodes #get the optional prefix value $EnvPrefix = $import.NonNodeData.Lability.EnvironmentPrefix $nodes.where( { $_.Nodename -ne '*' }).Foreach( { if ($_.lability_startupmemory) { $mem = $_.lability_startupmemory } elseif ($_.lability_MinimumMemory) { $mem = $_.lability_minimummemory } else { $mem = $nodes[0].Lability_MinimumMemory } if ($_.Lability_ProcessorCount) { $ProcCount = $_.Lability_ProcessorCount } else { $ProcCount = 1 } #added for issue #245 where lability_media might not be defined if ($_.lability_media) { $description = $media[$_.lability_media] } else { #check for default if ($nodes[0].lability_media) { $description = $nodes[0].lability_media } else { $description = "unknown" } } [pscustomobject]@{ PSTypeName = "PSAutolabVM" Computername = $_.NodeName VMName = "{0}{1}" -f $envPrefix, $_.NodeName InstallMedia = $_.lability_media Description = $description Role = $_.Role IPAddress = $_.IPAddress MemoryGB = $mem / 1GB Processors = $ProcCount Lab = $labname } }) } else { Write-Warning "Failed to find $psd1." } } #process End { Write-Verbose "Ending $($myinvocation.mycommand)" } } #endregion #region Get-PSAutoLabSetting Function Get-PSAutoLabSetting { [cmdletbinding()] [outputType("PSAutoLabSetting")] Param() Write-Verbose "Starting $($myinvocation.mycommand)" $psver = $PSVersionTable Try { Write-Verbose "Getting operating system details" $cimos = Get-CimInstance -class Win32_operatingsystem -Property caption, FreePhysicalMemory, TotalVisibleMemorySize -ErrorAction Stop $os = $cimos.caption $mem = $cimos.TotalVisibleMemorySize $pctFree = [math]::round(($cimos.FreePhysicalMemory / $cimos.TotalVisibleMemorySize) * 100, 2) } Catch { $os = "" $mem = 0 $pctFree = 0 } Write-Verbose "Getting Autolab folder if installed and free hard drive space" Try { $LabHost = Lability\Get-LabHostDefault -ErrorAction stop $AutoLab = Split-Path $LabHost.ConfigurationPath $free = (Get-Volume $autolab[0]).SizeRemaining } Catch { $AutoLab = "NotFound" $free = (Get-Volume C).SizeRemaining #Assume C drive } Write-Verbose "Get network category for LabNet" $net = Get-NetConnectionprofile -interfacealias *LabNet* if ($net) { $netProfile = $net.NetworkCategory } else { $netProfile = "unknown" } [pscustomobject]@{ PSTypeName = "PSAutoLabSetting" AutoLab = $Autolab PSVersion = $psver.PSVersion PSEdition = $psver.PSEdition OS = $os FreeSpaceGB = [math]::Round($free / 1GB, 2) MemoryGB = ($mem * 1kb) / 1GB -as [int] PctFreeMemory = $pctFree Processor = (Get-CimInstance -ClassName Win32_Processor -Property Name).Name IsElevated = (Test-IsAdministrator) RemotingEnabled = $(try { [void](Test-WSMan -ErrorAction stop); $True } catch { $false }) NetConnectionProfile = $netProfile HyperV = (Get-Item $env:windir\System32\vmms.exe).versioninfo.productversion PSAutolab = (Get-Module -Name PSAutolab -ListAvailable | Sort-Object -Property Version -Descending).version Lability = (Get-Module -Name Lability -ListAvailable | Sort-Object -Property Version -Descending).version Pester = (Get-Module -Name Pester -ListAvailable | Sort-Object -Property Version -Descending).version PowerShellGet = (Get-Module -Name PowerShellGet -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1).version PSDesiredStateConfiguration = (Get-Module -Name PSDesiredStateConfiguration -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1).version } if ($netProfile -eq 'Unknown' -or (-not $netprofile)) { Write-Warning "The network connection profile for LabNet is not set to Private or DomainAuthenticated. Commands that rely on PowerShell remoting may fail." } if ([math]::Round($free / 1GB, 2) -le 50) { Write-Warning "You may not have enough free disk space. 100GB is recommended, although you can get by with less depending on the lab configuration you need to run." } if (($mem * 1kb) / 1GB -as [int] -le 16) { Write-Warning "You may not have enough memory. 16GB or more is recommended" } Write-Verbose "Ending $($myinvocation.mycommand)" } #endregion #region Invoke-RefreshHost Function Invoke-RefreshHost { [cmdletbinding(SupportsShouldProcess)] [alias("Refresh-Host")] Param( [Parameter(Position = 0, HelpMessage = "The path to your Autolab configuration path, ie C:\Autolab\ConfigurationPath")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Destination = (Get-LabHostDefault).configurationpath, [switch]$SkipPublisherCheck ) #test if a new version of lability is required if ($pscmdlet.ShouldProcess("version $LabilityVersion", "Check for Lability Requirements")) { _LabilityCheck -requiredVersion $LabilityVersion -skipPublishercheck:$SkipPublisherCheck } #test and update Pester as needed if ($pscmdlet.ShouldProcess("version $PesterVersion", "Check for required Pester version")) { _PesterCheck } # Setup Path Variables $SourcePath = $ConfigurationPath Microsoft.PowerShell.Utility\Write-Host "Updating configuration files from $sourcePath" -ForegroundColor Cyan if ($pscmdlet.ShouldProcess($Destination, "Copy configurations")) { if (Test-Path $Destination) { Copy-Item -Path $SourcePath\* -Recurse -Destination $Destination -Force } else { Write-Warning "Can't find target path $Destination." } } Microsoft.PowerShell.Utility\Write-Host "This process will not remove any configurations that have been deleted from the module." -ForegroundColor yellow } #endregion #region Invoke-SetupHost Function Invoke-SetupHost { [CmdletBinding(SupportsShouldProcess)] [alias("Setup-Host")] Param( [Parameter(HelpMessage = "Specify the parent path. The default is C:\Autolab. The command will create the folder.")] [string]$DestinationPath = "C:\Autolab" ) Clear-Host # Setup Path Variables #use module defined variable $SourcePath = $ConfigurationPath Write-Verbose "Starting $($invocation.mycommand)" Write-Verbose "SourcePath = $SourcePath" Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" This is the Setup-Host script. This script will perform the following: * For PowerShell Remoting, configure the host 'TrustedHosts' value to * * Install the Lability module from PSGallery * Update the Pester module if necessary * Install Hyper-V * Create the $DestinationPath folder (DO NOT DELETE) * Copy configurations and resources to $DestinationPath * You will then need to reboot the host before continuing "@ Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Yellow -Object @" !!IMPORTANT SECURITY NOTE!! This module will set trusted hosts to connect to any machine on the local network. This is NOT a recommended security practice. It is assumed you are installing this module on a non-production machine and are willing to accept this potential risk for the sake of a training and test environment. If you do not want to proceed, press Ctrl-C. "@ Pause # For remoting commands to VM's - have the host set trustedhosts Write-Verbose "Enable PSRemoting" Enable-PSRemoting -Force -SkipNetworkProfileCheck Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object "Setting TrustedHosts so that remoting commands to VMs work properly" $trust = Get-Item -Path WSMan:\localhost\Client\TrustedHosts if (($Trust.Value -eq "*") -or ($trust.Value -match "<local>")) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object "TrustedHosts is already set to *. No changes needed" } else { $add = '<local>' # Jeffs idea - 'DC,S*,Client*,192.168.3.' - need to automate this, not hard code Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object "Adding $add to TrustedHosts" Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value $add -Concatenate -Force } # Lability install Write-Verbose "Bootstrapping Nuget install" Install-PackageProvider -Name Nuget -ForceBootstrap Write-Verbose "Setting PSGallery as a trusted package source" Get-PackageSource -Name PSGallery | Set-PackageSource -Trusted -Force -ForceBootstrap | Out-Null <# Test if the current version of Lability is already installed. If so, do nothing. If an older version is installed, update the version, otherwise install the latest version. #> #update Pester as needed Write-Verbose "Calling internal Pester check" _PesterCheck #use the module defined variable and a private function if ($pscmdlet.ShouldProcess("Lability $labilityVersion", "Install or Update Lability")) { Write-Verbose "Calling internal Lability check" _LabilityCheck $LabilityVersion Import-Module Lability } # Set Lability folder structure $DirDef = @{ ConfigurationPath = "$DestinationPath\Configurations" DifferencingVhdPath = "$DestinationPath\VMVirtualHardDisks" HotfixPath = "$DestinationPath\Hotfixes" IsoPath = "$DestinationPath\ISOs" ModuleCachePath = "C:\ProgramData\Lability\Modules" ParentVhdPath = "$DestinationPath\MasterVirtualHardDisks" ResourcePath = "$DestinationPath\Resources" } Write-Verbose "Set-LabHostDefault" Lability\Set-LabHostDefault @DirDef Write-Verbose "Test-LabHostConfiguration" $HostStatus = Lability\Test-LabHostConfiguration If (-Not $HostStatus ) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan "Starting to Initialize host and install Hyper-V" if ($pscmdlet.shouldprocess($DirDef.ConfigurationPath, "Lability\Start-LabHostConfiguration")) { Lability\Start-LabHostConfiguration -ErrorAction SilentlyContinue } } ###### COPY Configs to host machine Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object "Copying configs to $($DirDef.ConfigurationPath)" if ($pscmdlet.ShouldProcess($DirDef.ConfigurationPath, "Copy configurations")) { Copy-Item -Path $SourcePath\* -Recurse -Destination $DirDef.ConfigurationPath -Force } if ($pscmdlet.ShouldProcess($env:computername, "Prompt for restart")) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" The localhost is about to reboot. After the reboot, open a Windows PowerShell prompt, navigate to a configuration directory $($DirDef.ConfigurationPath)\<yourconfigfolder> And run: PS $($DirDef.ConfigurationPath)\<yourconfigfolder>Setup-Lab -ignorependingreboot or PS $($DirDef.ConfigurationPath)\<yourconfigfolder>Unattend-Lab -ignorependingreboot "@ Write-Warning "System needs to reboot, especially if you just installed Hyper-V." $reboot = Read-Host "Do you wish to reboot now? (y/n)" If ($reboot -eq 'y') { Write-Output "Rebooting now" Restart-Computer } } #whatif Write-Verbose "Ending $($invocation.mycommand)" } #endregion #region Setup-Lab Function Invoke-SetupLab { [cmdletbinding(SupportsShouldProcess)] [Alias("Setup-Lab")] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = ".", [switch]$IgnorePendingReboot, [Parameter(HelpMessage = "Override any configuration specified time zone and use the local time zone on this computer.")] [switch]$UseLocalTimeZone, [Parameter(HelpMessage = "Run the command but suppress all status messages.")] [switch]$NoMessages ) Write-Verbose "Starting $($myinvocation.mycommand)" $Path = Convert-Path $path $labname = Split-Path $Path -Leaf $LabData = Import-PowerShellDataFile -Path $(Join-Path $Path -ChildPath *.psd1) $DSCResources = $LabData.NonNodeData.Lability.DSCResource if (-Not $DSCResources) { Write-Warning "Failed to find DSC Resource information. Are you in a directory with configuration data .psd1 file?" #bail out return } if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" This is the Setup-Lab script. This script will perform the following: * Run the configs to generate the required .mof files Note! - If there is an error creating the .mofs, the setup will fail * Run the lab setup Note! If this is the first time you have run this, it can take several hours to download the ISO files and resources depending on the configuration. You may also see new drives being added and removed as the ISO is mounted and converted. This step should only happen the first time you run this command. ** Possible problem If the downloads finish but the script doesn't continue (pauses), press the Enter key once and it should continue *You will be able to wipe and rebuild this lab without needing to perform the downloads again. "@ } if ($UseLocalTimeZone) { #modifying this old code since it doesn't translate properly from some #non-US locations. (Issue #227) # $localtz = [System.TimeZone]::CurrentTimeZone.StandardName $localtz = (Get-TimeZone).ID Write-Verbose "Using local time zone $localtz" if ($LabData.allnodes.count -gt 1) { if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host "Overriding configured time zones to use $localtz" -ForegroundColor yellow } $nodes = $labdata.allnodes.where( { $_.nodename -ne "*" }) foreach ($node in $nodes) { # $tz = $node.Lability_timezone $node.Lability_timeZone = $localtz } } else { if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host "Updating Allnodes to $localtz" -ForegroundColor Yellow } $LabData.AllNodes.Lability_TimeZone = $localtz } } #use local timezone $LabData.allnodes | Out-String | Write-Verbose Write-Verbose "Install DSC Resource modules specified in the .PSD1" If (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object 'Installing required DSCResource modules from PSGallery' Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Yellow -Object 'You may need to say "yes" to a Nuget Provider' } #force updating/installing nuget to bypass the prompt [void](Install-PackageProvider -Name nuget -Force -ForceBootstrap) Foreach ($DSCResource in $DSCResources) { #test if current version is installed otherwise update or install it $dscmod = Get-Module -FullyQualifiedName @{Modulename = $DSCResource.name; ModuleVersion = $DSCResource.RequiredVersion } -ListAvailable if ((-not $dscmod ) -or ($dscmod.version -ne $DSCResource.RequiredVersion)) { if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host "install $($dscresource.name) version $($DSCResource.requiredversion)" -ForegroundColor yellow } if ($pscmdlet.ShouldProcess($DSCResource.name, "Install-Module")) { Install-Module -Name $DSCResource.Name -RequiredVersion $DSCResource.RequiredVersion } } elseif ($dscmod.version -ne ($DSCResource.RequiredVersion -as [version])) { if (-not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host "Update $($dscmod.name) to version $($DSCResource.requiredversion)" -ForegroundColor cyan } if ($pscmdlet.ShouldProcess($DSCResource.name, "Update-Module")) { Update-Module -Name $DSCResource.Name -RequiredVersion $DSCResource.RequiredVersion } } else { If (-Not $nomessages) { Microsoft.PowerShell.Utility\Write-Host "$($dscmod.name) [v$($dscmod.version)] requires no updates." -ForegroundColor green } } } Write-Verbose "Run the config to generate the .mof files" If (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object 'Build the .Mof files from the configs' } $vmconfig = Join-Path -Path $path -ChildPath 'VMConfiguration.ps1' if ($PSCmdlet.ShouldProcess($vmConfig)) { . $VMConfig } # Build the lab without a snapshot if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object "Building the lab environment for $labname" } # Creates the lab environment without making a Hyper-V Snapshot $Password = ConvertTo-SecureString -String "$($labdata.allnodes.labpassword)" -AsPlainText -Force $startParams = @{ ConfigurationData = $LabData #Import-PowerShellDataFile -path (Join-Path -path $path -childpath "VMConfigurationdata.psd1") Path = $Path NoSnapshot = $True Password = $Password IgnorePendingReboot = $True WarningAction = "SilentlyContinue" ErrorAction = "stop" } Write-Verbose "Using these start parameters" Write-Verbose ($startParams | Out-String) if ($PSCmdlet.ShouldProcess($labname, "Start-LabConfiguration")) { Try { Write-Verbose "Invoking Start-LabConfiguration" Lability\Start-LabConfiguration @startParams } Catch { Microsoft.PowerShell.Utility\Write-Host "Failed to start lab configuration." -foreground red throw $_ } Write-Verbose "Disable secure boot for VM's" $VM = Hyper-V\Get-VM ( Lability\Get-LabVM -ConfigurationData "$path\*.psd1" ).Name Hyper-V\Set-VMFirmware -VM $vm -EnableSecureBoot Off -SecureBootTemplate MicrosoftUEFICertificateAuthority If (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" Next Steps: When this task is complete, run: Run-Lab To enable Internet access for the VM's, run: Enable-Internet Run the following to validate when configurations have converged: Validate-Lab To stop the lab VM's: Shutdown-lab When the configurations have finished, you can checkpoint the VM's with: Snapshot-Lab To quickly rebuild the labs from the checkpoint, run: Refresh-Lab To destroy the lab to build again: Wipe-Lab "@ } } #should process Write-Verbose "Ending $($myinvocation.mycommand)" } #endregion Setup-lab #region Run-Lab Function Invoke-RunLab { [cmdletbinding(SupportsShouldProcess)] [alias("Run-Lab")] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = ".", [Parameter(HelpMessage = "Run the command but suppress all status messages.")] [switch]$NoMessages ) $Path = Convert-Path $path if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" This is the Run-Lab script. This script will perform the following: * Start the Lab environment Note! If this is the first time you have run this, it can take up to an hour for the DSC configurations to apply and converge. "@ } $labname = Split-Path (Get-Location) -Leaf $datapath = Join-Path $(Convert-Path $path) -ChildPath "*.psd1" if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object "Starting the lab environment from $datapath" } $data = Import-PowerShellDataFile -Path $datapath # Creates the lab environment without making a Hyper-V Snapshot if ($pscmdlet.ShouldProcess($labname, "Start Lab")) { try { Start-Lab -ConfigurationData $data -ErrorAction Stop } Catch { Write-Warning "Failed to start lab. Are you running this in the correct configuration directory? $($_.exception.message)" #bail out because no other commands are likely to work return } if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" Next Steps: To enable Internet access for the VM's, run: Enable-Internet Run the following to validatae when configurations have converged: Validate-Lab To stop the lab VM's: Shutdown-lab When the configurations have finished, you can checkpoint the VM's with: Snapshot-Lab To quickly rebuild the labs from the checkpoint, run: Refresh-Lab To destroy the lab to build again: Wipe-Lab "@ } } #whatif } #endregion Run-lab #region Enable-Internet Function Enable-Internet { [cmdletbinding(SupportsShouldProcess)] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = ".", [Parameter(HelpMessage = "Run the command but suppress all status messages.")] [switch]$NoMessages ) $Path = Convert-Path $path if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" This is the Enable-Internet script. This script will perform the following: * Enable Internet to the VM's using NAT * Note! - If this generates an error, you are already enabled, or one of the default settings below does not match your .PSD1 configuration "@ } $LabData = Import-PowerShellDataFile -Path $path\*.psd1 $LabSwitchName = $labdata.NonNodeData.Lability.Network.name $GatewayIP = $Labdata.AllNodes.DefaultGateway $GatewayPrefix = $Labdata.AllNodes.SubnetMask $NatNetwork = $Labdata.AllNodes.IPnetwork $NatName = $Labdata.AllNodes.IPNatName $Index = Get-NetAdapter -Name "vethernet ($LabSwitchName)" | Select-Object -ExpandProperty InterfaceIndex if ($pscmdlet.ShouldProcess("Interface index $index", "New-NetIPAddress")) { New-NetIPAddress -InterfaceIndex $Index -IPAddress $GatewayIP -PrefixLength $GatewayPrefix -ErrorAction SilentlyContinue } # Creating the NAT on Server 2016 -- maybe not work on 2012R2 if ($pscmdlet.ShouldProcess($NatName, "New-NetNat")) { New-NetNat -Name $NatName -InternalIPInterfaceAddressPrefix $NatNetwork -ErrorAction SilentlyContinue } if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" Next Steps: When complete, run: Run-Lab And run: Validate-Lab "@ } } #endregion Enable-Internet #region Validate-Lab Function Invoke-ValidateLab { [cmdletbinding()] [alias("Validate-Lab")] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( {Test-Path $_ })] [string]$Path = ".", [Parameter(HelpMessage = "Run the command but suppress all status messages.")] [switch]$NoMessages ) Write-Verbose "Starting $($myinvocation.mycommand)" $modVersion = (Get-Module PSAutolab).Version #build a hashtable of computernames and VMNames $sum = Get-LabSummary -path $path $cnHash = @{} foreach ($item in $sum) { $cnHash.Add($item.Computername,$item.VMName) } #remove pester v5 Get-Module Pester | Remove-Module -Force Write-Verbose "Importing Pester module version $PesterVersion" #use the module specific version of Pester Import-Module -Name Pester -RequiredVersion $PesterVersion -Force -Global $Path = Convert-Path $path Write-Verbose "Using path $path" If (-Not $NoMessages) { $msg = @" [$(Get-Date)] Starting the VM testing process. This could take some time to complete depending on the complexity of the configuration. You can press Ctrl+C at any time to break out of the testing loop. If you feel the test is taking too long, break out of the testing loop and manually run the test: Invoke-Pester .\vmvalidate.test.ps1 Make sure you are using version $PesterVsion of the Pester module. Remove any other versions first and then re-import Get-Module Pester | Remove-Module -force Import-Module -name Pester -RequiredVersion $PesterVersion -force You might also use Get-VM to verify all the VMs are running. Use Start-VM to start a stopped virtual machine. If only one of the VMs appears to be failing, you might try stopping and restarting it with the Hyper-V Manager or the cmdlets: Stop-VM <vmname> Start-VM <vmname> Errors are expected until all tests complete successfully. "@ Microsoft.PowerShell.Utility\Write-Host $msg -ForegroundColor Cyan } $Complete = $False #define a resolved path to the test file $testPath = Join-Path -Path $path -ChildPath VMValidate.test.ps1 $firstTest = Get-Date #keep track of the number of passes $i=0 Write-Verbose "Running initial validation test" do { $i++ Write-Verbose "Validation pass $i" $test = Invoke-Pester -Script $testpath -Show none -PassThru if ($test.Failedcount -eq 0) { $Complete = $True } else { #test every 5 minutes if ($i -ge 2) { #get names of VMs with failing tests $failedVMs = $test.TestResult.where({-Not $_.passed}).Describe | Get-Unique | ForEach-Object {$cnHash[$_]} if ( ($i -eq 4 -OR $i -eq 7)-AND $failedVMs) { #restart VMs that are still failing Get-VM $failedVMs | Where-Object {$_.state -eq 'running'} | foreach-object { Write-Warning "Restarting virtual machine $($_.name)" $_ | Restart-VM -force #give the VM a chance to change state Start-Sleep -seconds 10 } } #restart #restart any stopped VMs that are failing tests if ($failedVMs) { Get-VM $failedVMs | Where-Object {$_.state -eq 'Off'} | foreach-object { Write-Warning "Starting virtual machine $($_.name)" $_ | Start-VM } } $prog = @{ Activity = "VM Validation [$($test.passedcount)/$($test.Totalcount) tests passed in $i loop(s)] v$modVersion" Status = "In a separate PowerShell window, use Get-VM to verify the status of $($FailedVMs -join ',')." CurrentOperation = "Waiting until next test run" } } else { $prog = @{ Activity = "VM Validation [$($test.passedcount)/$($test.Totalcount) tests passed in $i loop(s)] v$modVersion" Status = "Waiting 5 minutes for configurations to converge" CurrentOperation = "Waiting until next test run" } } 300..1 | ForEach-Object { Write-Progress @prog -SecondsRemaining $_ Start-Sleep -Seconds 1 } } #bail out of testing after 65 minutes if ( ((get-date) - $firsttest).totalminutes -ge 65) { $Aborted = $True } } until ($Complete -OR $Aborted) if ($complete) { $lastTest = Get-Date Write-Verbose "Validation completed in $($lasttest - $firsttest)" #re-run test one more time to show everything that was tested. Write-Verbose "Re-run test to show results" Invoke-Pester -Script $path\VMValidate.Test.ps1 Write-Progress -Activity "VM Completion Test v.$modVersion" -Completed if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host "[$(Get-Date)] VM setup and configuration complete. It is recommended that you snapshot the VMs with Snapshot-Lab" -ForegroundColor Green } } else { #aborted $msg = @" Validation testing aborted. One or more virtual machines are not working properly. It is recommended that you run this command: Invoke-Pester .\vmvalidate.test.ps1 To see what tests are failing. Depending on the error, you may be able to manually resolve it in the virtual machine, or feel you can ignore it. Otherwise, use Get-VM to ensure virtual machines are running. You can also try rebooting your computer, returning to this folder and re-running Unattend-Lab. Or run Wipe-Lab -force and try the setup process again. "@ Write-Warning $msg } Write-Verbose "Ending $($myinvocation.mycommand)" } #endregion Validate-Lab #region Shutdown-Lab Function Invoke-ShutdownLab { [cmdletbinding(SupportsShouldProcess)] [alias("Shutdown-Lab")] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = ".", [Parameter(HelpMessage = "Run the command but suppress all status messages.")] [switch]$NoMessages ) $Path = Convert-Path $path if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" This is the Shutdown-Lab command. It will perform the following: * Shutdown the Lab environment: "@ } $labname = Split-Path $path -Leaf if (-Not $noMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object 'Stopping the lab environment' } # Creates the lab environment without making a Hyper-V Snapshot if ($pscmdlet.ShouldProcess($labname, "Stop-Lab")) { Try { Stop-Lab -ConfigurationData $path\*.psd1 -ErrorAction Stop } Catch { Write-Warning "Failed to stop lab. Are you running this in the correct configuration directory? $($_.exception.message)" #bail out because no other commands are likely to work return } } if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" Next Steps: When the configurations have finished, you can checkpoint the VM's with: Snapshot-Lab To quickly rebuild the labs from the checkpoint, run: Refresh-Lab To start the lab environment: Run-Lab To destroy the lab environment: Wipe-Lab "@ } } #endregion Shutdown-Lab #region Snapshot-Lab Function Invoke-SnapshotLab { [cmdletbinding(SupportsShouldProcess)] [alias("Snapshot-Lab")] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = ".", [Parameter(HelpMessage = "Specify a name for the virtual machine checkpoint")] [ValidateNotNullorEmpty()] [string]$SnapshotName = "LabConfigured", [Parameter(HelpMessage = "Run the command but suppress all status messages.")] [switch]$NoMessages ) $Path = Convert-Path $path if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" This is the Snapshot-Lab command. It will perform the following: * Snapshot the lab environment for easy and fast rebuilding Note! This should be done after the configurations have finished. "@ } $labname = Split-Path $Path -Leaf if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object 'Snapshot the lab environment' } # Creates the lab environment without making a Hyper-V Snapshot if ($pscmdlet.ShouldProcess($labname, "Stop-Lab")) { Try { Stop-Lab -ConfigurationData $path\*.psd1 -ErrorAction Stop } Catch { Write-Warning "Failed to stop lab. Are you running this in the correct configuration directory? $($_.exception.message)" #bail out because no other commands are likely to work return } } if ($pscmdlet.ShouldProcess($labname, "Checkpoint-Lab")) { Checkpoint-Lab -ConfigurationData $path\*.psd1 -SnapshotName $SnapshotName } if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" Next Steps: To start the lab environment, run: Run-Lab To quickly rebuild the labs from the checkpoint, run: Refresh-Lab "@ } } #endregion #region Refresh-Lab Function Invoke-RefreshLab { [cmdletbinding(SupportsShouldProcess)] [alias("Refresh-Lab")] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = ".", [Parameter(HelpMessage = "Specify a name for the virtual machine checkpoint")] [ValidateNotNullorEmpty()] [string]$SnapshotName = "LabConfigured", [Parameter(HelpMessage = "Run the command but suppress all status messages.")] [switch]$NoMessages ) $Path = Convert-Path $path if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" This command will perform the following: * Refresh the lab from a previous Snapshot You will be prompted to restore the snapshot for each VM, although the prompt won't show you the virtual machine name. Note! This can only be done if you created a snapshot using Snapshot-Lab "@ } $labname = Split-Path $path -Leaf if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object 'Restore the lab environment from a snapshot' } Try { if ($pscmdlet.ShouldProcess($labname, "Stop-Lab")) { Stop-Lab -ConfigurationData $path\*.psd1 -ErrorAction Stop } } Catch { Write-Warning "Failed to stop lab. Are you running this in the correct configuration directory? $($_.exception.message)" #bail out because no other commands are likely to work return } if ($pscmdlet.ShouldProcess($SnapshotName, "Restore-Lab")) { Restore-Lab -ConfigurationData $path\*.psd1 -SnapshotName $SnapshotName -Force } if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" Next Steps: To start the lab environment, run: Run-Lab To stop the lab environment, run: Shutdown-Lab To destroy this lab, run: Wipe-Lab "@ } } #endregion Refresh-Lab #region Wipe-Lab Function Invoke-WipeLab { [cmdletbinding(SupportsShouldProcess)] [alias("Wipe-Lab")] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = ".", [Parameter(HelpMessage = "Remove the VM Switch. It is retained by default")] [switch]$RemoveSwitch, [Parameter(HelpMessage = "Remove lab elements with no prompting.")] [switch]$Force, [Parameter(HelpMessage = "Run the command but suppress all status messages.")] [switch]$NoMessages ) $Path = Convert-Path $path if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" This command will perform the following: * Wipe the lab and VM's from your system for this configuration If you intend to rebuild labs or run other configurations you do not need to remove the LabNat PolicyStore Local. Press Ctrl+C to abort this command. "@ } if (-Not $Force) { Pause } $removeParams = @{ ConfigurationData = "$path\*.psd1" } if ($force) { $removeParams.Add("confirm", $False) } $labname = Split-Path $path -Leaf $LabData = Import-PowerShellDataFile -Path $path\*.psd1 if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Cyan -Object "Removing the lab environment for $labname" } # Stop the VM's if ($pscmdlet.ShouldProcess("VMs", "Stop-Lab")) { Try { #Forcibly stop all VMS since they are getting deleted anyway (Issue #229) Write-Verbose "Stopping all virtual machines in the configuration" #use the VMName which might be using a prefix (Issue 231) Hyper-V\Stop-VM -vmname (PSAutolab\Get-LabSummary -Path $Path).VMName -TurnOff Write-Verbose "Calling Stop-Lab" Stop-Lab -ConfigurationData $path\*.psd1 -ErrorAction Stop -WarningAction SilentlyContinue } Catch { Write-Warning "Failed to stop lab. Are you running this in the correct configuration directory? $($_.exception.message)" #bail out because no other commands are likely to work return } } # Remove .mof iles Write-Verbose "Removing MOF files" Remove-Item -Path $path\*.mof if ($RemoveSwitch) { Write-Verbose "Removing the Hyper-V switch" $removeParams.Add("RemoveSwitch", $True) # Delete NAT $NatName = $Labdata.AllNodes.IPNatName if ($pscmdlet.ShouldProcess("LabNat", "Remove NetNat")) { Write-Verbose "Remoting NetNat" Remove-NetNat -Name $NatName } } # Delete vM's if ($pscmdlet.ShouldProcess("VMConfigurationData.psd1", "Remove lab configuration")) { Write-Verbose "Removing Lab Configuration" Lability\Remove-LabConfiguration @removeParams } #only delete the VHD files associated with the configuration as you might have more than one configuration #running Write-Verbose "Removing VHD files" $nodes = ($labdata.allnodes.nodename).where( { $_ -ne '*' }) Get-ChildItem (Lability\Get-LabHostDefault).differencingVHDPath | Where-Object { $nodes -contains $_.basename } | Remove-Item #Remove-Item -Path "$((Get-LabHostDefault).DifferencingVHdPath)\*" -Force if (-Not $NoMessages) { Microsoft.PowerShell.Utility\Write-Host -ForegroundColor Green -Object @" Next Steps: Run the following and follow the onscreen instructions: Setup-Lab When complete, run: Run-Lab Run the following to validate when configurations have converged: Validate-Lab To enable Internet access for the VM's, run: Enable-Internet To stop the lab VM's: Shutdown-lab When the configurations have finished, you can checkpoint the VM's with: Snapshot-Lab To quickly rebuild the labs from the checkpoint, run: Refresh-Lab To destroy the lab to build again: Wipe-Lab "@ } } #endregion Wipe-Lab #region Unattend-Lab Function Invoke-UnattendLab { [cmdletbinding(SupportsShouldProcess)] [alias("Unattend-Lab")] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = ".", [switch]$AsJob, [Parameter(HelpMessage = "Override any configuration specified time zone and use the local time zone on this computer.")] [switch]$UseLocalTimeZone, [Parameter(HelpMessage = "Run the command but suppress all status messages.")] [switch]$NoMessages ) Write-Verbose "Starting $($myinvocation.mycommand)" $Path = Convert-Path $path Write-Verbose "Using Path $path" $sb = { [cmdletbinding()] Param([string]$Path, [bool]$UseLocalTimeZone, [bool]$NoMessages, [bool]$WhatIf, [string]$VerboseAction) #uncomment for testing and development #import-module C:\scripts\psautolab\PSAutoLab.psd1 -force $VerbosePreference = $VerboseAction if ($VerboseAction -eq "Continue") { [void]$psboundparameters.Add("Verbose", $True) } [void]$psboundparameters.remove("VerboseAction") Write-Verbose "Starting the unattended scriptblock" $WhatIfPreference = $WhatIf [void]$psboundparameters.remove("WhatIf") Write-Verbose "Using these scriptblock parameters:" Write-Verbose ($psboundparameters | Out-String) if (-Not $NoMessages) { $msg = @" This runs Setup-Lab, Run-Lab, and Validate-Lab commands. Starting the lab environment "@ Microsoft.PowerShell.Utility\Write-Host $msg -ForegroundColor Green } if ($pscmdlet.ShouldProcess("Setup-Lab", "Run Unattended")) { Write-Verbose "Setup-Lab" PSAutolab\Invoke-SetupLab @psboundparameters } #this parameter isn't used in the remaining commands [void]($psboundparameters.remove("UseLocalTimeZone")) if ($pscmdlet.ShouldProcess("Enable-Internet", "Run Unattended")) { Write-Verbose "Enable-Internet" PSAutolab\Enable-Internet @psboundparameters } if ($pscmdlet.ShouldProcess("Run-Lab", "Run Unattended")) { Write-Verbose "Run-Lab" PSAutolab\Invoke-RunLab @psboundparameters } if ($pscmdlet.ShouldProcess("Validate-Lab", "Run Unattended")) { Write-Verbose "Validate-Lab" PSAutolab\Invoke-ValidateLab @psboundparameters } if (-Not $NoMessages) { $msg = @" Unattended setup is complete. To stop the lab VM's: Shutdown-lab When the configurations have finished, you can checkpoint the VM's with: Snapshot-Lab To quickly rebuild the labs from the checkpoint, run: Refresh-Lab "@ Microsoft.PowerShell.Utility\Write-Host $msg -ForegroundColor Green } } #close scriptblock $icmParams = @{ Computername = $env:computername ArgumentList = @($Path, $UseLocalTimeZone, $NoMessages, $WhatIfPreference, $VerbosePreference) Scriptblock = $sb } if ($asjob) { $icmParams.Add("AsJob", $True) } Write-Verbose "Invoking command with these parameters" $icmParams | Out-String | Write-Verbose Invoke-Command @icmParams Write-Verbose "Ending $($myinvocation.mycommand)" } #endregion Unattend-Lab #region Get-LabSnapshot Function Get-LabSnapshot { [cmdletbinding()] Param ( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = "." ) Write-Verbose "Starting $($myinvocation.mycommand)" $Path = Convert-Path $path Write-Verbose "Getting mofs from $(Convert-Path $Path)" $VMs = (Get-ChildItem -Path $path -Filter *.mof -Exclude *meta* -Recurse).Basename if ($VMs) { Write-Verbose "Getting VMSnapshots for $($VMs -join ',')" $snaps = Hyper-V\Get-VMSnapshot -VMName $VMs if ($snaps) { $snaps Microsoft.PowerShell.Utility\Write-Host "All VMs in the configuration should belong to the same snapshot if you want to use Refresh-Lab." -foreground green } else { Microsoft.PowerShell.Utility\Write-Host "No VM snapshots found for lab machines in $path." -ForegroundColor yellow } } else { Write-Warning "No configuration MOF files found in $Path." } Write-Verbose "Ending $($myinvocation.mycommand)" } #endregion #region Update-Lab Function Update-Lab { [cmdletbinding()] Param( [Parameter(HelpMessage = "The path to the configuration folder. Normally, you should run all commands from within the configuration folder.")] [ValidateNotNullorEmpty()] [ValidateScript( { Test-Path $_ })] [string]$Path = ".", [switch]$AsJob ) Begin { Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)" $data = Import-PowerShellDataFile -Path $path\*.psd1 #The prefix only changes the name of the VM not the guest computername $prefix = $data.NonNodeData.Lability.EnvironmentPrefix $upParams = @{ VMName = $null Credential = $null } if ($AsJob) { Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Will update as background job" $upParams.Add("AsJob", $True) } } #begin Process { Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Updating Lab" if ($data) { $pass = ConvertTo-SecureString -String $data.AllNodes.labpassword -AsPlainText -Force $domain = $data.AllNodes.domainName $domcred = New-Object PSCredential -ArgumentList "$($domain)\administrator", $pass $wgcred = New-Object PSCredential -ArgumentList "administrator", $pass #get defined nodes $nodes = ($data.allnodes).where( { $_.nodename -ne '*' }) foreach ($node in $nodes) { $vmNode = ("{0}{1}" -f $prefix, $node.Nodename) #Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] ... $($node.nodename)" Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] ... $vmNode" #verify VM is running $vm = Hyper-V\Get-VM -Name $VMNode # $node.Nodename if ($vm.state -ne 'running') { # Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] ... Starting VM $($node.nodename)" Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] ... Starting VM $vmnnode" $vm | Start-VM Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] ... Waiting 30 seconds to give VM time to boot" Start-Sleep -Seconds 30 } $upParams.VMName = $VMNode #$node.nodename if ($node.role -contains "DC" -or $node.role -contains "DomainJoin") { $upParams.Credential = $domcred } else { $upParams.Credential = $wgcred } #calling a private function Invoke-WUUpdate @upParams } } else { Throw "Failed to find lab configuration data" } } #process End { Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)" } #end } #close Update-Lab #endregion #region Test-LabDSCResource Function Test-LabDSCResource { [cmdletbinding()] [outputtype("PSAutolabResource")] Param( [Parameter(Position = 0, HelpMessage = "Specify the folder path of an Autolab configuration or change locations to the folder and run this command.")] [ValidateScript( { Test-Path $_ })] [string]$Path = "." ) Begin { Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)" $cPath = Convert-Path -Path $Path $config = Join-Path -Path $cpath -ChildPath VMConfigurationData.psd1 $configName = Split-Path $cPath -Leaf } #begin Process { Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Testing resources in $cpath " Try { $data = Import-PowerShellDataFile -Path $config -ErrorAction Stop } Catch { Throw $_ } if ($data.NonNodeData.Lability.DSCResource) { $dsc = $data.NonNodeData.Lability.DSCResource Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Found $($dsc.count) required DSC resources" $dsc.GetEnumerator() | ForEach-Object { $installed = Get-Module $_.name -ListAvailable [pscustomobject]@{ PSTypeName = 'PSAutoLabResource' ModuleName = $_.Name RequiredVersion = $_.RequiredVersion Installed = $installed.version -contains $_.requiredVersion InstalledVersions = $Installed.version Configuration = $configName } } } else { Write-Warning "No DSC Resources found in $config." } } #process End { Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)" } #end } #close Test-LabDSCResource #endregion |