I am gathering info from separate AD Domains and updating a DB on the front-end side and I have having issues with adding a single quote for the active NIC. The -filter and the -InterfaceAlias needs a single quote and I found a way to add the single quote with a varible “‘{0}"‘“ -f $var, but it will not pass as an Invoke-Command with Creds. If you have any ideas, please let me know. Here is the code:
Thank you for simplifying. So after testing with these updates you made the $QUOTECHRNIndex only shows as a string not the values (added a Write-Host $scriptBlock to show this):
However on the previous code the variable show the single quotes in the right place with the built ScriptBlock and converting to Ssystem.Management.Automating.ScriptBlock:
I think your use of scope-qualified variables. I.e., all those “$global:” scopes just clutters up the code. Is there a reason you’re using explicit scoping?
Using a scriptblock to convey information to a remote machine means that the value of variables from the local machine have to be passed to the remote machine IN the scriptblock. The easiest way to do this is to prefix the local variable name with the “using:” prefix within the scriptblock. Or you can use a “param()” inside the scriptblock and pass the variable name in the -ArgumentList parameter of the Invoke-Code cmdlet.
In my original reply I forgot to include the “using:”. Here’s the corrected line:
You’re correct about the placement of the “$”. It should go in front of the entire variable reference (which includes the scope). That’s what comes from not actually testing the code I write. My bad.
But in your correction you used “$using:QUOTECHRNInd” without the scope keyword. That’ll work in the context of the given code example, but if that same “QUOTECHRNInd” had also been created using another scope (i.e., local or script) then the variable with the most limited scope would have been used.