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.

470 questions
79
votes
4 answers

How do I pass named parameters with Invoke-Command?

I have a script that I can run remotely via Invoke-Command Invoke-Command -ComputerName (Get-Content C:\Scripts\Servers.txt) ` -FilePath C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 As long as I use default parameters, it…
Sean
  • 1,035
  • 2
  • 9
  • 10
67
votes
8 answers

Invoke a second script with arguments from a script

I have a script that reads a configuration file that results in a set of name value pairs that I would like to pass as arguments to a function in a second PowerShell script. I do not know what parameters will be placed in this configuration file at…
35
votes
4 answers

How to capture the Return Value of a ScriptBlock invoked with Powershell's Invoke-Command

My question is very similar to this one, except I'm trying to capture the return code of a ScriptBlock using Invoke-Command (so I can't use the -FilePath option). Here's my code: Invoke-Command -computername $server {\\fileserver\script.cmd $args}…
16
votes
3 answers

Powershell Invoke-RestMethod over HTTPS

I'm trying to communicate with a service over powershell but I am failing miserably. I suspect it is the certificate and I have googled for the answer and found two options, none of which worked for me. I have also tried to combine the two…
PatrikJ
  • 1,958
  • 1
  • 18
  • 29
15
votes
3 answers

How to prevent InvokeCommandAction from propagating event to parent elements?

I realised that when using an InvokeCommandAcction associated to an EventTrigger, the original event was still routing up to the parent elements until it is handled. Well, I guess it is an expected behavior. But my question is how I can mark the…
Ucodia
  • 6,280
  • 6
  • 40
  • 77
15
votes
5 answers

Invoke Command When "ENTER" Key Is Pressed In XAML

I want to invoke a command when ENTER is pressed in a TextBox. Consider the following XAML: ... …
bitxwise
  • 3,414
  • 2
  • 15
  • 21
15
votes
4 answers

Handle errors in ScriptBlock in Invoke-Command Cmdlet

I am trying to install a service on a remote machine using the powershell. So far I have the following: Invoke-Command -ComputerName $remoteComputerName -ScriptBlock { param($password=$password,$username=$username) $secpasswd =…
flayn
  • 5,042
  • 4
  • 45
  • 68
13
votes
4 answers

Invoking a Java Method in JSP

Setup Mac OSX 10.6.8, Apache Tomcat 6.0.16, Java1.6.0_29, Eclipse IDE Indigo. I asked a similar question to this before at How to execute and include a Java API from a Web App project but the set up has since changed in that i now have the Java code…
Deepend
  • 3,673
  • 15
  • 51
  • 93
12
votes
2 answers

How can I get 7za.exe to run via Powershell Remoting?

I've tried a number of different ways to do this, they all result in the same error. Here is one set of commands: $s = New-PSsession -ComputerName ServerA $job = Invoke-Command -Session $s -Scriptblock { Start-Process -FilePath …
Sean
  • 1,035
  • 2
  • 9
  • 10
11
votes
4 answers

Run Command as administrator in PowerShell script. UAC

OK here is my issue: I am trying to run a script remotely on a server. I am an administrator on both boxes, firewall exceptions are in place, remote admin is enabled, and everything else looks good that i can see. invoke-command -ComputerName…
Tim
  • 211
  • 1
  • 2
  • 7
11
votes
1 answer

ArgumentList parameter in Invoke-Command don't send all array

First short code, then question $session = New-PSSession -ComputerName someServer $servicesList = "Service1", "Service2", "Service3" Invoke-Command -ScriptBlock { Param ($newServicesList) Write-Host $newServicesList } -ArgumentList…
Zabaa
  • 307
  • 1
  • 3
  • 15
10
votes
3 answers

OutOfMemory Exception on remote execution using Powershell Invoke-Command

I am trying to execute an exe on a remote computer using invoke-command. Executing the exe on the remote machine after logging into the machine using remote desktop takes 1GB of memory and executes to completion after a minute. Whereas when I…
manukranthk
  • 109
  • 1
  • 4
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
  • 1,977
  • 14
  • 27
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
  • 1,903
  • 6
  • 25
  • 32
7
votes
1 answer

Invoke-Command faster than the command itself?

I was trying to measure some ways to write to files in PowerShell. No question about that but I don't understand why the first Measure-Command statement below takes longer to be executed than the 2nd statement. They are the same but in the second…
Ben
  • 73
  • 4
1
2 3
31 32