@@ -15,57 +15,81 @@ vm_ipath=${STEMBUILD_CONSTRUCT_TARGET_VM}
1515vm_username=" ${VM_USERNAME} "
1616vm_password=" ${VM_PASSWORD} "
1717
18- powershell_exe=" \\ Windows\\ System32\\ WindowsPowerShell\\ V1.0\\ powershell.exe"
18+ function start_powershell_command() {
19+ local powershell_command=" ${1} "
20+
21+ echo " Starting '${powershell_command} '" >&2
22+ govc guest.start \
23+ -vm.ipath=" ${vm_ipath} " \
24+ -l=" ${vm_username} :${vm_password} " \
25+ " \\ Windows\\ System32\\ WindowsPowerShell\\ V1.0\\ powershell.exe" \
26+ " ${powershell_command} "
27+ }
1928
20- govc_pwsh_cmd=" govc guest.start -vm.ipath=${vm_ipath} -l=${vm_username} :${vm_password} ${powershell_exe} "
29+ function get_powershell_pid_exit_code() {
30+ local powershell_pid=" ${1} "
2131
22- # get wu-install /wu-update set up to work on the vm...
32+ echo " Getting exit code for ${powershell_pid} " >&2
33+ # -X blocks until the guest process exits
34+ govc guest.ps \
35+ -vm.ipath=" ${vm_ipath} " \
36+ -l=" ${vm_username} :${vm_password} " \
37+ -p=" ${powershell_pid} " \
38+ -X -json \
39+ | jq ' .processInfo[0].exitCode'
40+ }
41+
42+ function download_remote_file() {
43+ local remote_path=" ${1} "
44+ local local_path=" ${2} "
45+
46+ govc guest.download \
47+ -l " ${vm_username} :${vm_password} " \
48+ -vm=" ${vm_ipath} " \
49+ " ${remote_path} " " ${local_path} "
50+ }
51+
52+ function run_powershell_command_with_logging() {
53+ local powershell_command=" ${1} "
54+
55+ pid=$( start_powershell_command " ${powershell_command} " )
56+ echo " Started '${powershell_command} ' with pid '${pid} '" >&2
57+
58+ exit_code=$( get_powershell_pid_exit_code " ${pid} " )
59+ echo " Exited '${powershell_command} ' with exit code '${exit_code} '" >&2
60+ }
2361
2462function wait_for_vm_to_come_up() {
2563 result=-1
2664 set +e
2765 while [[ result -ne 0 ]]; do
28- # try to connect
29- $govc_pwsh_cmd Get-ChildItem \\ 2> /dev/null
66+ start_powershell_command Get-ChildItem \\ 2> /dev/null # try to connect
3067 result=$?
3168 sleep 1
3269 done
3370 set -e
3471}
3572
36- function run_powershell_command_with_logging() {
37- command=$1
38- echo " Running $command "
39- pid=$(
40- ${govc_pwsh_cmd} " ${command} "
41- )
42- return=$( govc guest.ps -vm.ipath=" ${vm_ipath} " -l=" ${vm_username} :${vm_password} " -p=" ${pid} " -X -json | jq ' .processInfo[0].exitCode' )
43- echo " ${command} returned ${return} "
73+ function get_windows_updates_remaining() {
74+ # run powershell command that "exits" with the Count returned by Get-WindowsUpdate
75+ get_update_count_pid=" $( start_powershell_command " exit (([array](Get-WindowsUpdate)).Count)" ) "
76+
77+ get_powershell_pid_exit_code " ${get_update_count_pid} "
4478}
4579
4680wait_for_vm_to_come_up
4781
82+ # get wu-install /wu-update set up to work on the vm...
4883run_powershell_command_with_logging ' Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'
4984run_powershell_command_with_logging ' Install-Module -Name PSWindowsUpdate -MinimumVersion 2.1.0.1 -Force'
5085
51- returnWindowsUpdateCount=" exit (([array](Get-WindowsUpdate)).Count)"
52- echo " getting update count"
53- get_update_count_pid=$( ${govc_pwsh_cmd} " ${returnWindowsUpdateCount} " )
5486echo " getting update count exit code via guest.ps"
55- updates_remaining=$( govc guest.ps -vm.ipath= " ${vm_ipath} " -l= " ${vm_username} : ${vm_password} " -p= " ${get_update_count_pid} " -X -json | jq ' .processInfo[0].exitCode ' )
87+ updates_remaining=$( get_windows_updates_remaining )
5688
57- echo " Windows Updates to install: $updates_remaining "
89+ echo " Windows Updates to install: ${ updates_remaining} "
5890while [[ updates_remaining -ne 0 ]]; do
59- install_update_pid=$(
60- ${govc_pwsh_cmd} Install-WindowsUpdate -AcceptAll -AutoReboot
61- )
62- echo " install-WU pid is $install_update_pid "
63-
64-
65- # ignore unreachable agent if the vm just went down for reboot
66- # -X blocks until the guest process exits
67- set +e
68- govc guest.ps -vm.ipath=" ${vm_ipath} " -l=" ${vm_username} :${vm_password} " -p=" ${install_update_pid} " -X
91+ set +e # ignore unreachable agent if the vm just went down for reboot
92+ run_powershell_command_with_logging " Install-WindowsUpdate -AcceptAll -AutoReboot"
6993 set -e
7094 echo " Install-WU done"
7195
@@ -78,17 +102,17 @@ while [[ updates_remaining -ne 0 ]]; do
78102 updates_remaining=
79103 while [[ -z " ${updates_remaining} " ]] ; do
80104 echo " Trying to discover how many updates remain..."
81- # ignore failures here since the vmware tools agent may be down while updates are being applied
82- set +e
83- get_update_count_pid=$( ${govc_pwsh_cmd} " ${returnWindowsUpdateCount} " )
84- updates_remaining=$( govc guest.ps -vm.ipath=" ${vm_ipath} " -l=" ${vm_username} :${vm_password} " -p=" ${get_update_count_pid} " -X -json | jq ' .processInfo[0].exitCode' )
105+ set +e # ignore failures here since the vmware tools agent may be down while updates are being applied
106+ updates_remaining=$( get_windows_updates_remaining)
85107 set -e
86108 done
87109 echo " Updates remaining: ${updates_remaining} "
88110done
89111
90- run_powershell_command_with_logging " Get-Hotfix > C:\\ hotfix.log"
112+ remote_hotfix_log_path=" C:\\ hotfix.log"
113+
114+ run_powershell_command_with_logging " Get-Hotfix > ${remote_hotfix_log_path} "
91115
92- govc guest.download -l " ${vm_username} : ${vm_password} " -vm= " ${vm_ipath} " " C: \\ hotfix.log " hotfix-log/hotfixes.log
116+ download_remote_file " ${remote_hotfix_log_path} " hotfix-log/hotfixes.log
93117
94118run_powershell_command_with_logging " Dism.exe /online /Cleanup-Image /StartComponentCleanup"
0 commit comments