Save-AzureRmVMImage mandatorily creates invalid blob container name - azure

I am capturing an ARM VM image to use it in create from image operations.
However the Save-AzureRmVMImage command creates mandatorily (or just by default?) such a container and blob name what seems to be invalid for other commands.
The parts of "/Microsoft.Compute/ in the container name and "osDisk.xxxx" in the blob name are implicitly placed by the Save-AzureRmVMImage command.
To clarify:
I used the Save-AzureRmVMImage -DestinationContainerName 'vhds' -VHDNamePrefix 'template'... but the command creates the container system/Microsoft.Compute/Images/vhds + "invents" the blobname which contains the osdisk. (dot) part. Both of them are not coming from me... and can not be prevented-
First of all this name seems to be not usable in the New-AzureRmVM operation. (see my detailed description at Creating new ARM VM from captured image: Blob name in URL must end with '.vhd' extension error
As a workaround to the error described above I found out that I will copy theimage to an other blob name which does not contains . (dot). However it turned out that my fully tested and otherwise working copy script also throws error for that blob name...
Copy source code (again, it is fully functional for other blobs)
(note: This question about the Save-AzureRmVMImage issue, or how it can its weird and invalid name workarounded in subsequent operations. So the question is not about this copy script. The script here is just demonstrating a diagnostic experience.
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName "Visual Studio Premium with MSDN"
$sourceRgName = "rg-wp"
$sourceName = "mystorage"
$sourceContainerName = "system/Microsoft.Compute/Images/vhds" #the dots seems to be invalid
$sourceBlobName = "template-osDisk.be7b0cf4-a28b-47f9-89c7-43887f1570ab.vhd" #the dots seems to be invalid
$destinationRgName = "rg-wp"
$destinationName = "mystorage"
$destinationContainerName = "vhds"
$destinationBlobName = "template.vhd"
$sourceKeys = Get-AzureRmStorageAccountKey -Name $sourceName -ResourceGroupName $sourceRgName
$destinationKeys = Get-AzureRmStorageAccountKey -Name $destinationName -ResourceGroupName $destinationRgName
$contextSource = New-AzureStorageContext -StorageAccountName $sourceName -StorageAccountKey $sourceKeys.Key1
$contextDestination = New-AzureStorageContext -StorageAccountName $destinationName -StorageAccountKey $destinationKeys.Key1
Start-AzureStorageBlobCopy -Context $contextSource -SrcContainer $sourceContainerName -SrcBlob $sourceBlobName -DestContext $contextDestination -DestContainer $destinationContainerName -DestBlob $destinationBlobName
# Wait for copy to complete
Get-AzureStorageBlobCopyState -Context $contextDestination -Container $destinationContainerName -Blob $destinationBlobName -WaitForComplete
Full error message:
[ERROR] Start-AzureStorageBlobCopy : Container name 'system/Microsoft.Compute/Images/vh
[ERROR] ds' is invalid. Valid names start and end with a lower case letter or a number
[ERROR] and has in between a lower case letter, number or dash with no consecutive dash
[ERROR] es and is 3 through 63 characters long.
[ERROR] At D:\work\.vsonline\Azure\PowerShell\PowerShell\copy blob.ps1:40 char:1
[ERROR] + Start-AzureStorageBlobCopy -Context $contextSource -SrcContainer $sourceConta
[ERROR] ine ...
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR] ~~~
[ERROR] + CategoryInfo : NotSpecified: (:) [Start-AzureStorageBlobCopy],
[ERROR] ArgumentException
[ERROR] + FullyQualifiedErrorId : System.ArgumentException,Microsoft.WindowsAzure.
[ERROR] Commands.Storage.Blob.Cmdlet.StartAzureStorageBlobCopy
[ERROR]

The reason your code is failing is because the container name you're specifying in your script is invalid. Please see this link for a valid container name: https://msdn.microsoft.com/en-us/library/azure/dd135715.aspx.
Based on the information you provided in comments above, please try the following:
$sourceContainerName = "system"
$sourceBlobName = "Microsoft.Compute/Images/vhds/template-osDisk.be7b0cf4-a28b-47f9-89c7-43887f1570ab.vhd

Related

Error while adding tags to vm with powershell after reading from csv

I am trying to add tags to azure vm by reading from csv file with a powershell script. I want to read the value via loop and add to existing tags of vm, if any. Below is my code and the respective errors.
$data = Import-CSV C:\Documents\tags-vms.csv
foreach($info in $data){
$tags = (Get-AzResource -ResourceGroupName policyResourceGroup -Name $info.psobject.properties.value[0]).Tags
$scriptBlock = [scriptblock]::Create('#{$info.Tags}')
$newtags = (& $scriptBlock)
$tags += $newtags
Write-Host $tags
}
Now the error is
Exception calling "Create" with "1" argument(s): "At line:1 char:13
2021-12-20T16:01:09.2145247Z + #{$info.Tags}
2021-12-20T16:01:09.2147301Z + ~
2021-12-20T16:01:09.2149246Z Missing '=' operator after key in hash literal.
2021-12-20T16:01:09.2152282Z At line:1 char:13
2021-12-20T16:01:09.2154454Z + #{$info.Tags}
2021-12-20T16:01:09.2156590Z + ~
2021-12-20T16:01:09.2166796Z The hash literal was incomplete."
2021-12-20T16:01:09.2168854Z At C:\agent\_work\_temp\e8ee61c2-c3f4-4ea5-99ac-4feb671fff57.ps1:18 char:1
2021-12-20T16:01:09.2169842Z + $scriptBlock = [scriptblock]::Create('#{$info.Tags}')
2021-12-20T16:01:09.2170521Z + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2021-12-20T16:01:09.2171329Z + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
2021-12-20T16:01:09.2171826Z + FullyQualifiedErrorId : ParseException
Can someone please help.
**csv file contains**
VmName Tags
test-vm01 "loc"="us"
test-vm02 "Loc"="Us";"doseage"="Second"
I think your issue lies in this line and how you're retrieving the tag info from the CSV object from your foreach loop:
$scriptBlock = [scriptblock]::Create('#{$info.Tags}')
In the previous line in your script, to define the $tags variable, you retrieve the value of the name of the VM in the CSV object using ($info.psobject.properties.value[0]). I would attempt to use that same format in the next line, but modifying it to retrieve the tag part of the CSV object using the index of 1:
So instead of:
$scriptBlock = [scriptblock]::Create('#{$info.Tags}')
use:
$scriptBlock = [scriptblock]::Create('#{$info.psobject.properties.value[1]}')

Create Automation Action Group in Azure using powershell

I want to use this command Set-AzActionGroup to action the standard automation runbook restart VM but there don't seem to be any examples of this around. To do this in Set-AzActionGroup I need to reference the VM, an automation account and some things called service uri and webhook resource id (which I think refers to the runbook). Has anyone got a fully specified example? I have a azure automation account but would need a webhook.
Just try the PowerShell below to create an Action group with restart VM AutomationRunbookReceiver:
$automationAccountResourceGroup = ""
$automationAccountName = ""
$webhookName = "Webhook20210316"
$receiverEmail = ""
$actionGroupName = ""
#create automationRunbookReceivers
$webhook = New-AzAutomationWebhook -Name $webhookName -IsEnabled $true -ExpiryTime "10/2/2030" -RunbookName "RestartAzureVmInResponseToVmAlertGlobalRunbook" -ResourceGroup $automationAccountResourceGroup -AutomationAccountName $automationAccountName -Force
$serviceURI = $webhook.WebhookURI
$automationAccountID = (Get-AzResource -ResourceGroup $automationAccountResourceGroup -name $automationAccountName).ResourceId
$WebhookResourceID = $automationAccountID + "/webhooks/" + $webhook.name
$AutomationRunbookReceiver = New-AzActionGroupReceiver -Name 'restartVM' -RunbookName 'Restart VM' -AutomationAccountId $automationAccountID.ToLower() -IsGlobalRunbook -AutomationRunbookServiceUri $serviceURI -WebhookResourceId $WebhookResourceID
#create email receivers
$EmailReceiver = New-AzActionGroupReceiver -Name $receiverEmail -EmailReceiver -EmailAddress $receiverEmail
Set-AzActionGroup -ResourceGroupName $automationAccountResourceGroup -Name $actionGroupName -ShortName $actionGroupName -Receiver $EmailReceiver,$AutomationRunbookReceiver
Result:
I also tested it on my side under some rules and it works as excepted:

Azure Powershell Command failure when using Invoke-AzsRegistrationValidation

I have been following the steps given here(https://learn.microsoft.com/en-us/azure-stack/operator/azure-stack-validate-registration?view=azs-2008&tabs=az) to validate a subscription but when i run the invoke command it fails with the below error:
> PS C:\WINDOWS\system32> Invoke-AzsRegistrationValidation
> -RegistrationSubscriptionID $subscriptionID Invoke-AzsRegistrationValidation : Parameter set cannot be resolved
> using the specified named parameters. At line:1 char:1
> + Invoke-AzsRegistrationValidation -RegistrationSubscriptionID $subscri ...
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : InvalidArgument: (:) [Invoke-AzsRegistrationValidation], ParentContainsErrorRecordExcept
> ion
> + FullyQualifiedErrorId : AmbiguousParameterSet,Invoke-AzsRegistrationValidation
The obvious question is do you have the correct subscriptionID in $SubscriptionID
The doc you reference suggest some logs might be created, is there anything in them that help?
Log location (contains PII):
C:\Users\username\AppData\Local\Temp\AzsReadinessChecker\AzsReadinessChecker.log**
s
Report location (contains PII):
C:\Users\username\AppData\Local\Temp\AzsReadinessChecker\AzsReadinessCheckerReport.json

Using Azure Automation Account, Copy files from one Fileshare to another Fileshare across Storage

I am trying to copy one file (x) from a fileshare (y) of storage (z), to a fileshare (a) of storage (b).
$StorageAccountName = "z"
$StorageAccessKey =
"md226fZvTKPKaCd9TxDGPn4mitCPfvBvgzElwoqIgpbf2Pe09GKKfNJlMcK2EXXqRLqOrhBslybaE1Cpz2BcPg=="
$context1=New-AzureStorageContext $StorageAccountName $StorageAccessKey
$DestStorageAccountName = "b"
$DestStorageAccessKey = "xZjU0r5g5fU+/ieAbwc22OmVtFY5BJdQuF8OZsFd7hGHYLzQFGg9ZCpsVnQi5u6Wi/nigb8jdupUGo0YaXBphg=="
$destcontext1=New-AzureStorageContext $DestStorageAccountName $DestStorageAccessKey
$SrcPath = "https://z.file.core.windows.net/y/x"
$SrcShare = "y"
$DestPath = "https://b.file.core.windows.net/a"
$DestShare = "a"
Start-AzureStorageFileCopy -SrcFilePath $SrcPath -SrcShareName $SrcShare -DestShareName $DestShare -
DestFilePath $DestPath -Context $context1 -DestContext $destcontext1 -Force
It gives me error as below:
Start-AzStorageFileCopy: The given path/prefix 'https:' is not a valid name for a file or directory or does match the requirement for Microsoft Azure File Service REST API.
Thanks in Advance!
Please change the source and destination paths. In your particular scenario they should represent the path relative to the share.
$StorageAccountName = "z"
$StorageAccessKey =
"md226fZvTKPKaCd9TxDGPn4mitCPfvBvgzElwoqIgpbf2Pe09GKKfNJlMcK2EXXqRLqOrhBslybaE1Cpz2BcPg=="
$context1=New-AzureStorageContext $StorageAccountName $StorageAccessKey
$DestStorageAccountName = "b"
$DestStorageAccessKey = "xZjU0r5g5fU+/ieAbwc22OmVtFY5BJdQuF8OZsFd7hGHYLzQFGg9ZCpsVnQi5u6Wi/nigb8jdupUGo0YaXBphg=="
$destcontext1=New-AzureStorageContext $DestStorageAccountName $DestStorageAccessKey
$SrcPath = "test.txt" #assuming the name of the source file is "test.txt"
$SrcShare = "y"
$DestPath = "test.txt" #assuming you want to copy the file and keep the same name
$DestShare = "a"
Start-AzureStorageFileCopy -SrcFilePath $SrcPath -SrcShareName $SrcShare -DestShareName $DestShare -
DestFilePath $DestPath -Context $context1 -DestContext $destcontext1 -Force
From the documentation link:
-SrcFilePath Specifies the path of the source file relative to the source directory or source share.
-DestFilePath Specifies the path of the destination file relative to the destination share.

Why do I get error "Name or Service not known" when encrypting Azure VM?

I am trying to encrypt a VM in Azure using the following code
$keyVault = Get-AzureRmKeyVault –VaultName “azkeyvaultWestUS” -ResourceGroupName “azkeyvault”;
$diskEncryptionKeyVaultUrl = $keyVault.VaultUri;
$keyVaultResourceId = $keyVault.ResourceId;
$keyEncryptionKeyUrl = (Get-AzureKeyVaultKey –VaultName “azkeyvaultWestUS” –Name “azpavdiskencryption”).Key.kid;
But when I tried to run it, I got the message
Get-AzKeyVaultKey: Name or Service not known
After the instruction ending in .Key.kid
Does anyone know how to fix this?
Thanks in advance.
Try accessing it separately and it should bye key.id
$Key = Add-AzureKeyVaultKey -VaultName "azkeyvaultWestUS" -Name 'azpavdiskencryption'
#get uri
$keyEncryptionKeyUrl = $Key.key.kid

Resources