Invoke-Expression inside of Invoke-Command ScriptBlock

I apologize if I’m way off course, but I’ve tried everything I can think of.

I’m Trying to launch cmd with some parameters on a remote machine, with the ultimate goal of getting a powershell script kicked off running under my credentials, but getting a:

‘Cannot evaluate parameter ‘Command’ because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input’ error.

The reason i’m doing this is the remote script needs to connect to SQL, but running into the issue of the script tries to connect to the sql instance as ‘NT Authority’ rather than my domain credentials.

Essentially the final product should be script 1 --> invokes script 2 on remote machines --> pulls data from sql --> does stuff. I have everything working except for connecting to sql with the right creds.

foreach ($server in $servers)

Invoke-Command -Computername $server -scriptblock {
Invoke-Expression {"cmd /c start powershell -command {C:\scripts\test.ps1 -p1 '$var1' -p2 '$var2' -p3 '$var3' -p4 '$var4'};"}} -ArgumentList $var1, $var2, $var3, $var4



Something more like this, perhaps?

Invoke-Command -Computername $server -ScriptBlock {
    param($var1, $var2, $var3, $var4)
    & C:\scripts\test.ps1 -p1 $var1 -p2 $var2 -p3 $var3 -p4 $var4
} -ArgumentList $var1, $var2, $var3, $var4

When I tried that, the remote script executes as expected, however when the connection to sql server is created, it tries to connect via ‘NT Authority’ instead of the credentials I’m running script 1 as. I’m wondering if i could open a powershell or cmd process remotely as my domain credentials.I’ve tried using the -credential on the Invoke-Command from script 1

Here’s the code from script 2 (running on remote server):


$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = servername; Database = databasename;Integrated Security = True;"

$dataset = Invoke-SqlCmd -Query $target_query -ConnectionString $SqlConnection.ConnectionString


What is “C:\scripts\test.ps1” doing ?

Is it just running a tsql query?

If so, why not just use Invoke-SqlCmd directly ?

See example 3: