Convert output of a command or script in a custom single line - azure

Above hyperlink is the output image.
Hi All,
I want to convert the whole output in a single line result in powershell.Below is my code,
$containername = "testoutput"
$storageAccKey = (Get-AzStorageAccountKey -ResourceGroupName
$rgname -AccountName $storageAccountName)[0].value
$storagecontext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccKey
New-AzStorageContainer -Name $containername -Context $storagecontext -Permission Off
Write-Output "Container $($containername) created"

Assuming that this is what you're looking for:
$containername = "testoutput"
$storageAccountName = "teststorage001"
$storageAccKey = (Get-AzStorageAccountKey -ResourceGroupName $rgname -AccountName $storageAccountName)[0].value
$storagecontext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccKey
$result = New-AzStorageContainer -Name $containername -Context $storagecontext -Permission Off
Write-Host "Container $($containername) created in Storage Account-"$($result.Name)",PublicAccess-"$($result.PublicAccess)",LastModified-"$($result.LastModified)""
Either Write-Host or Write-Output can be used. The multiple lines you're seeing is the result of New-AzStorageContainer. Now, I have assigned it to a variable called $result and used in Write-Output.

One way to achieve your requirement is to use if-else condition and check New-AzStorageContainer -Name $containername -Context $storagecontext -Permission Off doesn't result null. Below is the complete code that worked for me.
$rgname = "<YOUR_RESOURCE_GROUP>"
$containername = "<YOUR_CONTAINER_NAME>"
$storageAccountName = "<YOUR_STORAGE_ACCOUNT>"
$storageAccKey = (Get-AzStorageAccountKey -ResourceGroupName $rgname -AccountName $storageAccountName)[0].value
$storagecontext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccKey
if((New-AzStorageContainer -Name $containername -Context $storagecontext -Permission Off) -ne $null){
Write-Output "Container $($containername) created"
}else{
Write-Output "Container $($containername) Not created"
}
RESULTS:

Related

Showing all the blobs from multiple storage containers in Azure using powershell

I am trying to list all the blobs from various containers that i have in an Azure storage account using powershell
So i run the below commands :
$storageAccountName = "contoso"
$resourceGroup = "Contoso-Rg"
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccountName).Value[0]
$context=New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountkey
$containerName = Get-AzStorageContainer -Context $context
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$ctx = $storageAccount.Context
$containerName | ForEach-Object { Get-AzStorageBlob -Container $_ -Context $ctx }
When i run the last command it throws me the error as shown in the screenshot. Any idea what i am doing wrong and how to fix this
Instead of passing $_ to '-contianer' flag you need to pass $_.Name to container parameter in foreach-object.
Posting making the above changes to the shared script we are able to pull all the bolbs inside the container.
Here is the modified script :
$storageAccountName = "<strgAccountName>"
$resourceGroup = "<rgName>"
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccountName).Value[0]
$context=New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountkey
$containerName = Get-AzStorageContainer -Context $context
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$containerName | ForEach-Object { Get-AzStorageBlob -Container $_.Name -Context $context }
Here is the sample output for reference:

Powershell script to delete file from subfolder from blob container

I am trying to delete file from specific folder like from full or diff in blob container but unable to do.
Container_name and then there are two folders full and diff and I want to delete file from full only.
Please help.
$context = New-AzureStorageContext -StorageAccountName "storage_name" -StorageAccountKey "key"
$blobs= Get-AzureStorageBlob -Container "container_name" -blob *DIFF*.bak -Context $context
foreach ($blob in $blobs)
{
$modifieddate = $blob.LastModified
Write-Host $modifieddate
if ($modifieddate -ne $null)
{
$howold = ([DateTime]::Now - [DateTime]$modifieddate.LocalDateTime)
if ($howold.TotalDays -ge 5)
{
Remove-AzureStorageBlob -Blob $blob.Name -Container "container_name" -Context $context
Write-Host $blob.Name
}
}
}
If you want to use Azure PowerShell to delete blobs from one subfolder in one container, you can use the following script :
$StorageAccountKey=" "
$StorageAccountName=" "
$ContainerName=" "
$Token = $null
$Total = 0
$MaxCount=5000
$context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
do
{
$Blobs = Get-AzStorageBlob -Container $ContainerName -MaxCount $MaxCount -ContinuationToken $Token -Context $context -Prefix "your fodler name"
if($Blobs.Length -le 0) { Break;}
$Token = $Blobs[$blobs.Count -1].ContinuationToken;
foreach($blob in $blobs){
Remove-AzStorageBlob -Blob $blob.Name -Container $ContainerName -Context $context
}
}
While ($Token -ne $null)
Update
I use the latest version Azure Az module to do a test.

Get-AzStorageBlob - Specific Folder

I have the following script to delete files older than 35 days of age.
How can I make it work only on a particular folder? I'm not sure how to incorporate the $FolderName into the Get-AzStorageBlob command as I can't see any specific switches to use.
$StorageAccountName = "#"
$StorageAccountKey = "#"
$ContainerName = "container"
$CleanupOlderThan35Days = [DateTime]::UtcNow.AddDays(-35)
$FolderName = "folder"
$Ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzStorageBlob -Container "$ContainerName" -Context $Ctx | Where-Object { $_.LastModified.UtcDateTime -lt $CleanupOlderThan35Days } |Remove-AzStorageBlob
You could use -prefix to filter the "folder" like this -Prefix $FolderName/, note you should add the suffix / in the prefix parameter.
For example,
$StorageAccountName = "xx"
$StorageAccountKey = "xx"
$ContainerName = "sss"
$CleanupOlderThan35Days = [DateTime]::UtcNow.AddDays(-35)
$FolderName = "test"
$Ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzStorageBlob -Container "$ContainerName" -Context $Ctx -Prefix $FolderName/ | Where-Object { $_.LastModified.UtcDateTime -lt $CleanupOlderThan35Days } |Remove-AzStorageBlob
The result with your partial command,

Azure PowerShell Download blob contents from container

I'm trying to download sme blob files from an azure storage account.
There are a mix of other containers and blockblobs in the parent container, I need to download just the blockblobs and not the other containers, I can't find a way of seperating them out, also I need to download some blobs from a conatiner within a container.
My code will download all the contents in the parent blob, including all sub containers.
$sub = "MySub"
$staccname = "straccname1234"
$key = "sdcsecurekeythinghere"
$ctx = New-AzureStorageContext -StorageAccountName $staccname `
-StorageAccountKey $key
$cont = "data\download\files.001" ##the container includes other cntainers and subcontainers
$dest = "C:\blb-Downloads"
Select-AzureSubscription -SubscriptionName $sub –Default
Set-AzureSubscription -Currentstaccname $staccname -SubscriptionName $sub
Get-AzureStorageBlob -Container $cont -Context $ctx
$blobs = Get-AzureStorageBlob -Container $cont -Context $ctx
$blobs | Get-AzureStorageBlobContent –Destination $dest -Context $ctx
There are approx 75 files in the parent blob and 123 files in data\downloads.
Using the newer Azure PowerShell Az module, you can use Get-AzStorageBlob to list all the block blobs from the container, then use Get-AzStorageBlobContent to download the blobs.
As already shown by #George Wallace, we can use Where-Object or its alias ? to filter block blob types.
Demo:
$resourceGroup = "myResourceGroup"
$storageAccount = "myStorageAccount"
$container = "myContainerName"
$destination = "./blobs"
# Create destination directory if it doesn't exist
if (-not (Test-Path -Path $destination -PathType Container)) {
New-Item -Path $destination -ItemType Directory
}
# Get storage account with container we want to download blobs from
$storageAccount = Get-AzStorageAccount -Name $storageAccount -ResourceGroupName $resourceGroup
# Get all BlockBlobs from container
$blockBlobs = Get-AzStorageBlob -Container $container -Context $storageAccount.Context
| Where-Object {$_.BlobType -eq "BlockBlob"}
# Download each blob from container into destination directory
$blockBlobs | Get-AzStorageBlobContent -Destination $destination -Force
Can you not just run the following and limit it to BlockBlobs only?
Get-AzureStorageBlob -Container $cont -Context $ctx | ? {$_.BlobType -eq "BlockBlob"}
This works for me
$storageaccountname = "jawadtestsaacc"
$sastoken = ""
$ContainerName = "access"
$Ctx = New-AzStorageContext -StorageAccountName $storageaccountname -SasToken $sastoken
Get-AzStorageBlob -Container "$ContainerName" -Blob "Az.json" -Context $Ctx | Get-AzStorageBlobContent
or
Get-AzStorageBlobContent -Container "$ContainerName" -Blob "Az.json" -Context $Ctx

copying one blob container data into another blob container in azure throught run books

i have write a script to copy one blob container data into another blob container
it copying only blob name and showing completed inside VHDs are not copying can any one please help in script,,,
what exactly i am trying is
ex:- storage account 1 contain 3 vhds
i have created new storage account in the same log and trying to copy all vhds into new storage account
In the script i have passed few parameters to copy the vhds with date
when i am running that script it only creating name but Vhds are not copying
Thanks in Advance
According to your description, you want to copy all blobs to another storage accounts, we can use this script to do it:
Add this to a new runbook:
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$RGName = "vm"
$SAName = "jasondisk321"
$ConName = "vhd"
$key = "UUzQRoWeIMHzwzwJW9LxtgmwaJJS/Ac3DoXnPMHFIbUmupDpQ+KXCWG8ISJ4E20zjq7ugPtgN4vtVIv3A4m2Pg=="
$Ctx = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $Key
$List = Get-AzureStorageBlob -Blob * -Container $ConName -Context $Ctx
$List = $List.ICloudBlob.Uri.AbsoluteUri
$storageAccount = "jasondisk322"
$storageKey = "6lRJq6hTS1aHfVF4/iWskq/QS+tu4Jm/2zdz7Mo6AINGZOQKUiHtOAKmdZhBAWbcNEcBQq0YxZjXHgHha/iUKw=="
$destContext = New-AzureStorageContext –StorageAccountName $storageAccount -StorageAccountKey $storageKey
$containerName = "vhd"
foreach ( $l in $list ){
$bn = ($l -split '/')[4]
Start-AzureStorageBlobCopy -srcUri $l -context $Ctx -DestContainer $containerName -DestBlob $bn -DestContext $destContext
}
If you want to use powershell on your local PC, we can use this script:
Login-AzureRmAccount
$RGName = "vm" #source storage account resource group name
$SAName = "jasondisk321" #source storage account name
$ConName = "vhd" #source container name
$key = "UUzQRoWeIMHzwzwJW9LxtgmwaJJS/Ac3DoXnPMHFIbUmupDpQ+KXCWG8ISJ4E20zjq7ugPtgN4vtVIv3A4m2Pg=="#source storage account key
$Ctx = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $Key
$List = Get-AzureStorageBlob -Blob * -Container $ConName -Context $Ctx
$List = $List.ICloudBlob.Uri.AbsoluteUri
$storageAccount = "jasondisk322"#destination storage account name
$storageKey = "6lRJq6hTS1aHfVF4/iWskq/QS+tu4Jm/2zdz7Mo6AINGZOQKUiHtOAKmdZhBAWbcNEcBQq0YxZjXHgHha/iUKw=="#destination storage account key
$destContext = New-AzureStorageContext –StorageAccountName $storageAccount -StorageAccountKey $storageKey
$containerName = "vhd"#destination container name
foreach ( $l in $list ){
$bn = ($l -split '/')[4]
Start-AzureStorageBlobCopy -srcUri $l -context $Ctx -DestContainer $containerName -DestBlob $bn -DestContext $destContext
}
You can do it with AzCopy:
AzCopy /source:https://[SourceStorageAccountName].blob.core.windows.net/vhds /dest:https://[DestStprageAccountName].blob.core.windows.net/vhds /sourcekey:<here-is-source-key> /destkey:<here-is-destination-key> /Pattern:[vhd-name].vhd
more info: https://learn.microsoft.com/en-us/azure/storage/storage-use-azcopy
If you want to automate, then you can use powershell using Start-AzureStorageBlobCopy:
Select-AzureSubscription "my subscription"
### Source VHD (West US) - anonymous access container ###
$srcUri = "http://mwwestus1.blob.core.windows.net/source/testcopy1.vhd"
### Target Storage Account (East US) ###
$storageAccount = "mweastus1"
$storageKey = "STORAGEACCOUNTKEY"
### Create the destination context for authenticating the copy
$destContext = New-AzureStorageContext –StorageAccountName $storageAccount `
-StorageAccountKey $storageKey
### Target Container Name
$containerName = "copiedvhds"
### Create the target container in storage
New-AzureStorageContainer -Name $containerName -Context $destContext
### Start the Asynchronous Copy ###
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `
-DestContainer $containerName `
-DestBlob "testcopy1.vhd" `
-DestContext $destContext
more info:https://www.opsgility.com/blog/windows-azure-powershell-reference-guide/copying-vhds-blobs-between-storage-accounts/
UPDATE:
AzCopy /Source:https://myaccount1.blob.core.windows.net/myContainer/ /Dest:https://myaccount2.blob.core.windows.net/myContainer/ /SourceKey:key1 /DestKey:key2 /Pattern:ab /SyncCopy

Resources