Skip to content

Instantly share code, notes, and snippets.

@tyconsulting

tyconsulting/cloud.ps1 Secret

Last active Feb 27, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.