Unable to create Azure VM using Powershell - powershell

Using PS 5. 0 on windows 10 I created an Azure Storage account and an Azure Service. Got the latest image name using the following command. But when I run the following command to create a VM I get following error:
PS Command to get the latest image:
$images = Get-AzureVMImage `
| where { $_.ImageFamily -eq “Windows Server 2012 Datacenter” } `
| Sort-Object -Descending -Property PublishedDate
$latestImage = $images[0]
$latestImage
The above command ran successfully and gave me the image name as: a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-20171017-en.us-127GB.vhd that I used in the following command for creating a VM.
PS command to create VM:
New-AzureVMConfig -Name "Server15" -InstanceSize ExtraSmall -ImageName "a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-20171017-en.us-127GB.vhd" | Add-AzureProvisioningConfig -Windows -AdminUsername "MyAdmin" -Password "MyPsswd" | New-AzureVM -ServiceName "MyServiceName"
Error:
WARNING: No deployment found in service: 'MyServiceName'.
New-AzureVM : BadRequest: OSImage a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-20171017-en.us-127GB.vhd
not found. If you are using a VM image, it must be specified as VMImageName for the role, not as SourceImageName for
OSVirtualHardDisk.
OperationID : '498779aecff53369ac9e793da15c16c3'
At line:1 char:250
+ ... d "D7v.oeiue4ieiur" | New-AzureVM -ServiceName "MyServiceName"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureVM], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.PersistentVMs.NewAzureVMCommand

Your script works for me, here is the output:
PS C:\Users\jason> New-AzureVMConfig -Name "Server16" -InstanceSize ExtraSmall -ImageName "a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-20171017-en.us-127GB.vhd" | Add-AzureProvisioningConfig -Windows -AdminUse
rname "jason" -Password "xxxxxxx" | New-AzureVM -ServiceName "jasontest321"
OperationDescription OperationId OperationStatus
-------------------- ----------- ---------------
New-AzureVM 05a8d386-ac4b-3843-8fe4-1018325112a3 Succeeded
Please check your Azure PowerShell version, for now the latest version is 5.0.1, my Azure PowerShell version is 4.4.1, that script works fine, maybe we should upgrade your Azure PowerShell.
We can download Azure PowerShell 5.0.1 installer to your Windows 10 and install it, then test it again.
More information about Azure PowerShell Version, please refer to this link.
Hope this helps.

Related

PowerBI New-PowerBIReport Powershell cmdlet error "A task was canceled."

I am trying to create a new PowerBI Report using Powershell .
I am running below command :
$destinationReport = New-PowerBIReport -Path $sourcePBIX -Name $datasetName -WorkspaceId $GroupID -ConflictAction CreateOrOverwrite
It runs the command for a while and through error:
New-PowerBIReport : A task was canceled."
+ ... ionReport = New-PowerBIReport -Path $sourcePBIX -Name $datasetName - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (Microsoft.Power...ewPowerBIReport:NewPowerBIReport) [New-PowerBIReport], TaskCanceledException
+ FullyQualifiedErrorId : A task was canceled.,Microsoft.PowerBI.Commands.Reports.NewPowerBIReport
The PBIX I am using is of size 200MB so I thought it could be due to time out and I increased the IdleTimeout using the steps in this article How to set Timeout in Powershell .
But this did not worked either .
In the azure devops hosted agent, we do not have the MicrosoftPowerBIMgmt.Reports package. So we recommend you can add the ps commend:
Install-Module -Name MicrosoftPowerBIMgmt.Reports -RequiredVersion 1.0.946
Also, you can try to run the commend line on your local machine and if it works well, please install a self-agent in your local machine to try again.

Assign Network Security Group to a Virtual Machine Network Interface using PowerShell

I have existing NSG and VM and planning to add the NSG to existing VM's NIC and simultaneously remove as well. I have prepared this based on example provided in http://windowsitpro.com/azure/manage-network-security-groups-powershel. The below command failing with method not supported errors.
$NICName = 'azwebvm0186'
$RGName = 'Prod_ResourceGroup'
$NsgName = 'Prod_ILB_SG'
$NSG = Get-AzureRmNetworkSecurityGroup -Name $NsgName -ResourceGroupName $RGName
$NIC = Get-AzureRmNetworkInterface -Name $NICName -ResourceGroupName $RGname
$NIC.NetworkSecurityGroup = $NSG
Set-AzureRmNetworkInterface -NetworkInterface $NIC
its failing with below error
$NIC.NetworkSecurityGroup = $NSG : Specified method is not supported.
+ CategoryInfo : NotImplemented: (:) [], PSNotSupportedException
+ FullyQualifiedErrorId : NotSupported
I have tested in my lab, your script works for me. I think you had better check as the following ways:
1.Check $NSG and $NIC.NetworkSecurityGroup value and type. Please ensure they have the same type.
PS C:\Users\v-shshui> $NIC|gm
TypeName: Microsoft.Azure.Commands.Network.Models.PSNetworkInterface
2. Check your Azure Powershell version. My version is 3.3.0. You could get version by use the following cmdlet.
Get-Module -ListAvailable -Name Azure -Refresh
If your version is not latest, I suggest you could update your version to latest. You can download the PS version 3.3.0 installation file from this link

New-AzureQuickVM not creating VM on exsisting Cloud Service?

I'm trying to run the following Azure Powershell cmdlet to provision a new Virtual Machine on a existing Cloud Service.
PS C:\> Get-AzureService | ft ServiceName
ServiceName
-----------
$AZURETEST-EUWEST0
$AZURETEST-JPWEST0
$AZURETEST-USEAST0
$AZURETEST-USWEST0
PS C:\> $image = "a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201505.01-en.us-127GB.vhd"
PS C:\> New-AzureQuickVM -Windows -ServiceName "$AZURETEST-USEAST0" -name "AZURESVM-USE1" -ImageName $image -Password Password1 -AdminUsername admin -WaitForBoot
New-AzureQuickVM : ResourceNotFound: The deployment name '$AZURETEST-USEAST0' does not exist.
At line:1 char:1
+ New-AzureQuickVM -Windows -ServiceName "$AZURETEST-USEAST0" -name "AZURESVM-USE1" -Im ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureQuickVM], CloudException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.PersistentVMs.NewQuickVM
New-AzureQuickVM : Sequence contains no matching element
At line:1 char:1
+ New-AzureQuickVM -Windows -ServiceName "$AZURETEST-USEAST0" -name "AZURESVM-USE1" -Im ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureQuickVM], InvalidOperationException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.PersistentVMs.NewQuickVM
PS C:\>
What doesn't make sense is I'm getting an error that says my Cloud Service - or Deployment Name doesn't exist when you can clearly see it returned when I listed all the available Cloud Services!
First of all, admin as administrator account name will not work. It's a reserved word, once you get past the service name, it will break on this one.
Second, there's nothing wrong with your powershell command and I tested exactly with the same parameters to make sure. I can bet it's the "$" you used for the cloud service name that is being misinterpreted by the cmdlet.
Since you can't rename cloud services, create a new one without the "$" and try again to see if it works.
Update: This is what happens when i try to create CS from the portal with the "$" as first character:
"This field can contain only letters, numbers, and hyphens. The first
and last character in the field must be a letter or number.
Trademarks, reserved words, and offensive words are not allowed."
Try to enter all parameters inline without using named parameters (e.g $image). that solved it for me.
Here is a working example:
$image = "a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-Datacenter-20160229-en.us-127GB.vhd"
$location = 'West Europe'
New-AzureQuickVM -Windows -Location $location -ServiceName "RamiSOTest1-cs" -name "RamiSOTest1-VM" -ImageName $image -Password 'RamiPass-1' -AdminUsername 'rami' -WaitForBoot

Creating a VM in Azure using Powershell

I am trying to create a VM in Azure using powershell. I am running this on my local PC. I have been able to setup websites and SQL Azure instances without any issues but None of the samples on the web work.
Import-Module Azure
Import-AzurePublishSettingsFile -PublishSettingsFile "C:\Cloud\Azure.publishsettings"
$VMImage = #(Get-AzureVMImage | Where-Object -Property Label -Match "Windows Server 2012 R2 Datacenter, November 2014").ImageName
$myVMName = "jsTestVM1"
$myAdminName ="jsTestAdmin1"
$myAdminPwd ="not telling you"
New-AzureQuickVM -ImageName $VMImage -Windows -Name $myVMName -ServiceName $myVMName -AdminUsername $myAdminName -Password $myAdminPwd -InstanceSize "ExtraSmall"
I get the following message:
New-AzureQuickVM : ResourceNotFound: The hosted service does not exist.
At line:1 char:1
+ New-AzureQuickVM -ImageName $VMImage -Windows -Name $myVMName -ServiceName $myVM ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureQuickVM], CloudException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.PersistentVMs.NewQuickVM
I've tried samples from the web and they give the same error message as well.
Edit:
If I specify a location I get the error message 'Service already exists, Location cannot be specified'.
If I specify AffinityGroup I get 'Service already exists, AffinityGroup cannot be specified'
$VMImage = #(Get-AzureVMImage | Where-Object -Property Label -Match "Windows Server 2012 R2 Datacenter, November 2014").ImageName
$myVMName = "srikstest"
$myAdminName ="srikstest"
$myAdminPwd ="NeitherMe"
New-AzureQuickVM -Windows -ServiceName $myVMName -ImageName $VMImage -AdminUsername $myAdminName - Password $myAdminPwd -InstanceSize "ExtraSmall" -Location "Southeast Asia" -Name $myVMName -Verbose
You can create a new Azure service for the virtual machine by specifying either the Location or AffinityGroup parameters, or deploy the new virtual machine into an existing service.
I've just added the location parameter and it worked !!
In the end I added an affinity group and changed the service name I was using. This enabled me to create the VM. The service name I was using was not in use as far as I was aware in my subscription so either a previous failed install left some artefact around or the name needs to be globally unique. I've checked the docs here
http://msdn.microsoft.com/en-us/library/azure/dn495183.aspx
and it doesn't state the service name needs to be globally unique
Assign/replace the value to variable.
New-AzureVMConfig -Name $VmName -InstanceSize $InstanceSize -Label $VmName -ImageName $Imagename -DiskLabel $DiskLabel |
add-azureprovisioningconfig -LinuxUser $cred.GetNetworkCredential().Username -Linux -Password $cred.GetNetworkCredential().Password -SSHPublicKeys $sshkey |
Add-AzureEndpoint -LocalPort $LocalPort1 -Name $LocalPortName1 -Protocol tcp -PublicPort $LocalPort1 |
New-AzureVM -ServiceName $CloudServiceName -AffinityGroup $AffinityGroup –WaitForBoot

Azure Powershell to stop VM's in a subscription

I am new to Powershell, and to be specific entirely new to Azure Powershell. I need to create a powershell script to switch off all the VM's found on a subscription. I guess this needs to be done with the help of management certificate. But not sure where to start. I just done some simple lines of code to list all VM's as shown below:
Add-AzureAccount -Environment "AzureCloud"
$subscription = "my-sub-scri-ption"
Select-AzureSubscription -Current "SubName"
Set-AzureSubscription -SubscriptionName "SubName"
Get-AzureVM -ServiceName "VM1"
The output recieved is "Get-AzureVM : Value cannot be null.
Parameter name: subscriptionId".
Can someone please help me in this regard?
**EDIT:**
The powershell script which I am using is given below:
Add-AzureAccount -Environment "AzureCloud"
Set-AzureSubscription -SubscriptionName "My Subs"
$serviceName = "Service01"
$vmName = "Service01"
Get-AzureVM | Stop-AzureVM -Force
Though while running it shows script execution is successful, I can see still that vm is powered on. Please note that the serviceName and vmName is same in my case. Anything wrong here in my code?
**Re-Edit**
Executed code:
Add-AzureAccount -Environment "AzureCloud"
Set-AzureSubscription -SubscriptionName "My Subs"
$serviceName = "Service01"
$vmName = "Service01"
Get-AzureVM
Error for the above code:
Get-AzureVM : Value cannot be null.
Parameter name: subscriptionId
At D:\TC_PS\Untitled1.ps1:5 char:1
+ Get-AzureVM
+ ~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzureVM], ArgumentNullException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand
You can start from here: Getting Started with Windows Azure PowerShell
Then you can simply run Get-AzureVM that will return all the virtual machines for every cloud service.
To stop a VM: Stop-AzureVM -ServiceName xxx -Name vm-test-01
To stop all the VMs in the subscription simply run: Get-AzureVM | Stop-AzureVM -Force
The -Force switch is necessary to stop the last virtual machine of the cloud service, otherwise the system would throw an error when trying to stop the last VM in the service, to avoid drop all the resources of your published app.
If you don't specify the -StayProvisioned switch, the virtual machine will be deallocated. This is the default behavior of Stop-AzureVM.