-
Notifications
You must be signed in to change notification settings - Fork 11
/
runonce.ps1
executable file
·114 lines (93 loc) · 3.13 KB
/
runonce.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
#!/usr/bin/powershell
#
# Reused from the StackOverflow article. Solution by Dennis Williamson
#
# Place this file in /root/Framework_Scripts/
# Create directory /root//runonce.d
# Add the line "@reboot root /root/Framework_Scripts/runonce.ps1" to /etc/crontab
#
# When there's a script you want to run at the next boot, put it in /root/runonce.d.
#
# Author: John W. Fawcett, Principal Software Development Engineer, Microsoft
#
function callItIn($c, $m) {
$output_path="c:\temp\progress_logs\$c"
$m | out-file -Append $output_path
return
}
$global:isHyperV = $false
function phoneHome($m) {
if ($global:isHyperV -eq $true) {
invoke-command -session $s -ScriptBlock ${function:callItIn} -ArgumentList $c,$m
if ($? -eq $false)
{
#
# Error on ps. Try reconnecting.
#
Exit-PSSession $s
$o = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck
$pw=convertto-securestring -AsPlainText -force -string 'P@ssW0rd-'
$cred=new-object -typename system.management.automation.pscredential -argumentlist "mstest",$pw
$s=new-PSSession -computername lis-f1637.redmond.corp.microsoft.com -credential $cred -authentication Basic -SessionOption $o
}
} else {
$output_path="/opt/microsoft/borg_progress.log"
$m | out-file -Append $output_path
}
}
#
# Give the machine 30 seconds to settle down
#
sleep 30
echo "Checking for platform..."
$global:isHyperV=$true
$lookup=nslookup cdmbuildsna01.redmond.corp.microsoft.com
if ($? -eq $false) {
$global:isHyperV = $false
echo "It looks like we're in Azure"
} else {
echo "It looks like we're in Hyper-V"
$o = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck
$pw=convertto-securestring -AsPlainText -force -string 'P@ssW0rd-'
$cred=new-object -typename system.management.automation.pscredential -argumentlist "mstest",$pw
$s=new-PSSession -computername lis-f1637.redmond.corp.microsoft.com -credential $cred -authentication Basic -SessionOption $o
}
#
# What OS are we on?
#
$linuxInfo = Get-Content /etc/os-release -Raw | ConvertFrom-StringData
# $c = $linuxInfo.ID
# $c=$c -replace '"',""
$ourHost=hostname
$c=$ourHost + "_RunOnce"
phoneHome "RunOnce starting up on machine $c"
#
# Check for the runonce directory
#
if ((Test-Path /root/runonce.d) -eq 0) {
echo "No runonce directory found"
$LASTEXITCODE = 1
exit $LASTERRORCODE
}
#
# If there are entries, execute them....
#
$scriptsArray=@()
Get-ChildItem /root/runonce.d -exclude ran |
foreach-Object {
$script=$_.Name
echo "Found script $script"
phoneHome "RunOnce has located $script in execution folder"
$fullName='/root/runonce.d/ran/'+$script
Move-Item -force $_ $fullName
$scriptsArray+=$fullName
phoneHome "Script has been copied to staging folder"
}
foreach ($script in $scriptsArray) {
phoneHome "RunOnce initiating execution of script $script from staging folder"
iex $script
phoneHome "RunOnce execution of script $script complete"
}
if ($global:isHyperV -eq $false) {
remove-pssession $s
}