Questions tagged [invoke-command]

Invoke-Command is a powershell cmdlet that runs commands on local and remote computers.

Invoke-Command is a powershell cmdlet that runs commands on local and remote computers and returns all output from the commands, including errors. With a single Invoke-Command command, you can run commands on multiple computers.

To run a single command on a remote computer, use the ComputerName parameter. To run a series of related commands that share data, use the New-PSSession cmdlet to create a PSSession (a persistent connection) on the remote computer, and then use the Session parameter of Invoke-Command to run the command in the PSSession. To run a command in a disconnected session, use the InDisconnectedSession parameter. To run a command in a background job, use the AsJob parameter.

You can also use Invoke-Command on a local computer to evaluate or run a string in a script block as a command. Windows PowerShell converts the script block to a command and runs the command immediately in the current scope, instead of just echoing the string at the command line.

555 questions
9
votes
3 answers

Multiple commands within ScriptBlock

I'm having trouble running multiple commands within a Scriptblock parameter. All documentation points towards using a semicolon for separation between cmdlets. Using the same methodology of separating cmdlets via a semicolon works on my local…
Alexander Edwards
  • 772
  • 2
  • 7
  • 18
9
votes
3 answers

How do I retain ScriptStackTrace in an exception thrown from within an Invoke-Command on a remote computer?

I'm writing a Powershell script which executes one of the steps in my build/deploy process, and it needs to run some actions on a remote machine. The script is relatively complex, so if an error occurs during that remote activity I want a detailed…
FacticiusVir
  • 2,037
  • 15
  • 28
9
votes
6 answers

PowerShell Splatting the Argumentlist on Invoke-Command

How is it possible to use the parameters collected in a hash table for use with ArgumentList on Invoke-Command? $CopyParams = @{ Source = 'E:\DEPARTMENTS\CBR\SHARE\Target' Destination = 'E:\DEPARTMENTS\CBR\SHARE\Target 2' Structure …
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
9
votes
3 answers

Passing array to another script with Invoke-Command

I'm stuck trying to figure this out. I've seen the other articles and have tried everything, but I'm not getting anywhere. I am trying to pass an array as an argument to another PS script. Here's what I'm trying: $IPArray = Get-Content…
Sameer
  • 103
  • 1
  • 4
8
votes
2 answers

Using Powershell's Invoke-Command to call a batch file with arguments

I want to use Powershell in order to call a batch file on remote machines. This batch file has arguments. Here's what I have so far: $script = "\\fileshare\script.cmd" $server = $args[0] $args [string]::join(',',$args[1 ..…
Jay Spang
  • 2,013
  • 7
  • 27
  • 32
8
votes
2 answers

Powershell - Pass multiple parameters to Invoke-Command

I'm trying to write a one-liner to leverage some of the capabilities of netbackup remotely. I know how to pass parameters to Invoke Command using -args[0] and [1] at the end, with repeating parameters. An example of what I'm trying to accomplish: CC…
itsmrmarlboroman2u
  • 125
  • 1
  • 2
  • 8
7
votes
1 answer

Powershell Invoke RestMethod error 415 Unsupported Media Type

I'm trying to execute Azure Table Storage API using Powershell Invoke-RestMethod but it returns 415 error. Here's Powershell code: $accessKey = "accesskey" $account_name = "storage account" $table_name = "table storage" $date = "Fri, 10 Nov 2017…
ssk3
  • 167
  • 1
  • 1
  • 10
7
votes
2 answers

enter-pssession invoke-command, when to use?

I am writing a script to stop and start services in two remote servers. Here's my question, in my script I did new-pssession and used invoke-command to stop and start services. Do I need to use enter-pssession? Updates: Here's what my script needs…
Ninja
  • 347
  • 3
  • 5
  • 14
7
votes
3 answers

How to start remotely process in PowerShell

I have a problem, I have a script which: Connect with PSSession (I use PSSession with admin account) Stop 2 process Do change on them files Start the 2 process (Problem here) I want to start process on server, so i'm connect with PSSession (No…
Servuc
  • 328
  • 1
  • 4
  • 16
7
votes
2 answers

Start-job vs. Invoke-command -asjob

I'm trying to do basic background jobs in PowerShell 2.0, and I'm seeing different things with start-job and invoke-command -asjob. If I do this: start-job -scriptblock {get-process} I get a job object, but the child job (which is created…
Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
6
votes
2 answers

Get script directory in PowerShell when script is invoked with Invoke-Command

I have a set of PowerShell scripts that include a "common" script, located in the same folder, like this: # some-script.ps1 $scriptDir = Split-Path -Parent $myinvocation.mycommand.path . "$scriptDir\script-utils.ps1" This is fine if the script is…
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
6
votes
2 answers

How do you correctly exit a remote job in a PowerShell session

I have a number of remote jobs started with a Powerhsell invoke-command -session command. In some cases, at least one of the remote child job exits, but the local PS session state reports the jobs as "Running" and the session hangs waiting for the…
cmcginty
  • 113,384
  • 42
  • 163
  • 163
6
votes
1 answer

Get full stack trace from remote session

I'm using Invoke-Expression under remote session and when throws exception - it returns just RemoteException without any stack trace information. Example: try { Invoke-Expression "$command 2>&1" } catch { Write-Host $_ } If I…
Sergey
  • 180
  • 1
  • 9
5
votes
2 answers

Remotely zipping files with PowerShell start-process and invoke-command

I want to be able to remote into a system and zip or unzip files there and have the process signal when it is complete. Start-process works with the -wait parameter to run 7z.exe synchronously from PowerShell. When I try to combine that with…
What Would Be Cool
  • 6,204
  • 5
  • 45
  • 42
5
votes
2 answers

Run Invoke-Command in remote computer as administrator

I'm trying to run invoke-command to launch a powershell script in a powershell file on a remote computer. I'm using the credentials for a user with Administrator privilege. The command needs to be executed by running powershell as an administrator.…
Aditya Nair
  • 514
  • 1
  • 9
  • 18
1
2
3
36 37