Home > other >  Convert output of a command or script in a custom single line
Convert output of a command or script in a custom single line

Time:12-02

output image 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"

CodePudding user response:

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.

CodePudding user response:

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:

enter image description here

  • Related