function Send-DeploymentManifestToS3 { <# .SYNOPSIS Generates an inventory for the current machine (or accepts one from an existing local file location) and uploads it to s3. If a valid bucket is supplied that will be targeted. Otherwise a bucket name will be generated from machine.config values. If that generates an invalid bucket name it will throw. .EXAMPLE Send-DeploymentManifestToS3 This will upload a local manifest to the bucket generated based on machine.config values. Send-DeploymentManifestToS3 -Bucket "alkami-manifest-test" This will upload a local manifest to the "alkami-manifest-test" bucket Send-DeploymentManifestToS3 -Bucket "alkami-manifest-test" -Filename "C:\Temp\foo1.json" This will upload the manifest in the specified file (no validation is performed here) to the "alkami-manifest-test" bucket .PARAMETER S3BucketName Optional bucket name. Mostly used for local (non-aws) machine testing. .PARAMETER InventoryFilename Optional local manifest file name. .PARAMETER ProfileName [string] Specific AWS CLI Profile to use in AWS API calls #> [CmdletBinding()] param( [Parameter(Mandatory = $false)] [Alias("Bucket")] [string]$S3BucketName, [Parameter(Mandatory = $false)] [Alias("Filename")] [string]$InventoryFilename, [Parameter(Mandatory = $false)] [string]$ProfileName ) $logLead = (Get-LogLeadName) Import-AWSModule # S3 $defaultTempFileLocation = "c:\temp\manifest" $filename = "" $splatParams = @{} if (!([string]::IsNullOrEmpty($ProfileName))) { $splatParams["ProfileName"] = "$ProfileName" } if ([string]::IsNullOrEmpty($s3BucketName)) { $envName = Get-AppSetting "Environment.Name" $envType = Get-AppSetting "Environment.Type" $envName = $envName.Split(" ")[-1].Replace(".", "-") if ($envType -eq "Production") { $envType = "prod" } $s3BucketName = "orb-$envType-$envName-script-bucket" } if ([string]::IsNullOrEmpty($inventoryFilename)) { if (!(Test-Path $defaultTempFileLocation)) { New-Item -Path $defaultTempFileLocation -ItemType "directory" } $filename = "$defaultTempFileLocation\$env:computername.json" Get-MachineInventory -filter chocolatey -asJson | Out-File -FilePath $filename } elseif (!($null -eq $inventoryFilename)) { $filename = $inventoryFilename } else { throw "$logLead : Inventory source is required" } $leafName = Split-Path $filename -leaf $leafNameWithoutExtension = $leafName.Split(".")[0] $existingManifests = Get-S3Object -BucketName $s3BucketName -KeyPrefix $leafNameWithoutExtension @splatParams if ($null -eq $existingManifests) { Write-S3Object -BucketName $s3BucketName -File $filename @splatParams } else { if ($existingManifests.Count -gt 1) { $oldManifest = $existingManifests | Where-Object {$_.Key -ne $leafName} if ($oldManifest.Count -gt 1) { throw "$loglead : Unexpected manifests found in S3. Please manually inspect the bucket." } # Force this delete so that we don't ask for human input. Remove-S3Object -BucketName $s3BucketName -Key $oldManifest.Key -Force @splatParams } $fileTimestamp = Get-Date -Format FileDateTimeUniversal $destinationKey = [System.Io.Path]::GetFileNameWithoutExtension($leafName) + "-" + $fileTimestamp + ".json" Copy-S3Object -BucketName $s3BucketName -key $leafName -DestinationBucket $s3BucketName -DestinationKey $destinationKey @splatParams # This should overwrite the existing one that we just copied to a new renamed file. Write-S3Object -BucketName $s3BucketName -File $filename @splatParams } Write-Host "Manifest has been uploaded to $s3BucketName." }