Configurations/PowerShellLab/PostSetup/RunAll.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
#requires -version 5.1

[cmdletbinding()]

Param(
    #run the script without any prompting
    [Switch]$Force
)
#this is here in case the script is aborted from previous runs
[void]$PSDefaultParameterValues.remove("write-host:foregroundcolor")

if (-Not $Force) {
    $msg = @"
 
 This control script will run all post update tasks on virtual machines that are
 part of the PowerShellLab Autolab configuration. It is assumed you have not
 modified anything in the configuration files.
 
 The script will do the following:
   - Download Git on the Win10 client
   - Download Sysinternals tools on the Win10 client
   - Install VSCode on the Win10 client
   - Update PowerShell help on the Win10 client
   - Reboot all virtual machines
 
 Before running this script, all configuration setup must be complete. You
 must also have performed an interactive logon on the Windows 10 client with the
 Company\Administrator account to complete the Windows 10 setup.
 
 Once the profile is complete, you can log off.
 
 If any of the commands fail, try running it separately.
 
 Make sure all virtual machines are running before continuing.
 
   $(Get-VM Win10,SRV1,SRV2,SRV3,DOM1 | Out-String)
"@


Clear-Host
Write-Host $msg -ForegroundColor Cyan

$coll = @()
$coll+= [System.Management.Automation.Host.ChoiceDescription]::new("Yes &Y")
$coll+= [System.Management.Automation.Host.ChoiceDescription]::new("No &N")
$r = $host.ui.PromptForChoice("Do you want to continue with this script?","",$coll,1)
If ($r -eq 1) {
    return
}
} #if not using -Force

#set a default color for the progress messages
$PSDefaultParameterValues.add("write-host:foregroundcolor","yellow")
Get-Job | Remove-Job -force

$pass = ConvertTo-SecureString -String "P@ssw0rd" -AsPlainText -Force
#credential for domain administrator
$admin = New-Object PSCredential "company\administrator",$pass
#password for SRV3 which is in a workgroup
$wg = New-Object PSCredential "srv3\administrator",$pass

Write-Host "[$(Get-Date -format T)] Starting the post process"
Write-Host "[$(Get-Date -format T)] Creating PSSessions"

#create PSSessions. Some of these variables are un-used at this time
$all = @()
$srv3 = New-PSSession -VMName SRV3 -Credential $wg
$all+=$srv3
$win10 = New-PSSession -VMName Win10 -Credential $admin
$all+=$win10
$servers = New-PSSession -VMName SRV1,SRV2,DOM1 -Credential $admin
$all+=$servers

Write-Host "[$(Get-Date -format T)] Waiting for sessions to become available"
Do {
    Start-Sleep -Seconds 1
} Until ( ($all| Get-PSSession).Availability -notcontains 'busy')

Write-Host "[$(Get-Date -format T)] Waiting for Win10 session to become available"

Do {
    Start-Sleep -Seconds 1
} Until ( ($win10|Get-PSSession).Availability -eq 'available')
Write-Host "[$(Get-Date -format T)] Install SysInternals on Windows 10"
&$PSScriptRoot\Install-Sysinternals.ps1 -Session $win10 | Out-Null

Write-Host "[$(Get-Date -format T)] Waiting for Win10 session to become available"
Do {
    Start-Sleep -Seconds 1
} Until ( ($win10|Get-PSSession).Availability -eq 'available')
Write-Host "[$(Get-Date -format T)] Downloading Git on Windows 10"
&$PSScriptRoot\Download-Git.ps1 -session $win10 | Out-Null

Write-Host "[$(Get-Date -format T)] Waiting for Win10 session to become available"
Do {
    Start-Sleep -Seconds 1
} Until ( ($win10|Get-PSSession).Availability -eq 'available')

<#
#add some name resolution calls to help with errors I'm getting about
#being unable to resolve the redirected name
Invoke-Command {
        Resolve-DnsName go.microsoft.com | Out-Null
        Resolve-DnsName azurewebsites.net | out-null
        Resolve-DnsName az764295.vo.msecnd.net | out-null
} -Session $all
#>

Write-Host "[$(Get-Date -format T)] Installing VSCode on Windows 10"
#for some unknown reason this fails when using an existing session
# .\install-vscode -session $win10 | Out-Null
&$PSScriptRoot\install-vscode.ps1 -VMName Win10 -Credential $admin | Out-Null

Write-Host "[$(Get-Date -format T)] Updating PowerShell help on Windows 10"
Do {
    Start-Sleep -Seconds 1
} Until ( ($win10 | Get-PSSession).Availability -eq 'available')
#update help and suppress all error messages
Invoke-Command { Update-Help -force -ErrorAction SilentlyContinue } -session $win10

Write-Host "[$(Get-Date -format T)] Waiting for Windows Update Jobs to complete"
Wait-Job WinUp*

Write-Host "[$(Get-Date -format T)] Stopping Windows 10"
Stop-VM Win10 -AsJob | Wait-Job | Out-Null

Write-Host "[$(Get-Date -format T)] Stopping member servers"
Get-VM SRV1,SRV2,SRV3 | Stop-VM -Force -AsJob

Write-Host "[$(Get-Date -format T)] Stopping DOM1"
Stop-VM DOM1 -AsJob | Wait-Job | Out-Null

Write-Host "[$(Get-Date -format T)] Restarting all virtual machines"
Start-VM SRV3,DOM1 -asjob | Wait-Job | Out-Null
Start-VM SRV1,SRV2,Win10 -asjob | Out-Null

#clean up
$all | Remove-PSSession
$PSDefaultParameterValues.remove("write-host:foregroundcolor")

Write-Host "[$(Get-Date -format T)] Ending the post process script" -ForegroundColor green