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

Powershell Invoke Command Export to Excel

Having a few issues with this PowerShell code: $vmslist = Import-Csv -Path .\vmsList.csv -Delimiter ',' | ForEach-Object { $_.Servers $vms = [string]$env:COMPUTERNAME $currentVms = $_.App_Central function Check-DiskSpace { …
dcwilli5
  • 55
  • 4
3
votes
1 answer

PowerShell Invoke-Command timeout if no answer from frozen AD-Server

I'm writing a PowerShell-Script that reads all shares from all AD-Servers and outputs them into a csv-file. At the same time the script is saving all occuring errors and outputs them into an error-log. The script will be run as a weekly task. When I…
zynory
  • 43
  • 3
3
votes
2 answers

PowerShell safe expansion of non printing characters in arbitrary strings

I have data in an XML file, that will eventually be used as a Registry path, which MAY contain non printing characters (for example when copying the path from a web site into the XML). I want to validate the data and throw a specific error if non…
Gordon
  • 6,257
  • 6
  • 36
  • 89
3
votes
1 answer

Invoke-Command to Microsoft Exchange

a recent patch of Microsoft Exchange changed the way people can use Invoke-Command to an Microsoft Exchange server. Invoke-Command -Session $ExOSessionVariable -ScriptBlock {Get-RemoteMailbox -Identity UserName ; Get-User -Identity UserName} is…
Michael
  • 31
  • 2
3
votes
5 answers

PowerShell Invoke-Command with FilePath on local computer - vague parameters error?

I want to run a script in file on the local machine using Invoke-Command so I can pass in parameters with -ArgumentList. I've been getting an error I don't understand, so I simplified my command. When I do this: Invoke-Command -FilePath…
BigScary
  • 530
  • 1
  • 6
  • 19
3
votes
1 answer

Powershell - capture output from Invoke-Command running exe on remote machine

I need to configure the auditing policy on a collection of remote servers. I'm trying to use the Invoke-Command commandlet to run auditpol.exe on each server. The issue is that I can't seem to capture any output from the auditpol command. I tried…
Mike Bruno
  • 600
  • 2
  • 9
  • 26
3
votes
1 answer

How to get Invoke-Command verbose or debug output from a PowerShell command in C#

I have a PowerShell command that invokes a command on a remote machine to print out a debug message, and then prints out a debug message on the running machine, as follows: function Start-DebugTest { [cmdletbinding()] param () $cmd = { …
robertkroll
  • 8,544
  • 6
  • 24
  • 24
3
votes
2 answers

Powershell invoke-command access is denied error - not a double hop

I am building a new network with Server 2016 and a handful of Windows 10 clients. I have run Enable-PSRemoting successfully on all the clients. From the server I run: Invoke-Command -ComputerName $computer -Scriptblock {'test'} which results in…
Lee Exothermix
  • 316
  • 2
  • 3
  • 14
3
votes
1 answer

Access denied while running Windows Update using Powershell's Invoke-Command

I've been trying to setup a Powershell module that would remotely call Windows/Microsoft update on a server using Invoke-Command, then process the updates, and send everything back to the calling server so it can send an email report. My issue comes…
3
votes
0 answers

Invoke-Command Cannot Find Path But File Does Exist

I'm very new to Windows scripting and am having an issue with trying to execute a Powershell script located on a remote node. It's a super simple HelloWorld script. I'm setting up my session and issuing the remote invocation command like this…
Chiefwarpaint
  • 643
  • 2
  • 12
  • 25
3
votes
1 answer

Using Powershell and Invoke-WebRequest to upload 2.5 GB to a web server

I am a networking engineer and do not have any background on scripting or powershell however i started to deal with it lately as i need to create couple of scripts. I work for VMware and want to create a script that will automate the upgrade of the…
Taher Shaker
  • 111
  • 2
  • 2
  • 6
3
votes
1 answer

How to pass param value to the Invoke-Command cmdlet?

I wrote a simple powershell script to modify hosts file on remote machines but something goes wrong. Script: param( [string]$value ) $username = 'username' $password = 'password' $hosts = "172.28.30.45","172.28.30.46" $pass =…
Vladlen Gladis
  • 1,699
  • 5
  • 19
  • 41
3
votes
1 answer

Powershell: How to pass parameter with invoke-command and -filepath remotely?

so there's a lot of similar topics to this I've found, but I can't find anything that correlates with my issue. invoke-command -computername $serverLocation -Credential $cred -filepath "C:\path\script2.ps1" -ArgumentList $configPath I am calling a…
crystallinity
  • 431
  • 2
  • 6
  • 10
3
votes
4 answers

Access is denied to localhost despite being administrator - PowerShell

Okay, so this has been bugging me for a while and I have tried too many things now. I'm trying to run a PowerShell script - my user account is a regular one on the domain, it is however local administrator on my computer. Therefore I've created a…
zniwalla
  • 367
  • 4
  • 17
3
votes
1 answer

Pester Mock does not work for Invoke-Command using script block

I have a console logger function Common-Write-Log-Console { param ( [Parameter(Mandatory=$true)] [string] $logText ) $textToOutput = [String]::Format("{0}:{1}", [System.DateTime]::Now.ToString(), $logText) …