Here-String and loading it with array values

FellowPowershellers:
I have an array loaded up with property object.
I am looping that array, pulling out the names and loading them into a string. It is used by SQL but that is besides the point.

Below is a snippet of what I have. For this posting, I loaded the array with one entry.
I loop my array, put the array values into variables and load the variables into the string - display shows it works perfectly. But when I skip the array to variable and go directly from array into the here-string, it appears to be dumping object info.
I have no problem using the array to variable to here-string, it just seems inefficient.

$VolumnErrorList = @()

$VolumnErrorListObject = New-Object PSObject -property @{
             AuditLogMessageKeyID = 10
             EnvironmentTypeName =  "Non Prod"
             DomainName = "Test"
             }
           
$VolumnErrorList += $VolumnErrorListObject

# now with variables in here-string and works perfectly

Foreach ($volumnerror in $VolumnErrorList)
{
$myAuditLogMessakeyIID = $volumnerror.AuditLogMessageKeyID
$myEnvironmentTypeName = $volumnerror.EnvironmentTypeName
$myDomainName = $volumnerror.DomainName

$myHereString = @"
Insert into mytable (AuditLogMessageKeyID,EnvironmentTypeName,DomainName)
Select  $myAuditLogMessakeyIID, '$myEnvironmentTypeName','$myDomainName'
"@

$myHereString
}

# now without variables and just using the array columns

Foreach ($volumnerror in $VolumnErrorList)
{

$myHereString = @"
Insert into mytable (AuditLogMessageKeyID,EnvironmentTypeName,DomainName)
Select  $volumnerror.AuditLogMessageKeyID, '$volumnerror.EnvironmentTypeName','$volumnerror.DomainName'
"@

$myHereString
}

thx
MG

You have to use subexpressions if you want to expand object properties (or variable properties) inside double quotes (strings). This should be enough:

Foreach ($volumnerror in $VolumnErrorList) {
@"
Insert into mytable (AuditLogMessageKeyID,EnvironmentTypeName,DomainName)
Select  $($volumnerror.AuditLogMessageKeyID), '$($volumnerror.EnvironmentTypeName)','$($volumnerror.DomainName)'
"@
}

Thx Olaf. Works perfectly
MG

1 Like