Last active
December 25, 2020 23:15
-
-
Save akomakom/1842c5372d75114520a0a5dec46e83ae to your computer and use it in GitHub Desktop.
Set up windows host for ansible remoting including upgrading powershell and rebooting if necessary (reboots only if PS requires upgrade)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script has been tested on Windows server 2008, 2012, 2016 and Windows 10. | |
# This script can be run from puppet using puppetlabs/powershell: | |
# exec { 'setup-ansible-remoting': | |
# command => file("${module_name}/ansible-setup-windows.ps1"), | |
# provider => powershell, | |
# logoutput => true, | |
# } | |
# To make sure that we can run local scripts. You may need to run this line manually before you can run this script | |
Set-ExecutionPolicy unrestricted | |
# Doing this the hard way for old versions of powershell that don't interpolate and don't support Invoke-WebRequest | |
$t = [Environment]::ExpandEnvironmentVariables('%TEMP%') | |
# Sometimes the temp dir for the user executing this doesn't exit (yet) | |
if ((Test-Path $t) -eq $false) { | |
md $t | |
} | |
$WebClient = New-Object System.Net.WebClient | |
# Check and upgrade powershell to 3.0 if necessary (reboots machine) | |
if ($PSVersionTable.PSVersion.Major -lt 3) { | |
$WebClient.DownloadFile("https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/upgrade_to_ps3.ps1","$t\ps3upgrade.ps1") | |
& "$t\ps3upgrade.ps1" | |
Write-Host "Initiated PowerShell upgrade which will require a reboot, exiting this script" | |
exit | |
} | |
# Run ansible windows remoting setup script (does nothing if already configured) | |
$WebClient.DownloadFile("https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1", "$t\ansible-setup-remoting.ps1") | |
& "$t\ansible-setup-remoting.ps1" -CertValidityDays 3650 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! Exactly what I needed 👍