|
1 | 1 | $InformationPreference = 'Continue' |
2 | 2 | $ErrorActionPreference = 'Stop' |
3 | 3 |
|
4 | | -function Set-CACert |
5 | | -{ |
6 | | - $uri = 'https://curl.haxx.se/ca/cacert.pem' |
7 | | - $CACertFile = Join-Path -Path $ENV:AppData -ChildPath 'RubyCACert.pem' |
8 | | - |
9 | | - $retryArgs = @{ |
10 | | - SuccessMessage = "Succeeded in downloading CA bundle from $uri" |
11 | | - FailMessage = "Failed to download CA bundle from $uri" |
12 | | - Retries = 5 |
13 | | - Timeout = 1 |
14 | | - Script = { |
15 | | - [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 |
16 | | - Invoke-WebRequest -Uri $uri -UseBasicParsing -OutFile $CACertFile | Out-Null |
17 | | - } |
18 | | - } |
19 | | - |
20 | | - # only download CA file if not present - throw on failures |
21 | | - If (-Not (Test-Path -Path $CACertFile)) { Invoke-ScriptBlockWithRetry @retryArgs } |
22 | | - |
23 | | - Write-Information "Setting CA Certificate store set to $CACertFile.." |
24 | | - $ENV:SSL_CERT_FILE = $CACertFile |
25 | | - [System.Environment]::SetEnvironmentVariable('SSL_CERT_FILE', $CACertFile, [System.EnvironmentVariableTarget]::Machine) |
26 | | -} |
27 | | - |
28 | | -function Install-Puppetfile |
29 | | -{ |
30 | | - Set-CACert |
31 | | - |
32 | | - # Forge connections may fail intermittently |
33 | | - $retryArgs = @{ |
34 | | - SuccessMessage = 'Succeeded in installing Puppetfile' |
35 | | - FailMessage = 'Failed to install required modules from Forge' |
36 | | - Retries = 10 |
37 | | - Timeout = 2 |
38 | | - Script = { bundle exec r10k puppetfile install } |
39 | | - } |
40 | | - |
41 | | - Invoke-ScriptBlockWithRetry @retryArgs |
42 | | -} |
43 | | - |
44 | | -function New-RandomPassword |
45 | | -{ |
46 | | - Add-Type -AssemblyName System.Web |
47 | | - "&aA4" + [System.Web.Security.Membership]::GeneratePassword(10, 3) |
48 | | -} |
49 | | - |
50 | | -function New-LocalAdmin($userName, $password) |
51 | | -{ |
52 | | - $userArgs = @{ |
53 | | - Name = $userName |
54 | | - Password = (ConvertTo-SecureString -String $password -Force -AsPlainText) |
55 | | - } |
56 | | - |
57 | | - $user = New-LocalUser @userArgs |
58 | | - Write-Information ($user | Format-List | Out-String) |
59 | | - Add-LocalGroupMember -Group 'Remote Management Users' -Member $user |
60 | | - Add-LocalGroupMember -Group Administrators -Member $user |
61 | | -} |
62 | | - |
63 | | -function Install-Certificate($path, $password) |
64 | | -{ |
65 | | - $importArgs = @{ |
66 | | - FilePath = $path |
67 | | - CertStoreLocation = 'cert:\\LocalMachine\\My' |
68 | | - Password = (ConvertTo-SecureString -String $password -Force -AsPlainText) |
69 | | - } |
70 | | - |
71 | | - return (Import-PfxCertificate @importArgs) |
72 | | -} |
73 | | - |
74 | | -#function Grant-WinRMHttpsAccess($certThumbprint) |
75 | | -#{ |
76 | | -# $winRMArgs = @{ |
77 | | -# ResourceURI = 'winrm/config/Listener' |
78 | | -# SelectorSet = @{ Address = '*'; Transport = 'HTTPS'; } |
79 | | -# ValueSet = @{ Hostname = 'boltserver'; CertificateThumbprint = $certThumbprint } |
80 | | -# } |
81 | | -# $instance = Set-WSManInstance @winRMArgs |
82 | | -# Write-Information ($instance | Format-List | Out-String) |
83 | | -#} |
84 | | - |
85 | | -#function Set-WinRMHostConfiguration |
86 | | -#{ |
87 | | -# # configure WinRM to use cert.pfx for SSL |
88 | | -# $cert = Install-Certificate -Path 'spec/fixtures/ssl/cert.pfx' -Password 'bolt' |
89 | | -# Write-Information ($cert | Format-List | Out-String) |
90 | | -# Grant-WinRMHttpsAccess -CertThumbprint $cert.Thumbprint |
91 | | -#} |
92 | | - |
93 | | -function Invoke-ScriptBlockWithRetry([ScriptBlock]$script, $failMessage, $successMessage, $retries = 15, $timeout = 1) |
94 | | -{ |
95 | | - $retried = 0 |
96 | | - |
97 | | - Do |
98 | | - { |
99 | | - try { |
100 | | - $script.Invoke() |
101 | | - Write-Information "$successMessage after $($retried + 1) attempt(s)" |
102 | | - return $true |
103 | | - } |
104 | | - catch |
105 | | - { |
106 | | - $retried++ |
107 | | - Start-Sleep -Seconds $timeout |
108 | | - } |
109 | | - } While ($retried -lt $retries) |
110 | | - |
111 | | - throw "ERROR: $failMessage in $retried retries`n$($Error[0])" |
112 | | - |
113 | | -} |
114 | | - |
115 | | -#function Test-WinRMConfiguration($userName, $password, $retries = 15, $timeout = 1) |
116 | | -#{ |
117 | | -# $retryArgs = @{ |
118 | | -# FailMessage = 'Failed to establish WinRM connection over SSL' |
119 | | -# SuccessMessage = "Successfully established WinRM connection with $userName" |
120 | | -# Retries = $retries |
121 | | -# Timeout = $timeout |
122 | | -# Script = { |
123 | | -# $pass = ConvertTo-SecureString $password -AsPlainText -Force |
124 | | -# $sessionArgs = @{ |
125 | | -# ComputerName = 'localhost' |
126 | | -# Credential = New-Object System.Management.Automation.PSCredential ($userName, $pass) |
127 | | -# UseSSL = $true |
128 | | -# SessionOption = New-PSSessionOption -SkipRevocationCheck -SkipCACheck |
129 | | -# } |
130 | | -# |
131 | | -# if (New-PSSession @sessionArgs) { return $true } |
132 | | -# } |
133 | | -# } |
134 | | -# |
135 | | -# Invoke-ScriptBlockWithRetry @retryArgs |
136 | | -#} |
137 | | - |
138 | | -# Ensure Puppet Ruby 5 / 6 takes precedence over system Ruby |
139 | | -function Set-ActiveRubyFromPuppet |
140 | | -{ |
141 | | - # https://github.com/puppetlabs/puppet-specifications/blob/master/file_paths.md |
142 | | - $path = @( |
143 | | - "${ENV:ProgramFiles}\Puppet Labs\Puppet\sys\ruby\bin", |
144 | | - "${ENV:ProgramFiles}\Puppet Labs\Puppet\puppet\bin", |
145 | | - $ENV:Path |
146 | | - ) -join ';' |
147 | | - |
148 | | - [System.Environment]::SetEnvironmentVariable('Path', $path, [System.EnvironmentVariableTarget]::Machine) |
149 | | -} |
150 | | - |
151 | | -$Pass = New-RandomPassword |
152 | | -$User = @{ UserName = $ENV:BOLT_WINRM_USER; Password = $Pass } |
153 | | -New-LocalAdmin @User |
154 | | -#Enable-PSRemoting |
155 | | -#Set-WSManQuickConfig -Force |
156 | | -#Set-WinRMHostConfiguration |
157 | | -#Test-WinRMConfiguration @User | Out-Null |
158 | | -#Write-Output "::set-env name=BOLT_WINRM_PASSWORD::$pass" |
| 4 | +Enable-PSRemoting |
| 5 | +winrm "set" "winrm/config/client/auth" "@{Basic=`"true`"}" |
| 6 | +winrm "set" "winrm/config/client" "@{AllowUnencrypted=`"true`"}" |
0 commit comments