{"id":6121,"date":"2015-05-16T00:01:00","date_gmt":"2015-05-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/05\/16\/weekend-scripter-get-started-with-powershell-for-amazon-web-services\/"},"modified":"2020-01-29T20:58:20","modified_gmt":"2020-01-30T04:58:20","slug":"weekend-scripter-get-started-with-powershell-for-amazon-web-services","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-get-started-with-powershell-for-amazon-web-services\/","title":{"rendered":"Weekend Scripter: Get Started with PowerShell for Amazon Web Services"},"content":{"rendered":"<p><b style=\"font-size: 12px;\">Summary<\/b><span style=\"font-size: 12px;\">: Windows PowerShell MVP, Mike F Robbins, walks through a setup to manage an Amazon Web Services environment with Windows PowerShell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Welcome back Mike F Robbins to talk about Windows PowerShell for Amazon Web Services.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog1a.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" title=\"Photo of Mike Robbins\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog1a.jpg\" alt=\"Photo of Mike Robbins\" \/><\/a><\/p>\n<p style=\"margin-left: 30px;\">Mike F Robbins is a Microsoft MVP for Windows PowerShell and for SAPIEN Technologies. He is a co-author of <i>Windows PowerShell TFM 4<sup>th<\/sup> Edition,<\/i> and he is the contributing author of a chapter in <i>PowerShell Deep Dives<\/i>. Mike has written guest posts for PowerShell Magazine, PowerShell.org, and the Hey, Scripting Guy! Blog (to read more of Mike&#8217;s guest posts, see these <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/tag\/mike-f-robbins\/\" target=\"_blank\" rel=\"noopener noreferrer\">Hey, Scripting Guy! Blog posts<\/a>). He is the winner of the advanced category in the 2013 Windows PowerShell Scripting Games. Mike is also the leader and cofounder of the <a href=\"http:\/\/mspsug.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Mississippi PowerShell User Group<\/a>. He blogs at <a href=\"http:\/\/mikefrobbins.com\/\">Mike F Robbins Computing Solutions<\/a>, he\u00a0and can be found on twitter at\u00a0<a href=\"https:\/\/twitter.com\/mikefrobbins\" target=\"_blank\" rel=\"noopener noreferrer\">@mikefrobbins<\/a>.<\/p>\n<p>Now, here&#8217;s Mike&#8230;<\/p>\n<p>Let me first say that this certainly isn\u2019t a marketing post for Amazon (or any cloud provider, for that matter), and this blog post isn\u2019t meant to endorse one cloud provider over another. I currently work with a mix of Azure and Amazon cloud environments, and I manage both of them (and multiple datacenters with on premise infrastructures) with Windows PowerShell.<\/p>\n<p>The first thing that you\u2019ll need to get started with Amazon Web Services is an AWS account. See <a href=\"http:\/\/aws.amazon.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Amazon Web Services<\/a> for more information about creating an account if you don\u2019t already have one.<\/p>\n<p>When you have an AWS account, visit <a href=\"http:\/\/aws.amazon.com\/powershell\/\" target=\"_blank\" rel=\"noopener noreferrer\">AWS Tools for Windows PowerShell<\/a> to download the tools. There\u2019s also an online reference for the AWS cmdlets on that webpage.<\/p>\n<p>Windows PowerShell can be used to download the AWS Tools for Windows PowerShell:<\/p>\n<p style=\"margin-left: 30px;\">$Uri = &#8216;http:\/\/sdk-for-net.amazonwebservices.com\/latest\/AWSToolsAndSDKForNet.msi&#8217;<\/p>\n<p style=\"margin-left: 30px;\">Invoke-WebRequest -Uri $Uri -OutFile &#8220;$env:HOMEPATH\\downloads\\$($Uri -replace &#8216;^.*\/&#8217;)&#8221;<\/p>\n<p style=\"margin-left: 30px;\">Unblock-File -Path &#8220;$env:HOMEPATH\\downloads\\$($Uri -replace &#8216;^.*\/&#8217;)&#8221;<\/p>\n<p>The AWS Tools for Windows PowerShell require that the script execution policy be set to <b>RemoteSigned<\/b> or less so that script execution is allowed on the system that they\u2019re being installed on:<\/p>\n<p style=\"margin-left: 30px;\">Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog1b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog1b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>If you\u2019re only installing the AWS Tools so you can manage your AWS environment with Windows PowerShell, you\u2019ll only need to install the AWS Tools for Windows PowerShell, as shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1104.awsblog2b.jpg\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1104.awsblog2b.jpg\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>This will add the installation location to your <b>$env:PSModulePath<\/b>, and if you\u2019re running Windows PowerShell 3.0 or higher, there is no need to manually import the module.<\/p>\n<p>From the services section of your online AWS account dashboard, create an IAM user account and assign it the appropriate permissions. The <b>AccessKey<\/b> and <b>SecretKey<\/b> will be needed. These credentials should be kept secure. After the first account is created, additional IAM user accounts can be created in Windows PowerShell by using the <b>New-IAMUser<\/b> cmdlet.<\/p>\n<p>Set the AWS credentials for your current Windows PowerShell session or save them persistently as I\u2019ve done in the following example:<\/p>\n<p style=\"margin-left: 30px;\">Set-AWSCredentials -AccessKey MYACCESSKEYFORAWS123 -SecretKey AWS1234567890abcdefghijklmnopqrstuvwxyz -StoreAs myprofile<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog3b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog3b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>List the stored credentials:<\/p>\n<p style=\"margin-left: 30px;\">Get-AWSCredentials -ListStoredCredentials<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog4b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog4b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>The stored credentials can be removed by using the <b>Clear-AWSCredentials<\/b> cmdlet, if desired.<\/p>\n<p>The next step is to set the default AWS region, but first let\u2019s determine what the valid regions are:<\/p>\n<p style=\"margin-left: 30px;\">Get-AWSRegion<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog5b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog5b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>To set the default region, use:<\/p>\n<p style=\"margin-left: 30px;\">Initialize-AWSDefaults -ProfileName myprofile -Region us-east-1<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog6b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog6b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Verify that the default region was indeed set correctly:<\/p>\n<p style=\"margin-left: 30px;\">Get-DefaultAWSRegion<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog7b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog7b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>The default region can be cleared by using the <b>Clear-DefaultAWSRegion<\/b> cmdlet, if needed.<\/p>\n<p>You\u2019re now set up to use Windows PowerShell to manage the products associated with your AWS account. That was easy enough, wasn\u2019t it?<\/p>\n<p>Now I\u2019ll demonstrate how to retrieve a list of and remove snapshots created by an AWS storage gateway.<\/p>\n<p>First, I\u2019ll query a list of storage gateways that are set up on AWS for this particular account:<\/p>\n<p style=\"margin-left: 30px;\">Get-SGGateway | Format-Table -AutoSize<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog8b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog8b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>To retrieve a list of all the volumes for the storage gateway returned in the previous results, use:<\/p>\n<p style=\"margin-left: 30px;\">Get-SGGateway | Get-SGVolume | Format-Table -AutoSize<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog9b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog9b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I\u2019ve written a Windows PowerShell script module named <b>MrAWS<\/b>. It contains a couple of functions that are designed to help make managing the storage gateway snapshots on AWS much easier without having to know all of the intricacies of working with the AWS cmdlets.<\/p>\n<p>To see all of the snapshots for all of the volumes on a particular AWS storage gateway that are older than 14 days, simply use the <b>Get-MrAWSSGSnapshot<\/b> function (which is part of my <b>MrAWS<\/b> module) and specify the gateway name with the number 14 for the <b>Days<\/b> parameter:<\/p>\n<p style=\"margin-left: 30px;\">Get-MrAWSSGSnapShot -GatewayName abcde -Days 14<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog10b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog10b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>The <b>Remove-MrAWSSGGateway<\/b> function (also part of my <b>MrAWS<\/b> module) is designed to remove all snapshots from all volumes older than the specified number of days from the specified AWS storage gateway. By default, to prevent accidentally deleting all snapshots, it only deletes snapshots that are older than 14 days if the <b>Days<\/b> parameter is not specified. This function supports the <b>Confirm<\/b> and <b>WhatIf<\/b> parameters. I recommend trying it with the <b>WhatIf<\/b> parameter first to see what it will do before actually deleting any snapshots:<\/p>\n<p style=\"margin-left: 30px;\">Remove-MrAWSSGSnapShot -GatewayName abcde -Days 14 -WhatIf<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog11b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog11b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Not specifying the <b>Confirm<\/b> or <b>WhatIf<\/b> parameter will permanently delete the snapshots that are older than the specified number of days:<\/p>\n<p style=\"margin-left: 30px;\">Remove-MrAWSSGSnapShot -GatewayName abcde -Days 14<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog12b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog12b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>When an attempt is made to retrieve the snapshots that are older than 14 days, nothing is returned because all of the snapshots that were older than 14 days were previously deleted.<\/p>\n<p style=\"margin-left: 30px;\">Get-MrAWSSGSnapShot -GatewayName abcde -Days 14<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog13b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog13b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>By default, the <b>Get-MrAWSSGGateway<\/b> function returns all snapshots for all volumes of the specified AWS storage gateway. By not specifying the <b>Days<\/b> parameter, you can see that there are still 42 snapshots available and those are the ones that have been created within the past 14 days:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog14b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog14b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I hope you\u2019ve enjoyed this blog post, but keep in mind that I really haven\u2019t scratched the surface when it comes to all of the cmdlets available for managing your AWS environment. Use the following command to view all of the cmdlets that are available in the AWSPowerShell module:<\/p>\n<p style=\"margin-left: 30px;\">Get-Command -Module AWSPowerShell<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog15b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog15b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>As of this writing, there are a total of 1142 cmdlets in the AWSPowerShell module:<\/p>\n<p style=\"margin-left: 30px;\">(Get-Command -Module AWSPowerShell).count<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog16b.jpg\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/awsblog16b.jpg\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>You can download the <b>MrAWS<\/b> module demonstrated in this post from GitHub: <a href=\"https:\/\/github.com\/mikefrobbins\/AWS\" target=\"_blank\" rel=\"noopener noreferrer\">mikefrobbins\/AWS<\/a>.<\/p>\n<p>Thanks, Mike, for sharing your time and knowledge.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener noreferrer\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener noreferrer\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener noreferrer\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener noreferrer\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size: 12px;\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Windows PowerShell MVP, Mike F Robbins, walks through a setup to manage an Amazon Web Services environment with Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Welcome back Mike F Robbins to talk about Windows PowerShell for Amazon Web Services. Mike F Robbins is a Microsoft MVP for Windows PowerShell and for SAPIEN [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[458,56,344,3,582,61,45],"class_list":["post-6121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-applications","tag-guest-blogger","tag-mike-f-robbins","tag-scripting-guy","tag-server-applications","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Windows PowerShell MVP, Mike F Robbins, walks through a setup to manage an Amazon Web Services environment with Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Welcome back Mike F Robbins to talk about Windows PowerShell for Amazon Web Services. Mike F Robbins is a Microsoft MVP for Windows PowerShell and for SAPIEN [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=6121"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=6121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=6121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=6121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}