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
5
votes
3 answers

How to convert $args to a string and execute?

I'd like to pass all arguments that were given to the script and execute. For example, given execute.ps1 script: Invoke-Expression $($args[0]) I can run: .\execute.ps1 hostname myhostname Same with two parameters with this…
kenorb
  • 155,785
  • 88
  • 678
  • 743
5
votes
5 answers

Powershell Start-Process msiexec on a remote machine not working

For some reason Start-Process msiexec won't work when run through invoke command on a remote machine. I looked it up and while some people recommend using psiexec i have seen a lot of people using the plain old invoke-command to start msi installers…
5
votes
2 answers

How to pass an xml element to a scriptBlock when using invoke-command

Using PowerShell 3.0, I am using the Invoke-Command cmdlet to pass an xmlElement to a scriptblock. The problem is that I think the scriptBlock is receiving the parameter as an arrayList and not as an xmlElement (actually, I know that the received…
Nathan Bills
  • 93
  • 1
  • 5
4
votes
1 answer

Argument Passed into ScriptBlock Doesn't Work on Last Iteration

I'm using the following script to run through all of the servers in a specified Active Directory OU and log out the specified user. This runs perfectly well for the first 35 servers but always errors out on the very last server it iterates through.…
4
votes
1 answer

$Using:var in Start-Job within Invoke-Command

I am using Invoke-Command, and within the -ScriptBlock I am using Start-Job. I have to use $Using:var within Start-Job but the session is looking for the declared variables in the local session (declared before Invoke-Command). Here's a very brief…
Dusty Vargas
  • 863
  • 6
  • 17
4
votes
1 answer

Why does Get-WmiObject fail when run remotely via Invoke-Command?

I'm attempting to build a simple "get-ProcessInfo.ps1" module to be used with a PowerShell (forensics/IR) framework called Kansa. The command is a simple one liner that calls Get-WMIObject win32_process and pipes it into Select-Object. Then Kansa…
Duck
  • 91
  • 7
4
votes
3 answers

How do I run a *.exe file from PowerShell

I have a folder at C:\Folder that has files input.xml, output.xml and licensegenerator.exe. Licensegenerator.exe takes variables that we put into input.xml and creates a temporary license for one of our programs using the output.xml file. We…
Christopher Cass
  • 817
  • 4
  • 19
  • 31
4
votes
3 answers

How to run multiple commands via Invoke-Command on remote server

In the below example, only the Get-Process in my scriptblock is being executed. "deh" does not print for some reason Invoke-Command -ComputerName mycomputer -Credential $mycreds -ScriptBlock { Get-Process Write-Host "deh" } If I remove…
Eric Furspan
  • 742
  • 3
  • 15
  • 36
4
votes
1 answer

Powershell Script not getting failed in Jenkins

I have downloaded SSH-Sessions by Joakim Svendsen which uses SSH.NET and installed the PowerShell module in the Jenkins Windows server In Jenkins, I have the following PowerShell script: Import-Module SSH-Sessions $lastExitCode = 0 $devApp1 =…
user2439278
  • 1,222
  • 7
  • 41
  • 75
4
votes
1 answer

PowerShell: Start-Job -scriptblock Multi line scriptblocks?

Hoping someone can help me to figure out if this is possible. I have a 20 ish lines that scan a log file in a while loop. I need this to happen in parallel with the rest of the script. The log scanning loop is passing the entries into SQLite and…
SynxUK
  • 83
  • 1
  • 10
4
votes
2 answers

Powershell Invoke-RestMethod "Unable to connect to the remote server"

I write a very simple powershell script to post a json string to server. I have a strange issue when I execute the script. The server is inside our intranet, the firewall is disable for all intranet server. I tried to execute the script in…
Jim
  • 769
  • 3
  • 10
  • 22
4
votes
1 answer

Property is empty when run through Invoke-Command

I am trying to individually monitor memory usage of a process (w3wp.exe) that has multiple instances of itself by filtering out a string found in the process' CommandLine property. It works when I run this script locally: $proc = (WmiObject…
4
votes
4 answers

Extraneous data returned from Invoke-Command

I'm working with PowerShell to gather data from a list of remote servers which I then turn into a JSON object. Everything is working fine, but I get some really weird output that I can't seem to exclude. I've tried piping the Invoke-Command results…
DiscOH
  • 177
  • 1
  • 7
4
votes
1 answer

Powershell Start-Process ignored in remote session

I am starting a new process using: $removeArguments = "-Command `"&{import-module .\deploy-utility.psm1; RemoveSolutions -solutionNames $solutionNames -url $url;}`"" start-process powershell -ArgumentList $removeArguments -Wait This works fine…
3
votes
2 answers

powershell: script to start a program with parameters?

When i run the Powershell script below i receive the error below. How do i run programs through powershell with parameters? The script will be a group policy logon. Invoke-Expression : A positional parameter cannot be found that accepts argument…
resolver101
  • 2,155
  • 11
  • 41
  • 53
1 2
3
36 37