Skip to content

Instantly share code, notes, and snippets.

@tyconsulting
Last active February 27, 2020 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyconsulting/72da9fbbe49ce6b468e7703340c7a1f6 to your computer and use it in GitHub Desktop.
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
# 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