Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for scripts 1 and 2 #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 62 additions & 65 deletions Scripts/1_Prereq.ps1
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
# Verify Running as Admin
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
If (!( $isAdmin )) {
Write-Host "-- Restarting as Administrator" -ForegroundColor Cyan ; Sleep -Seconds 1
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}

# Skipping 10 lines because if running when all prereqs met, statusbar covers powershell output

<#
.Synopsis
Preps for the creation of the Lab VMs
.Description
Creates the required directories; downloads the tools diskspd.exe, convert-windowsimage.ps1, and the required DSC resources
.EXAMPLE
.\1_Prereq.ps1
.EXAMPLE
.\1_Prereq.ps1 -verbose
#>
#Requires -RunAsAdministrator
[CmdletBinding()]
param()

Write-Verbose -Message "Skipping 10 lines because if running when all prereqs met, statusbar covers powershell output"
1..10 |% { Write-Host ""}

# Functions
function Get-WindowsBuildNumber {
$os = Get-WmiObject -Class Win32_OperatingSystem
return [int]($os.BuildNumber)
}
Write-Verbose -Message "Loading Functions"
. .\functions.ps1


# Get workdirectory and Start Time
$workdir = Split-Path $script:MyInvocation.MyCommand.Path
Start-Transcript -Path $workdir'\Prereq.log'
Write-Verbose -Message "Get workdirectory and Start Time"
$workdir = Get-ScriptDirectory
Start-Transcript -Path $workdir'\Prereq.log' -Append
$StartDateTime = get-date
Write-host "Script started at $StartDateTime"

# Checking for Compatible OS
Write-Host "Checking if OS is Windows 10 TH2/Server 2016 TP4 or newer" -ForegroundColor Cyan
Write-Information -MessageData "Script started at $StartDateTime" -InformationAction Continue

Write-Verbose -Message "Checking if OS is Windows 10 TH2/Server 2016 TP4 or newer"
$BuildNumber=Get-WindowsBuildNumber
if ($BuildNumber -ge 10586){
Write-Host "`t OS is Windows 10 TH2/Server 2016 TP4 or newer" -ForegroundColor Green
Write-Verbose -Message "OS is Windows 10 TH2/Server 2016 TP4 or newer"
}else{
Write-Host "`t Windows 10/ Server 2016 not detected. Exiting" -ForegroundColor Red
Write-Host "Press any key to continue ..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()
Exit
Write-Error "Windows 10/ Server 2016 not detected. Exiting"
Exit
}

# Checking Folder Structure
Write-Verbose -Message "Checking Folder Structure"
'OSClient','OSServer','Tools\DSC','Tools\ToolsVHD\DiskSpd','OSServer\Packages','OSClient\Packages' | ForEach-Object {
if (!( Test-Path "$Workdir\$_" )) { New-Item -Type Directory -Path "$workdir\$_" } }

Expand All @@ -52,72 +48,73 @@ If (!( Test-Path -Path "$workdir\OSServer\Packages\Copy_MSU_or_Cab_packages_here

If (!( Test-Path -Path "$workdir\OSClient\Packages\Copy_MSU_or_Cab_packages_here.txt" )) {
New-Item -Path "$workdir\OSClient\Packages\" -Name Copy_MSU_or_Cab_packages_here.txt -ItemType File }
# Downloading diskspd if its not in tools folder
Write-Verbose -Message "Downloading diskspd if its not in tools folder"
Switch ( Test-Path -Path "$workdir\Tools\ToolsVHD\DiskSpd\diskspd.exe" ) {
$false {
Write-Host "Downloading diskspd" -ForegroundColor Cyan
Write-Information -MessageData "Downloading diskspd" -InformationAction Continue
try{
$webcontent = Invoke-WebRequest -Uri aka.ms/diskspd
$downloadurl = $webcontent.BaseResponse.ResponseUri.AbsoluteUri.Substring(0,$webcontent.BaseResponse.ResponseUri.AbsoluteUri.LastIndexOf('/'))+($webcontent.Links | where-object { $_.'data-url' -match '/Diskspd.*zip$' }|Select-Object -ExpandProperty "data-url")
Invoke-WebRequest -Uri $downloadurl -OutFile "$workdir\Tools\ToolsVHD\DiskSpd\diskspd.zip"
}catch{
Write-Error -Message "Failed to download diskspd!"
}
Write-Verbose -Message "Unnzipping and extracting just diskspd.exe x64"
Expand-Archive "$workdir\Tools\ToolsVHD\DiskSpd\diskspd.zip" -DestinationPath "$workdir\Tools\ToolsVHD\DiskSpd\Unzip"
Copy-Item -Path (Get-ChildItem -Path "$workdir\tools\toolsvhd\diskspd\" -Recurse | Where-Object {$_.Directory -like '*amd64fre*' -and $_.name -eq 'diskspd.exe' }).fullname -Destination "$workdir\Tools\ToolsVHD\DiskSpd\"
Remove-Item -Path "$workdir\Tools\ToolsVHD\DiskSpd\diskspd.zip"
Remove-Item -Path "$workdir\Tools\ToolsVHD\DiskSpd\Unzip" -Recurse -Force

$webcontent = Invoke-WebRequest -Uri aka.ms/diskspd
$downloadurl = $webcontent.BaseResponse.ResponseUri.AbsoluteUri.Substring(0,$webcontent.BaseResponse.ResponseUri.AbsoluteUri.LastIndexOf('/'))+($webcontent.Links | where-object { $_.'data-url' -match '/Diskspd.*zip$' }|Select-Object -ExpandProperty "data-url")
Invoke-WebRequest -Uri $downloadurl -OutFile "$workdir\Tools\ToolsVHD\DiskSpd\diskspd.zip"
# Unnzipping and extracting just diskspd.exe x64
Expand-Archive "$workdir\Tools\ToolsVHD\DiskSpd\diskspd.zip" -DestinationPath "$workdir\Tools\ToolsVHD\DiskSpd\Unzip"
Copy-Item -Path (Get-ChildItem -Path "$workdir\tools\toolsvhd\diskspd\" -Recurse | Where-Object {$_.Directory -like '*amd64fre*' -and $_.name -eq 'diskspd.exe' }).fullname -Destination "$workdir\Tools\ToolsVHD\DiskSpd\"
Remove-Item -Path "$workdir\Tools\ToolsVHD\DiskSpd\diskspd.zip"
Remove-Item -Path "$workdir\Tools\ToolsVHD\DiskSpd\Unzip" -Recurse -Force
}
}

# Download convert-windowsimage if its not in tools folder
Write-Verbose -Message "Download convert-windowsimage if its not in tools folder"
Switch ( Test-Path -Path "$workdir\Tools\convert-windowsimage.ps1" ) {
$false {
Write-Host "Downloading Convert-WindowsImage" -ForegroundColor Cyan
Invoke-WebRequest -Uri https://raw.githubusercontent.com/Microsoft/Virtualization-Documentation/master/hyperv-tools/Convert-WindowsImage/Convert-WindowsImage.ps1 -OutFile "$workdir\Tools\convert-windowsimage.ps1"
Write-Information -MessageData "Downloading Convert-WindowsImage" -InformationAction Continue
try {
Invoke-WebRequest -Uri https://raw.githubusercontent.com/Microsoft/Virtualization-Documentation/master/hyperv-tools/Convert-WindowsImage/Convert-WindowsImage.ps1 -OutFile "$workdir\Tools\convert-windowsimage.ps1"
}
catch {
Write-Error -Message "Failed to download Convert-WindowsImage!"
}
}
}

# Specify needed modules and required version
Write-Verbose -Message "Specify needed modules and required version"
$modules=("xActiveDirectory","2.10.0.0"),("xDHCpServer","1.3.0.0"),("xNetworking","2.8.0.0"),("xPSDesiredStateConfiguration","3.9.0.0")

# Downloading modules into Tools folder if needed.
Write-Verbose -Message "Downloading modules into Tools folder if needed."
foreach ($module in $modules){
#testing if modules are present
Write-Host "Testing if modules are present" -ForegroundColor Cyan
Write-Verbose -Message "Testing if modules are present"
$modulename=$module[0]
$moduleversion=$module[1]
if (!(Test-Path $workdir'\Tools\DSC\'$modulename'\')){
Write-Host "Module $module not found... Downloading"
### Install NuGET package provider ###
if ((Get-PackageProvider -Name NuGet) -eq $null){
Write-Information -MessageData "Module $module not found... Downloading"
if ((Get-PackageProvider -Name NuGet) -eq $null){
Write-Verbose -Message "Install NuGET package provider"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
}
Find-DscResource -moduleName $modulename -RequiredVersion $moduleversion | Save-Module -Path $workdir'\Tools\DSC'
}else{
Write-Host "Module $modulename version found... Skipping Download"
Write-Information -MessageData "Module $modulename version found... Skipping Download" -InformationAction Continue
}
}

# Installing modules if needed
Write-Verbose -Message "Installing modules if needed"
foreach ($module in $modules) {
Write-Host "Testing DSC Module $module Presence" -ForegroundColor Cyan
# Check if Module is installed
Write-Information -MessageData "Testing DSC Module $module Presence"
if ((Get-DscResource -Module $Module[0] | where-object {$_.version -eq $module[1]}) -eq $Null) {
# module is not installed - install it
Write-Host "Module $module will be installed"
Write-Information -MessageData "Module $module will be installed" -InformationAction Continue
$modulename=$module[0]
$moduleversion=$module[1]
Copy-item -Path "$workdir\Tools\DSC\$modulename" -Destination "C:\Program Files\WindowsPowerShell\Modules" -Recurse -Force
Write-Host "Module was installed." -ForegroundColor Green
Write-Information -MessageData "Module was installed." -InformationAction Continue
Get-DscResource -Module $modulename
Write-Host ""
} else {
# module is already installed
Write-Host "Module $Module is already installed"
Write-Host ""
Write-Information -MessageData "Module $Module is already installed" -InformationAction Continue
}
}
# finishing
Write-Host "Script finished at $(Get-date) and took $(((get-date) - $StartDateTime).TotalMinutes) Minutes"
Stop-Transcript
Write-Host "Press any key to continue..." -ForegroundColor Green
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
Write-Information -MessageData "Script finished at $(Get-date) and took $(((get-date) - $StartDateTime).TotalMinutes) Minutes" -InformationAction Continue
Stop-Transcript
Loading