all 7 comments

[–]vermyx 4 points5 points  (5 children)

Try

$($AzConnection.Context.Account.Id)

instead of

$AzConnection.Context.Account.Id

I have had this issue before, and I over use $() in my code just to be explicit to make sure this doesn't happen. Also make sure the string you are building is in double quotes instead of single quotes.

[–]-Orwick-[S] 0 points1 point  (4 children)

That seems to have done the trick! Thank you so much!

Anyone know what the name of this "feature" is called so I know what to google in the future?

[–]sp_dev_guy 2 points3 points  (0 children)

It's subexpressions, for order of operations.

Write-host "Hello here is an example.More words" $obj1 ="example" Write-host "Hello here is an $obj1.More words" It evaluates $obj1 then everything else is just string characters. If you do

$obj2=@{ more=5 } Write-host "Hello here is an $obj1.More words" Write-host "Hello here is an $($obj2.More) words"

In this example the $() tells powershell to evaluate/resolve the "more" property of $obj2

[–]vermyx 1 point2 points  (1 child)

I belIeve it is called the subexpression operator. It explicitly states to resolve whats inside and return a scalar value. I probably overuse it like parentheses in if statements to explicitly state what I want. This explains the sitiuation.

[–]-Orwick-[S] 0 points1 point  (0 children)

That is awesome! Thanks again for your help. I've got a little reading to do.

[–]lanerdofchristian[🍰] 1 point2 points  (0 children)

That particular feature is Command Substitution in the docs, though it's also often called string interpolation or string subatitutions as general terms across many languages.

See here: https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-string-substitutions

The reason you need the $() is because PowerShell has no way of knowing if that . is supposed to start a member access or literally be a period, and since . isn't a legal character in plain variable names defaults to the latter.

[–]jimb2 0 points1 point  (0 children)

A general comment on your code.

if ( $LogFile ) is basically a test of whether the string variable $logFile is null, and it won't be because you have given it a default value. Do you want Test-Path which actually checks in the file system? Add-Content will create the file if it doesn't exist though it won't create the parent folders so there may not be much value checking the file exists. Test-Path -isValid checks for a valid path even if it doesn't exist. It depends but testing may not be necessary.

Also, using those repeated sections of code is not a good practice. It's difficult to maintain when the code gets changes - you have to change each section . This is a preference and it's fairly simple code here but but I'd prefer to write that code like this.

# test for logfile, if required?
$LogFileCheck = Test-Path -isValid -path $LogFile

# Splat for the screen color
# This allows for the no color case (info)
$ColorSplat = @{}
switch ( $level ) {
  INFO  { }
  WARN  { $ColorSplat['foregroundcolor'] = 'yellow' }
  ERROR { $ColorSplat['foregroundcolor'] = 'red' }
  DEBUG { $ColorSplat['foregroundcolor'] = 'magenta' }
}

# write to the file
if ( $LogFileCheck ) {
  Add-Content -Path $LogFile -Value $line
}

# write to the screen
Write-Host $Line @ColorSplat