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
3
votes
1 answer

How to pass -Verbose parameter to a function called inside Invoke-Command?

I have the following function that I am trying to invoke remotely with verbose output, but I don't know how to pass the VerbosePreference correctly? function TestVerbose() { [CmdletBinding()] Param() Write-Output "output test" …
tholesen
  • 460
  • 5
  • 10
3
votes
4 answers

remove-item network location using invoke-command on remote computer fails

Running the following powershell command invoke-computer -computer computer -scriptblock{remove-item -force \\otherpc\backup_dump\TEST\*} I receive the error An object at the specified path \otherpc\backup_dump\TEST\ does not exist. But when…
bEUY
  • 127
  • 3
  • 9
3
votes
2 answers

Using Powershell Invoke-Command on remote session to invoke executable by string

I want to write a Powershell script to invoke an executable on a remote machine by its path and then wait for it to finish running. This is what I've got so far: $executable = "C:\Temp\example.exe" $session = New-PSSession -ComputerName…
Void Star
  • 2,401
  • 4
  • 32
  • 57
3
votes
3 answers

Get Java version from PowerShell of remote server

I am trying to write a script in PowerShell that will use the invoke command with credentials on a remote machine to execute code that will return a string containing the Java version on that machine. Java is not installed, sometimes it is just…
alexfvolk
  • 1,810
  • 4
  • 20
  • 40
3
votes
4 answers

Invoke-Command in a background job

I have a powershell 2.0 script which should run a command on several servers and process the output. I want to run the command and the processing for each server in a background job. The comand works without any problems and terminates within half a…
bones
  • 33
  • 1
  • 1
  • 5
3
votes
1 answer

How to invoke Powershell version 2 on remote computer via Invoke-Command

I'm trying to remotely setup websites on web servers using powershell. The web servers I'm attempting to configure are Windows Server 2008 R2 SP1 which has powershell v2 on it by default. To make things easier, I'm using the Snapin…
AvianFLU
  • 530
  • 5
  • 9
2
votes
2 answers

Running Invoke-Command to Stop-Process while passing a variable not working

The syntax below absolutely works to stop a process on a remote computer: $hostname = 'PC1' $process = 'program1*' $Session = New-PSSession $Hostname Invoke-Command -Session $Session -ScriptBlock {param($process) Stop-Process -ProcessName…
DrixlRey
  • 205
  • 3
  • 4
  • 13
2
votes
1 answer

Invoke-Command - use predefined function and object

i want to use a predefined function on a remote server with ps invoke and i need to send an object as parameter. This code always writes objects first element. $object = @('a','b','c') function testA { [CmdletBinding()] param ( …
MaikelNight
  • 123
  • 2
  • 7
2
votes
2 answers

Powershell Capture Invoke-Command Output

I've come across a problem which I can't seem to figure out. I have this piece of code which does exactly what I want. It searches for Windows Updates which are installed and have an specific UpdateID. param( $updateId = $false, $hostName =…
k_k
  • 83
  • 7
2
votes
0 answers

Bypass Record limit in REST API Call

I currently have a Powershell script to fetch orders through a REST API Call. The script (1) Uses the INVOKE-ResetMethod for the API call, then (2) uses the Export-CSV cmdlet to save the results in a CSV file, which works fine: $uri =…
Depth of Field
  • 307
  • 2
  • 16
2
votes
1 answer

Azure run command for Fortinet virtual machine with serial console?

I'm trying to execute with Azure a .conf file that has some command lines that are executable into the Fortinet's serial console in a virtual machine with FortiOS v6.4.3 (Linux based system). I use AZ CLI to execute the script into the virtual…
Elias Arellano
  • 433
  • 5
  • 16
2
votes
2 answers

How do I pass a class object in a argument list to a another computer and call a function on it?

I am attempting to create a class object and use Invoke-Command to call a function on the class on a remote machine. When I use Invoke-Command with no computer name this works fine but when I attempt to do this on a remote computer I get an error…
Max Young
  • 1,522
  • 1
  • 16
  • 42
2
votes
1 answer

Invoke-command localhost Access Denied

When Trying to invoke-Command against the Local Host I get access denied. I have confirmed that PS remoting is enabled and the account is Administrator. Additionally Remoting in from remote machine works without issue. Invoke-Command -computername…
user2850560
  • 101
  • 1
  • 2
  • 5
2
votes
1 answer

forward function to new powershell session or use Invoke-command?

I have below command, that I want to be called from my script, how can I pass the function New-PopupMessage ? Start-Process $PSScriptRoot\ServiceUI.exe -process:TSProgressUI.exe %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe…
BenDK
  • 131
  • 2
  • 7
2
votes
1 answer

Why does Invoke-Command not work on some systems?

I have seen this question asked a lot, but most of the answers boil down to 4 things: be sure you have run enable-PSRemoting -force on remote system be sure WMI services are running on remote system. be sure you have Firewall rules allowing 5985…
cmwoodman
  • 65
  • 7