mirror of https://github.com/hashicorp/packer
Powershell Provisioner Error Handling (#13334)
* WIP Testing Approach * WIP Testing Approach * WIP Testing Approach Work * WIP Testing Approach * adding acceptance test for windows Amazon EBS. * modify wrapper string to use Set-Variable * fixing unit tests * cleanup * updated approach to use -file instead of inline powershell execution. * adding more scenarios for acceptance test. * using writeString for file directly. * changing variable name * updating test case. cleanup. * updating test case * updating test case * updating test case - nested try catch * adding unit test for None Execution Policy * adding unit test for None Execution Policy * fix test casepull/13333/merge
parent
fe6eba27f2
commit
089df02532
@ -0,0 +1,76 @@
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": ["invalid-cmdlet"],
|
||||
"valid_exit_codes": ["1"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": ["#Requires -Version 10.0"],
|
||||
"valid_exit_codes": ["1"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": ["exit 1"],
|
||||
"valid_exit_codes": ["1"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": ["}}"],
|
||||
"valid_exit_codes": ["1"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": ["$LASTEXITCODE=1"],
|
||||
"valid_exit_codes": ["1"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": ["throw 'XXX'"],
|
||||
"valid_exit_codes": ["1"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"script": "../../provisioner/powershell/test-fixtures/scripts/set_version_latest.ps1",
|
||||
"valid_exit_codes": ["0"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"elevated_user": "Administrator",
|
||||
"elevated_password": "{{.WinRMPassword}}",
|
||||
"inline": "Get-ItemProperty -Path HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
|
||||
"valid_exit_codes": ["0"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": "ping invalidhost",
|
||||
"valid_exit_codes": ["1"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": "sc.exe start command",
|
||||
"valid_exit_codes": ["1060"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": "echo 'Hi testing echo'; invalid command!; echo 'Another valid command';",
|
||||
"valid_exit_codes": ["1"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": ["$ErrorActionPreference='Stop'", "Get-Item 'C:\\nonexistent.txt'"],
|
||||
"valid_exit_codes": ["1"]
|
||||
},
|
||||
{
|
||||
"type": "powershell",
|
||||
"inline": [
|
||||
"try {",
|
||||
" invalid command",
|
||||
"} catch {",
|
||||
" exit 1",
|
||||
"}"
|
||||
],
|
||||
"valid_exit_codes": ["1"]
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,40 @@
|
||||
<powershell>
|
||||
# Set administrator password
|
||||
net user Administrator SuperS3cr3t!!!!
|
||||
wmic useraccount where "name='Administrator'" set PasswordExpires=FALSE
|
||||
|
||||
# First, make sure WinRM can't be connected to
|
||||
netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" new enable=yes action=block
|
||||
|
||||
# Delete any existing WinRM listeners
|
||||
winrm delete winrm/config/listener?Address=*+Transport=HTTP 2>$Null
|
||||
winrm delete winrm/config/listener?Address=*+Transport=HTTPS 2>$Null
|
||||
|
||||
# Disable group policies which block basic authentication and unencrypted login
|
||||
|
||||
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\WinRM\Client -Name AllowBasic -Value 1
|
||||
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\WinRM\Client -Name AllowUnencryptedTraffic -Value 1
|
||||
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\WinRM\Service -Name AllowBasic -Value 1
|
||||
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\WinRM\Service -Name AllowUnencryptedTraffic -Value 1
|
||||
|
||||
|
||||
# Create a new WinRM listener and configure
|
||||
winrm create winrm/config/listener?Address=*+Transport=HTTP
|
||||
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="0"}'
|
||||
winrm set winrm/config '@{MaxTimeoutms="7200000"}'
|
||||
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
|
||||
winrm set winrm/config/service '@{MaxConcurrentOperationsPerUser="12000"}'
|
||||
winrm set winrm/config/service/auth '@{Basic="true"}'
|
||||
winrm set winrm/config/client/auth '@{Basic="true"}'
|
||||
|
||||
# Configure UAC to allow privilege elevation in remote shells
|
||||
$Key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System'
|
||||
$Setting = 'LocalAccountTokenFilterPolicy'
|
||||
Set-ItemProperty -Path $Key -Name $Setting -Value 1 -Force
|
||||
|
||||
# Configure and restart the WinRM Service; Enable the required firewall exception
|
||||
Stop-Service -Name WinRM
|
||||
Set-Service -Name WinRM -StartupType Automatic
|
||||
netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" new action=allow localip=any remoteip=any
|
||||
Start-Service -Name WinRM
|
||||
</powershell>
|
||||
@ -0,0 +1,13 @@
|
||||
# Test fixture is a modified version of the example found at
|
||||
# https://www.powershellmagazine.com/2012/10/23/pstip-set-strictmode-why-should-you-care/
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$myNumbersCollection = 1..5
|
||||
if($myNumbersCollection -contains 3) {
|
||||
"collection contains 3"
|
||||
}
|
||||
else {
|
||||
"collection doesn't contain 3"
|
||||
}
|
||||
Loading…
Reference in new issue