I’m querying a database in the cloud, and using the data from the results to set variables in my caller script.
The variables drive a build process where I dynamically build out Azure resources.
The variable is not setting in the caller script. I’m a total PowerShell newbie so I’m not quite sure what I’m doing wrong. The function does work, and does return data, it just doesn’t seem to make it back to the caller or I’m setting the variable incorrectly in the caller.
Here is the function I’m using to query for the data, and to send the results back to the calling script:
Function Get-SubscriptionValues {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string] $tenantid,
[Parameter(Mandatory = $true)]
[string] $subscriptionid)
Process
{
$thequery = "SELECT prefix, suffix, location FROM dbo.tablenamehere WHERE sub_id = '$subscriptionid'"
$SQLConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=tcp:;Initial Catalog=;Persist Security Info=False;User ID=;Password=;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $thequery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$thedata = $DataSet.Tables[0]
return $thedata
}
}
In the caller script I set the variables like so:
$prefixname = ($thedata).prefix
I tried putting a pause in between when I called the function and when I set the variable to see if maybe the cloud was lagging on returning data to the caller, but the pause didn’t seem to have any affect.