0

I am looking for a way to pipe many (100 paired variables) items into this powershell script. Each single command has two variables ResourceGroup and AccountName. I can build an array, but not sure how to get the script to execute the paired variables.

EDIT: Would there be any way to import these variables from CSV , or pull them directly.

$rgName = "<resource-group>"
$accountName = "<storage-account>"
$location = "<location>"

# Create a storage account with MinimumTlsVersion set to TLS 1.1.
New-AzStorageAccount -ResourceGroupName $rgName `
    -AccountName $accountName `
    -Location $location `
    -SkuName Standard_GRS `
    -MinimumTlsVersion TLS1_1

# Read the MinimumTlsVersion property.
(Get-AzStorageAccount -ResourceGroupName $rgName -Name $accountName).MinimumTlsVersion

# Update the MinimumTlsVersion version for the storage account to TLS 1.2.
Set-AzStorageAccount -ResourceGroupName $rgName `
    -AccountName $accountName `
    -MinimumTlsVersion TLS1_2

# Read the MinimumTlsVersion property.
(Get-AzStorageAccount -ResourceGroupName $rgName -Name $accountName).MinimumTlsVersion

1 Answer 1

1

If you want to group variables together, you could use an array of System.Management.Automation.PSCustomObject, then iterate this array and run your code:

param (
    [Parameter(Mandatory = $true)]
    [PSCustomObject[]]
    $StorageAccountData
)

foreach ($obj in $StorageAccountData) {
    $rgName = $obj.ResourceGroupName
    $accountName = $obj.AccountName
    $location = $obj.Location

    # Create a storage account with MinimumTlsVersion set to TLS 1.1.
    New-AzStorageAccount -ResourceGroupName $rgName `
        -AccountName $accountName `
        -Location $location `
        -SkuName Standard_GRS `
        -MinimumTlsVersion TLS1_1

    # Read the MinimumTlsVersion property.
    (Get-AzStorageAccount `
        -ResourceGroupName $rgName `
        -Name $accountName).MinimumTlsVersion

    # Update the MinimumTlsVersion version for the storage account to TLS 1.2.
    Set-AzStorageAccount `
        -ResourceGroupName $rgName `
        -AccountName $accountName `
        -MinimumTlsVersion TLS1_2

    # Read the MinimumTlsVersion property.
    (Get-AzStorageAccount `
        -ResourceGroupName $rgName `
        -Name $accountName).MinimumTlsVersion
}

Then you could run this script like so:

PS C:\Users\user> $storageAccountData = @(
>>     [PSCustomObject]@{
>>         ResourceGroupName = 'storageRG'
>>         AccountName = 'storage1'
>>         Location = 'australiaeast'
>>     }
>>     [PSCustomObject]@{
>>         ResourceGroupName = 'storageRG'
>>         AccountName = 'storage2'
>>         Location = 'australiaeast'
>>     }
>> )
PS C:\Users\user> script.ps1 -StorageAccountData $storageAccountData

If you want to pipe this into the script, you can create a function and use ValueFromPipeline:

function MyFunction {
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $true, 
            ValueFromPipeline = $true
        )]
        [PSCustomObject[]]
        $StorageAccountData
    )
    # rest of your code
}

Then pass the array of PSCustomObject down then pipeline to this function with $storageAccountData | MyFunction. Make sure to dot source the script to load the function into your preferred scope before calling the function.

Update

If you wanted to read from a CSV file(e.g. storage-accounts.csv):

AccountName,ResourceGroupName,Location
storage1,storageRG,australiaeast
storage2,storageRG,australiaeast

You just need use Import-Csv:

$storageAccountData = Import-Csv -Path .\storage-accounts.csv

foreach ($obj in $storageAccountData) {
    # rest of your code
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.