How to launch an EC2 instance into a VPC with a public IP address in PowerShell? - powershell

I have been trying to use the following script, but haven't been successful:
$Ami=Get-EC2ImageByName WINDOWS_2012_BASE
New-EC2Instance -ImageId $Ami[0].ImageId -MinCount 1 -MaxCount 1 -KeyName uckey -InstanceType `
t1.micro -SubnetId subnet-56738b33 -AssociatePublicIp $true
The error is:
New-EC2Instance : Object reference not set to an instance of an object.
At line:1 char:1
+ New-EC2Instance -ImageId $Ami[0].ImageId -MinCount 1 -MaxCount 1 -KeyName uckey ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Amazon.PowerShe...2InstanceCmdlet:NewEC2InstanceCmdlet)
[New-EC2Instance], InvalidOperationException
+ FullyQualifiedErrorId : System.NullReferenceException,Amazon.PowerShell.Cmdlets.EC2.NewEC2InstanceC
mdlet
The problem is about the parameter -AssociatePublicIp without it, the script works.
Thanks for reading

As of AWS PowerShell version 2.1.3.0, this bug has been corrected.
I was able to execute this script:
New-EC2Instance -ImageId $Ami[0].ImageId -MinCount 1 -MaxCount 1 -KeyName uckey -InstanceType `
t1.micro -SubnetId subnet-56738b33 -AssociatePublicIp $true

I ran into the same problem, and a possible workaround while still using PowerShell is to create the network interface first, and then associating it with the instance:
$subnetId = "subnet-56738b33"
$keyName = "uckey"
$instanceType = "t1.micro"
$Ami = Get-EC2ImageByName WINDOWS_2012_BASE
$ImageId = $Ami[0].ImageId
$networkInterface = New-EC2NetworkInterface -SubnetId $subnetId -Description "Primary network interface"
$interfaceSpec = New-Object Amazon.EC2.Model.InstanceNetworkInterfaceSpecification -property #{"NetworkInterfaceId"=$networkInterface.NetworkInterfaceId}
$reservation = New-EC2Instance -ImageId $ImageId -MinCount 1 -MaxCount 1 -InstanceType $instanceType -KeyName $keyName -NetworkInterfaces $interfaceSpec
The InstanceNetworkInterfaceSpecification has a property to indicate if the interface needs a public IP address (see the docs)

I suspect this to be a bug in the AWS Tools for Windows PowerShell. As already commented, running the semantically identical command with the AWS Command Line Interface instead yields the desired result:
$ aws ec2 run-instances --image-id $ami.Imageid --count 1:1 --instance-type t1.micro `
--key-name uckey --subnet-id subnet-56738b33 --associate-public-ip-address
Beware of the slight syntax difference for --count and --associate-public-ip-address, the latter doesn't require a value, rather comprises the flag in itself, i.e. [--associate-public-ip-address | --no-associate-public-ip-address], see run-instances.
This is also confirmed by an (unfortunately unanswered) inquiry in the AWS Forum for PowerShell scripting, see Unable to get New-EC2Instance to honour -AssociatePublicIP.
Accordingly, your best bet to get this resolved might be to bump that thread and hope for a response from the AWS team. Meanwhile you can work around the issue by means of scripting the operation via the AWS CLI instead.

Related

Add-AzureRmServiceFabricNodeType -> 'accountName' cannot be null

I'm trying to use the 'Add-AzureRmServiceFabricNodeType' command to add a new nodeType to an existing service fabric cluster. This is my command:
Add-AzureRmServiceFabricNodeType -ResourceGroupName "$ResourceGroupName$" -Name "$ClusterName$" -NodeType "$TypeName$" -VmSku "Standard_H8" -Capacity 3 -VmUserName "$UserName$" -VmPassword $pwd
Having already logged in and set the subscription using 'Login-AzureRmAccount' and 'Set-AzureRmContext'
The call runs for ~1hr and then returns the following error:
WARNING: Rolling back the changes to the cluster
Add-AzureRmServiceFabricNodeType : 'accountName' cannot be null.
At line:1 char:1
+ Add-AzureRmServiceFabricNodeType -ResourceGroupName "%ResourceGroupName% ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Add-AzureRmServiceFabricNodeType], ValidationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ServiceFabric.Commands.AddAzureRmServiceFabricNodeType
I have successfully added a nodetype to this cluster in the past, but then i didn't set the -VmSku option. As well as that difference, the subscription has since been upgraded from a pay as you go sub to an Enterprise Agreement. Based on the error received I guess it might have something to do with that, but i can't seem to find what exactly.
Any ideas?
I will assume you used the secure encoding to provide the password
$password = ConvertTo-SecureString -String 'Password$123456' -AsPlainText -Force
I also suggest you add the -Tier as part of your command, because as part of provisioning process it requires the sku, tier and capacity. If you not provide one it will use the default, and the sku might not be compatible with the default tier or your account availability.
You can also check in Azure if the VMSS are created once you run the command.
If you want to investigate further, I would recommend reading the source code for the command operation executed for adding node types.

Azure swap not working

I am trying to use Switch-AzureWebsiteSlot as it follows:
Switch-AzureWebsiteSlot -Name 'sitename' -slot1 'Staging' -slot2 'Production' -f
orce -verbose
However even though my website has 2 slots, Staging and the default Production I still receive the following error from Power Shell.
Switch-AzureWebsiteSlot : The website must have at least two slots to apply swap
At line:1 char:1
+ Switch-AzureWebsiteSlot -Name 'sitename' -slot1 'St ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Switch-AzureWebsiteSlot], PSInvalidOperationException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Websites.SwitchAzureWebsiteSlotCommand
Do you have any ideas why? Did you also encounter this during your code journeys?
Switch-AzureWebsiteSlot is for ASM websites. In your case, I think you have created an ARM website, hence Switch-AzureWebsiteSlot is not suitable here.
To swap slot for ARM websites, you need to use below script:
$ParametersObject = #{targetSlot = "[slot name – e.g. “production”]"}
Invoke-AzureRmResourceAction -ResourceGroupName [resource group name]-ResourceType Microsoft.Web/sites/slots -ResourceName [web app name]/[slot name] -Action slotsswap -Parameters $ParametersObject -ApiVersion 2015-07-01
You can check this article for details.
Update:
Switch-AzureWebsiteSlot also works for ARM web app. My problem is that I have two subscriptions, I didn't select the correct subscription when testing the command previously. Thanks to Flemin!
Before:
Now with the right subscription:
This command works even if you have created app in ARM.
Switch-AzureWebsiteSlot -Name 'siteName' -slot1 'StagingDeploymentName' -slot2 'Production' -force -verbose
Please check the names that you have given and if the slots are up and running.
I tested this by stopping one of the deployment slots and go the same error as you got. So, I'm sure now that in your case either one slot is not in running state.
For an ARM web app, Switch-AzureWebsiteSlot does not work for me at all. Get the following error message:
Switch-AzureWebsiteSlot : No default subscription has been designated.
Use Select-AzureSubscription -Default to set the
default subscription.
From what I understand, Select-AzureSubscription is an ASM command so it's not applicable. (Instead, for ARM I need to use Select-AzureRmSubscription.)
The command Swap-AzureRmWebAppSlot is what worked for me:
Swap-AzureRmWebAppSlot -Name "sitename" -SourceSlotName "Staging" -DestinationSlotName "Production" -ResourceGroupName "<Your Resource Group Name"

Using AWS Powershell to deploy to AWS elastic beanstalk

Hi I am trying to deploy onto Elastic Beanstalk using AWS Powershell.
Currently I am just trying to get the EB environment using the following cmdlet:
+ Get-EBEnvironment
-ApplicationName
-EnvironmentId
-VersionLabel
-EnvironmentName
-IncludedDeletedBackTo
-IncludeDeleted
This is the cmdlet I used: Get-EBEnvironment -ApplicationName appName
However, I am getting the following error:
Get-EBEnvironment : No region specified or obtained from persisted/shell defaults.
At C:\Users\lowong\Desktop\script.ps1:22 char:1
Get-EBEnvironment -ApplicationName evcfacade
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (Amazon.PowerShe...vironmentCmdlet:GetEBEnvironmentCmdlet) [Get-EBEnvironment], InvalidOperationException
FullyQualifiedErrorId : InvalidOperationException,Amazon.PowerShell.Cmdlets.EB.GetEBEnvironmentCmdlet
Am I missing other fields I have to put onto the cmdlet? or what's the problem?
(Here's the link to the documentation of the cmdlet: http://docs.aws.amazon.com/powershell/latest/reference/index.html?page=New-EBApplicationVersion.html&tocid=New-EBApplicationVersion)
The error mentions the following:
No region specified or obtained from persisted/shell defaults.
So, you have 2 possible resolutions:
Include the -Region parameter, such as -Region us-east-1. See Get-EBEnvironment Cmdlet. Or
Use the Set-DefaultAWSRegion cmdlet to set the default region. See Set-DefaultAWSRegion Cmdlet
Note that Set-DefaultAWSRegion is not persisted (you have to specify it for every powershell session).
If you want to persist region, it can be done as part of setting up your credentials with:
Initialize-AWSDefaults -ProfileName {profileName} -Region {region}
For more information see: Specifying Credentials

Azure PowerShell Add-AlertRule Returns Bad Request when using CpuPercentage on Resource Group

I get an Azure Resource Group and try to add a Metric Alert rule of type CPUPercentage, but it fails with BadRequest.
> $r = Get-AzureRmResourcegroup
WARNING: The output object of this cmdlet will be modified in a future release.
> $a = $r[0]
> Add-AlertRule -Location "West US" -MetricName CpuPercentage -Name CPU98Percent -Operator GreaterThanOrEqual -ResourceGroup $a.ResourceGroupName -ResourceId $a.ResourceId -RuleType Metric -Threshold 98
Add-AlertRule : BadRequest:
At line:1 char:1
+ add-alertrule -Location "West US" -metricName CpuPercentage -Name CPU ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-AlertRule], CloudException
+ FullyQualifiedErrorId : Hyak.Common.CloudException,Microsoft.Azure.Commands.Insights.Alerts.AddAlertRuleCommand
It works fine from the portal. There isn't already an alert with the same name either.
I'm using version 1 of the Azure module and version 1.0.2 of AzureRM.
I was receiving the same error when the alert name was not unique across all databases on the Sql Server. That is, I had multiple databases on the same server and was trying to create a rule named "DTU Percentage", which would fail after the first one was created. Once a unique name ("DTU Percentage DB_NAME") was used, the alerts were created.
Unfortunately, it sounds like you are experiencing another issue. Just thought I would add this answer for anyone searching for similar errors.

Reserving static IP for existing Azure Virtual Machine

I have created 3 virtual machines which are already in operation and I forgot to first assign a static IP at time of creation. I am now encountering some issues when I am trying to do this.
I have created a reserved IP using the following powershell:
New-AzureReservedIP “dmz-live” –Label “be-dmz-ip” –Location “North Europe”
Following this post http://prmdbaora.blogspot.in/2015/06/normal-0-false-false-false-en-us-x-none.html I tried to delete the VM and reassign but when I use the following code:
New-AzureVMConfig -Name "be-dmz" -InstanceSize "Standard_D11" –ImageName "be-dmz-be-dmz-0-201508100952090393" | New-AzureVM -ServiceName "be-dmz" –ReservedIPName " be-dmz-ip " -Location "CloudVNET”
I am getting an error:
New-AzureVMConfig : Must specify MediaLocation or set a current storage account using Set-AzureSubscription.
At line:1 char:1
+ New-AzureVMConfig -Name "be-dmz" -InstanceSize "Standard_D11" -ImageName "be-dmz ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzureVMConfig], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.NewAzure
VMConfigCommand
I have confirmed that the name of the disk is correct, after deleting the cloud service this disk was present but when I tried to run the command it deleted the disk and it is no longer showing in the list of disks
I want to add these machines to a virtual network called "CloudVNET". Can anyone give me a pointer as to what I am doing wrong here please and how I can go about assigned these reserved IPs to the machines