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
2
votes
2 answers

Remote output from Get-SmbAccess

I have this code bit, I am trying to get to work in a script: $computers = Get-ADComputer -Filter * | Select-Object -ExpandProperty Name $results = Invoke-Command -ComputerName $computers -ScriptBlock {Get-SmbShare | Get-SmbShareAccess} I would…
Bubblies
  • 23
  • 4
2
votes
1 answer

Powershell Invoke-Command very slow to connect on one computer

So I'm trying to use Invoke-Command to run a command on a remote server in Azure. To test, I'm using a super simple script: Invoke-Command -ComputerName MyServer -ScriptBlock { Write-Output "HI"} I've run this command on a whole bunch of other…
Chris
  • 443
  • 1
  • 3
  • 14
2
votes
0 answers

Passing nested classes to Invoke-Command as argument

I'm trying to pass a class object as an argument to Invoke-Command. class A { [string]$str_a A($sa) { $this.str_a = $sa } } class B { [string]$str_b [A[]]$a_list B($sb, $al) { $this.str_b = $sb …
tabs_over_spaces
  • 352
  • 1
  • 3
  • 14
2
votes
2 answers

Invoke-Command on remote session returns local values

Question Should the script block of Invoke-Command, when run with a PSSession, always run on the remote computer? Context I ran the below powershell against a list of servers: Clear-Host $cred = get-credential 'myDomain\myUsername' $psSessions =…
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
2
votes
2 answers

powershell invoke-command does not process try-cache block

I have the folowing code: $output = foreach ($comp in $maschines.name) { invoke-command -computer comp1 -ScriptBlock { try { get-vm –VMName $using:comp | Select-Object VMId | Get-VHD | Select-Object @{ label = "vm";…
andrej
  • 547
  • 1
  • 6
  • 21
2
votes
1 answer

Powershell retrieving cert by Thumbprint as string versus string variable

I'm trying to piece together some PowerShell code to loop through a list of servers, return some info regarding their IIS sites and bindings, and if they have an https binding, get the certificateHash and use that locate the cert by thumbprint and…
Dylan Hayes
  • 2,331
  • 1
  • 23
  • 33
2
votes
1 answer

Invoke-Command changes the output encoding

I am using a script that runs on few computers, running Net command query on every one and comparing the results of any one with Compare-Object. The script is: Net users /domain Some of the users in my environment has Spanish letters. When running…
Lior
  • 139
  • 2
  • 13
2
votes
1 answer

PowerShell Invoke-Command with Start-Process access denied

I have a script that has this code: $secpasswd = ConvertTo-SecureString "Ukn545454" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential ("$env:USERDOMAIN\user1", $secpasswd) invoke-command -ComputerName "MyW10comp"…
Lior
  • 139
  • 2
  • 13
2
votes
0 answers

Can't run remote command stored in a variable

I'm trying to build a script which copies files and execute them on a specific amount of servers. To do so, I need to run a remote command. I stored it in a variable. I know that variables can't be used within a remote session. Except you use the…
2
votes
1 answer

PowerShell SCript to Execute CMD.exe with Arguments

SO I have surfed this site and the web and I feel as though I am missing something simple. I find related questions but none that combine a scriptblock and remote calling of a 3rd party app (not a simply windows function or app) I have the…
Peter Plott
  • 21
  • 1
  • 2
2
votes
3 answers

Powershell Invoke-Command passing environment variables

I wish to use Invoke-Command passing environment variables from the calling machine to the server where Invoke-Command is being executed. I want this to work: Invoke-Command -ComputerName MyServer-ScriptBlock { $env:VAR=$using:env:USERNAME …
jaime
  • 2,234
  • 1
  • 19
  • 22
2
votes
2 answers

Powershell Invoke-Command causes different result

I have a function that is used to purge a message queue on a machine but I'm looking to adapt it and make it a little more robust. I'd like to be able to fire the command to a machine even if the current machine doesn't have MSMQ installed. The…
Xanderu
  • 747
  • 1
  • 8
  • 30
2
votes
1 answer

Kerberos Delegation Issue Copying Files to Remote Session with 2008 R2 Domain functional Level

When running the below code, i can put anything in the block at the bottom - I'm trying to copy a folder across to run an exe from a local folder and perform an install of that exe during the remote session to remote machines. I am getting Access…
Royston
  • 433
  • 2
  • 9
  • 25
2
votes
1 answer

Installing Software Using PowerShell Invoke Command

$cs = New-PSSession -ComputerName MACHINE -Credential DOMAIN\admin Copy-Item -Path C:\Scripts\smart -Destination C:\smart -ToSession $cs msiexec /i "C:\Smart\SMART.msi" NB_PROD_KEY=NC-2ADA2-F9RKE-AKAIA-BBB ACTIVATE_LICENSE=1 INSTALL_INK=""…
Royston
  • 433
  • 2
  • 9
  • 25
2
votes
1 answer

Formatting Output with Invoke-Command

I'm trying to format my output to look like so PSComputerName Value -------------- ------ Computer1 Restricted Computer2 Unrestricted Computer3 Unrestricted This is my code the variable $computers points…
Katz
  • 826
  • 3
  • 19
  • 40