Installing Azure powershell in an azure Virtual Machine -


i need write powershell workflow creates azure virtual machine , executes azure cmdlets in azure virtual machine. newly created vm has no azure powershell module installed in it. code

    new-azurequickvm -windows -servicename $servicename -name $vmname -imagename $vmimage  -password $password -adminusername $username -instancesize "extrasmall" -waitforboot      $winrmuri = get-azurewinrmuri -servicename $servicename -name $vmname     $cred = new-object -typename system.management.automation.pscredential -argumentlist $username, $password      invoke-command -connectionuri $winrmuri -credential $cred -scriptblock {          add-azureaccount ......  ## these cmdlets need azure powershell module           set-azuresubscription........          new-azurestorageaccount......     } 

i not supposed manually rdp of vm , open install azure powershell module dynamically create vm using powershell cmdlet , install azure module in vm using powershell itself.

this can done arm (azure resource manager) template. json template defines objects deployed. in case, want deploy vm custom script extension. upon provisioning of vm, azure resource manager fetch supplied files , run custom powershell. see example below, , replace line https://<your-blob-here>.blob.core.windows.net/resources/custom-powershell-script.ps1 blob , powershell script. run script can use azure powershell, described here: https://azure.microsoft.com/en-us/documentation/articles/powershell-azure-resource-manager/

the key cmdlet purposes new-azureresourcegroup. invocation like:

switch-azuremode -name azureresourcemanager new-azureresourcegroup -name testrg1 -location "west us" -templatefile <your-json-arm-template>.json 

see list of arm templates here reference: https://github.com/azure/azure-quickstart-templates . sample template modify run custom code/install azure powershell.

{     "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymenttemplate.json#",     "contentversion": "1.0.0.0",     "parameters": {         "newstorageaccountname": {             "type": "string",             "metadata": {                 "description": "unique dns name storage account virtual machine's disks placed."             }         },         "adminusername": {             "type": "string",             "metadata": {                 "description": "username virtual machine."             }         },         "adminpassword": {             "type": "securestring",             "metadata": {                 "description": "password virtual machine."             }         },         "dnsnameforpublicip": {             "type": "string",             "metadata": {                 "description": "unique dns name public ip used access virtual machine."             }         },         "windowsosversion": {             "type": "string",             "defaultvalue": "2012-r2-datacenter",             "allowedvalues": [                 "2008-r2-sp1",                 "2012-datacenter",                 "2012-r2-datacenter"             ],             "metadata": {                 "description": "the windows version vm. pick patched image of given windows version. allowed values: 2008-r2-sp1, 2012-datacenter, 2012-r2-datacenter."             }         }     },     "variables": {         "location": "west us",         "imagepublisher": "microsoftwindowsserver",         "imageoffer": "windowsserver",         "osdiskname": "osdiskforwindowssimple",         "nicname": "myvmnic",         "addressprefix": "10.0.0.0/16",         "subnetname": "subnet",         "subnetprefix": "10.0.0.0/24",         "storageaccounttype": "standard_lrs",         "publicipaddressname": "mypublicip",         "publicipaddresstype": "dynamic",         "vmstorageaccountcontainername": "vhds",         "vmname": "mywindowsvm",         "vmsize": "standard_a2",         "virtualnetworkname": "myvnet",         "vnetid": "[resourceid('microsoft.network/virtualnetworks',variables('virtualnetworkname'))]",         "subnetref": "[concat(variables('vnetid'),'/subnets/',variables('subnetname'))]"     },     "resources": [         {             "type": "microsoft.storage/storageaccounts",             "name": "[parameters('newstorageaccountname')]",             "apiversion": "2015-05-01-preview",             "location": "[variables('location')]",             "properties": {                 "accounttype": "[variables('storageaccounttype')]"             }         },         {             "apiversion": "2015-05-01-preview",             "type": "microsoft.network/publicipaddresses",             "name": "[variables('publicipaddressname')]",             "location": "[variables('location')]",             "properties": {                 "publicipallocationmethod": "[variables('publicipaddresstype')]",                 "dnssettings": {                     "domainnamelabel": "[parameters('dnsnameforpublicip')]"                 }             }         },         {             "apiversion": "2015-05-01-preview",             "type": "microsoft.network/virtualnetworks",             "name": "[variables('virtualnetworkname')]",             "location": "[variables('location')]",             "properties": {                 "addressspace": {                     "addressprefixes": [                         "[variables('addressprefix')]"                     ]                 },                 "subnets": [                     {                         "name": "[variables('subnetname')]",                         "properties": {                             "addressprefix": "[variables('subnetprefix')]"                         }                     }                 ]             }         },         {             "apiversion": "2015-05-01-preview",             "type": "microsoft.network/networkinterfaces",             "name": "[variables('nicname')]",             "location": "[variables('location')]",             "dependson": [                 "[concat('microsoft.network/publicipaddresses/', variables('publicipaddressname'))]",                 "[concat('microsoft.network/virtualnetworks/', variables('virtualnetworkname'))]"             ],             "properties": {                 "ipconfigurations": [                     {                         "name": "ipconfig1",                         "properties": {                             "privateipallocationmethod": "dynamic",                             "publicipaddress": {                                 "id": "[resourceid('microsoft.network/publicipaddresses',variables('publicipaddressname'))]"                             },                             "subnet": {                                 "id": "[variables('subnetref')]"                             }                         }                     }                 ]             },                     },         {             "apiversion": "2015-05-01-preview",             "type": "microsoft.compute/virtualmachines",             "name": "[variables('vmname')]",             "location": "[variables('location')]",             "dependson": [                 "[concat('microsoft.storage/storageaccounts/', parameters('newstorageaccountname'))]",                 "[concat('microsoft.network/networkinterfaces/', variables('nicname'))]"             ],             "properties": {                 "hardwareprofile": {                     "vmsize": "[variables('vmsize')]"                 },                 "osprofile": {                     "computername": "[variables('vmname')]",                     "adminusername": "[parameters('adminusername')]",                     "adminpassword": "[parameters('adminpassword')]"                 },                 "storageprofile": {                     "imagereference": {                         "publisher": "[variables('imagepublisher')]",                         "offer": "[variables('imageoffer')]",                         "sku" : "[parameters('windowsosversion')]",                         "version":"latest"                     },                     "osdisk" : {                         "name": "osdisk",                         "vhd": {                             "uri": "[concat('http://',parameters('newstorageaccountname'),'.blob.core.windows.net/',variables('vmstorageaccountcontainername'),'/',variables('osdiskname'),'.vhd')]"                         },                         "caching": "readwrite",                         "createoption": "fromimage"                     }                 },                 "networkprofile": {                     "networkinterfaces": [                         {                             "id": "[resourceid('microsoft.network/networkinterfaces',variables('nicname'))]"                         }                     ]                 }             },             "resources": [                 {                     "name": "customscript",                     "type": "extensions",                     "location": "[variables('location')]",                     "apiversion": "2015-05-01-preview",                     "dependson": [                         "[concat('microsoft.compute/virtualmachines/', variables('vmname')]"                     ],                                         "properties": {                         "publisher": "microsoft.compute",                         "type": "customscriptextension",                         "typehandlerversion": "[variables('customscriptextensionversion')]",                         "settings": {                             "fileuris": [                                 "https://<your-blob-here>.blob.core.windows.net/resources/custom-powershell-script.ps1",                                 "http://go.microsoft.com/?linkid=9811175&clcid=0x409"                             ],                             "commandtoexecute": "[concat('powershell.exe -executionpolicy unrestricted -command .\\custom-powershell-script.ps1 -argument1 argument1')]"                         }                     }                 }             ]         }     ] } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -