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
1
vote
3 answers

How can I pass multi args with Invoke-Command

I want create a few shortcuts on my desktop, it works locally. But when I try it on a remote PC I got only one shortcut for the first target (path1), and script ignores path2 variable. $Servers = Get-Content D:\1.txt function add-sc { param…
Pavel Mi
  • 45
  • 2
  • 7
1
vote
1 answer

PowerShell script fails to authenticate user when running Invoke-Command

I've recently created a little script that allows me to get the disk size and free space of 2 servers at each school site when I provide the script with the schools 4 digit site code. First it pulls the information on the sites from a .csv file, and…
Random206
  • 757
  • 6
  • 19
1
vote
3 answers

Accessing variables defined in a scriptblock after running Invoke-Command

I'll try to make this example as simple as possible. $myValues = { $value1 = "hello" $value2 = "world" } Invoke-Command $myValues How can I access $value1 and $value2 after the Invoke-Command executes? Edit: The problem I am trying to…
Eric Furspan
  • 742
  • 3
  • 15
  • 36
1
vote
1 answer

Forwarding output of Invoke-Command on Remote Server

To delete old files on our servers we run a remote command on the server using PowerShell and Invoke-Command: Invoke-Command { Param($ServerPath) Remove-Item -Path $ServerPath -Recurse -Force -Verbose } -Computer servername -Credential…
Paxz
  • 2,959
  • 1
  • 20
  • 34
1
vote
1 answer

Enumerating local groups using Invoke-Command on remote machines

I have an issue with a function I've wrote to return members of local groups when it is run against a remote machine. We use secondary domain accounts for admin privileges so I've used Invoke-Command so we can run the script block with that account,…
Ash
  • 3,030
  • 3
  • 15
  • 33
1
vote
2 answers

PowerShell: How to run a powershell script on a remote machine using WMI

To be specific: I want to run a powershell script on a remote windows server, but I can connect to this server using WMI only. I used, for example, Get-Wmiobject to get some data like the running processes, but I failed after a lot of searching, to…
Yasser Mohsen
  • 1,411
  • 1
  • 12
  • 29
1
vote
0 answers

use Invoke-Command to start a process but failed with no action

I need to install some application on a list of machines. on one of machines, i use below command to install it, it works well and finish installation in a few mins. Start-Process -FilePath "c:\javabits.exe" -ArgumentList '/s' -Wait However when I…
Arthas Liu
  • 31
  • 4
1
vote
2 answers

Invoke-Command on remote target with specific Powershell Version possible?

I'm trying to write a small PowerShell script which will use a specific PowerShell module on a remote machine to do some things. This module requires the use of a PowerShell Version >= to work. I tried using Invoke-Command to execute the script on…
Manuel
  • 11
  • 2
1
vote
1 answer

Remote Installation of Python using Powershell fails using Invoke-Command

I am trying to use this script to install Python on the remote computer. If I run this file directly on the server. This is the Python_Pip_Proxy_PyWinAuto.ps1 file. It works. Set-ExecutionPolicy -ExecutionPolicy…
Addzy K
  • 715
  • 1
  • 7
  • 11
1
vote
2 answers

PowerShell List Results

I'm trying to list in a CSV or txt the results from the following PowerShell command: Show_Proxy.ps1: Invoke-Expression "netsh winhttp show proxy" Server_Name.txt: Server01 Server02 Server03 $ServerList = Get-Content…
1
vote
0 answers

How to execute sqlcmd in Powershell with different domain user credentials

I have a SQL script which I need to execute with credentials that I retrieve from registry. To store them in the registry $secureCredential = Get-Credential -Message "Enter service account credential as DOMAIN\Username format." $credentialName =…
1
vote
1 answer

PowerShell "=" not a recognised cmdlet error

Further to the response given here: PowerShell Enter Session find path bug, I've hit another wall in my script that I can't work around. The below script returns the error: The term '=' is not recognized as the name of a cmdlet, function, script…
Karl
  • 67
  • 1
  • 7
1
vote
1 answer

Invoking Command inside Scriptblock to remote session computers using powershell

Could anybody provide any insight on why the below code is not working? I had this working before and cannot see why the New-PSSession cannot validate the ComputerName parameter. Full Error Below code. $Cred = Get-Credential DOMAIN\User #…
Royston
  • 433
  • 2
  • 9
  • 25
1
vote
0 answers

Powershell seems to treat sub pipeline code as part of execution body of first pipeline script block

I am creating a retry command in powershell to invoke some script block with retry logic: function Invoke-CommandWithRetry { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] …
winterTTr
  • 1,739
  • 1
  • 10
  • 30
1
vote
1 answer

How to make Invoke-Command return empty array instead of null when run script block

I am creating a function and sometimes, it will return a empty array as there is not result. This function may need to run with Invoke-Command for some scenario, such as run on a remote PC and etc. But what i found that, when my function run under…
winterTTr
  • 1,739
  • 1
  • 10
  • 30