-
-
Save tyconsulting/72da9fbbe49ce6b468e7703340c7a1f6 to your computer and use it in GitHub Desktop.
Puppet external fact to detect which cloud platform is the Windows server running on
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
# Puppet External Facts - Detect public cloud provider that the VM is running under | |
Param ([switch]$verbose) | |
if($verbose) { | |
$VerbosePreference = "continue" | |
} | |
#region variables | |
$AzureMetadataServiceURI = "http://169.254.169.254/metadata/instance?api-version=2017-08-01" | |
$cloudProvider = $null | |
#endregion | |
#region main | |
$csproduct = Get-CIMInstance Win32_ComputerSystemProduct | |
$bios = Get-CIMInstance Win32_BIOS | |
$vendor = $csproduct.vendor | |
$serialNumber = $bios.serialNumber | |
Write-Verbose "Serial number: $serialNumber" | |
#Azure | |
If ($Vendor -imatch 'microsoft') | |
{ | |
try { | |
$AzureMetadataRequest = Invoke-webrequest -UseBasicParsing -URI $AzureMetadataServiceURI -Headers @{'Metadata'='true'} -ErrorAction SilentlyContinue -TimeoutSec 1 | |
} Catch { | |
Write-verbose "not an Azure VM" | |
} | |
If ($AzureMetadataRequest.StatusCode -eq 200) | |
{ | |
$cloudProvider = 'azure' | |
write-verbose "This is an Azure VM" | |
} else { | |
write-verbose "not an Azure VM" | |
} | |
} else { | |
write-verbose "not an Azure VM" | |
} | |
#AWS | |
If (!$cloudProvider) | |
{ | |
if ($serialNumber -imatch '^ec2*') { | |
$cloudProvider = 'aws' | |
write-verbose "This is an AWS VM" | |
} else { | |
Write-verbose "not an AWS VM" | |
} | |
} | |
#GCP | |
If (!$cloudProvider) | |
{ | |
if ($serialNumber -imatch '^googlecloud') { | |
$cloudProvider = 'gcp' | |
write-verbose "This is a GCP VM" | |
} else { | |
Write-verbose "not a GCP VM" | |
} | |
} | |
#Process output | |
If ($cloudProvider) | |
{ | |
Write-Host "cloud_provider=$CloudProvider" | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment