I am looking to pass a bunch of variables to the parameters of a function but since the script needs to be run unattended I will need to pass these variables within the script. i.e via invoke-expression or otherwise.
Below is the functions I am using:
Function Get-AzStorageContainerSASUri {
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
$TenantId,
[Parameter(Mandatory)]
[String]$SubscriptionId,
[Parameter(Mandatory)]
[String]$ResourceGroupName,
[Parameter(Mandatory)]
[String]$StorageAccountName,
[Parameter(Mandatory)]
[String]$ContainerName,
[Parameter(Mandatory=$false)]
[Int]$TokenDurationMinutes = 15,
[Parameter(Mandatory)]
[System.Management.Automation.PSCredential]$Credential
)
# Connect as Service Principal
Connect-AzAccount -TenantId $TenantId -Credential $Credential -ServicePrincipal -WarningAction 'SilentlyContinue' | Out-Null
Select-AzSubscription -SubscriptionId $SubscriptionId | Out-Null
# Load the Storage Account context
$StorageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageAccountName)[0].Value
$StorageContext = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
# Generate a full URI to the container with SAS token
$SasParam = @{
Context = $StorageContext
Name = $ContainerName
ExpiryTime = (Get-Date).AddMinutes($TokenDurationMinutes)
Permission = 'rw'
FullUri = $true
}
$DestinationUri = New-AzStorageContainerSASToken @SasParam
Return $DestinationUri
}
And here is the main script where I have put an invoke expression to assign the values to the parameters.
Foreach ($Source in $Sources) {
$Destination = Get-AzStorageContainerSASUri | Invoke-Expression '[string]$TenantId = "xxxxx-xxxxxx-xxxxx"', '[string]$SubscriptionId = "xxxxxxx-xxxxxx-xxxx"', '[string]$ResourceGroupName = "xxxxxx-xxxxx-xxxxx"', '[string]$StorageAccountName = "woukasrtest211202"', '[string]$Credential = Import-Clixml -Path "C:\AzCopy\xxxxxx-xxxxxxx-xxxxxx.credential"'
Sync-SftpToAzStorage -Source $Source -Destination $Destination ...
}
This should theoretically use both functions I've written without me needing to input for the parameters in the first function. However, I either get the error: "Expressions are only allowed as the first element of a pipeline." OR the invoke-expression doesn't seem to run at all and I get:"cmdlet Get-AzStorageContainerSASUri at command pipeline position 1 Supply values for the following parameters: TenantId:"
I believe its invoke-expression or invoke-command but I can't seem to get it working the way that is perhaps intended to achieve my desired outcome.
Thanks, Evan
Invoke-Expression
(iex
) should generally be avoided and used only as a last resort, due to its inherent security risks. Superior alternatives are usually available. If there truly is no alternative, only ever use it on input you either provided yourself or fully trust - see this answer. – mklement0Get-AzStorageContainerSASUri
function, consider the use of splatting. – mklement0