Skip to content

Instantly share code, notes, and snippets.

@akomakom
Last active December 25, 2020 23:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akomakom/1842c5372d75114520a0a5dec46e83ae to your computer and use it in GitHub Desktop.
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 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
@dandoingdev
Copy link

Thanks for this! Exactly what I needed 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment