0

Trying to run a logon script on a domain controlled computer through group policy. Works great on the Server with SQLserver module installed, but when converting code to use invoke-command and run from domain controlled computer, input into database is empty -- it changes from null to empty.

Code that works:

$var1 = $env:xxx
$var2 = $env:xxx
$var3 = $env:xxx

invoke-sqlcmd -Query "insert into tblxxx (abc, def, ghi) values('$var1', '$var2', '$var3')" -connectionstring <connectionstring>

Code that doesn't work:

$var1 = $env:xxx
$var2 = $env:xxx
$var3 = $env:xxx

invoke-command -computername <server> {invoke-sqlcmd -Query <same query as above> -connectionstring <connectionstring>}

What am I doing wrong?

Carmine
  • 19
  • 6

1 Answers1

1

Use $using:var1 inside the Invoke-Command because the variable $var1 is not known in the new scope.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Patrick
  • 2,128
  • 16
  • 24
  • Do I leave the apostrophe's around $using:var1 -- '$using:var1'? If I do, its still empty on input, if I take out the apostrophe's I get "invalid column name" and what the variable is set to -- so at least its reading the variable now but not inputting into the database... – Carmine Nov 08 '19 at 13:16
  • My test only mentioned in my comment above was only using 1 input column causing the issue -- this works thank you! – Carmine Nov 08 '19 at 13:31