invoke-command

Running Powershell script remotely through Java

风流意气都作罢 提交于 2019-12-02 03:03:17
I am able to run the below powershell command through Powershell itself, invoke-command -ComputerName "compName" -filepath "c:\script.ps1" -credential "admin" but when I try running that through Java, I get an error. Sounds like "Invoke-command" is not recognized as a program to be run though Java. If this is the case, is there any other solution? Process p = new ProcessBuilder() .inheritIO() .command("invoke-command", "-computername", "compName", "-filepath", "C:\\script.ps1").start(); The error, Cannot run program "invoke-command": CreateProcess error=2, The system cannot find the file

Script to get CPU Usage

限于喜欢 提交于 2019-12-02 01:17:14
I am using this script to get CPU usage from multiple server $Output = 'C:\temp\Result.txt' $ServerList = Get-Content 'C:\temp\Serverlist.txt' $CPUPercent = @{ Label = 'CPUUsed' Expression = { $SecsUsed = (New-Timespan -Start $_.StartTime).TotalSeconds [Math]::Round($_.CPU * 10 / $SecsUsed) } } Foreach ($ServerNames in $ServerList) { Invoke-Command -ComputerName $ServerNames -ScriptBlock { Get-Process | Select-Object -Property Name, CPU, $CPUPercent, Description | Sort-Object -Property CPUUsed -Descending | Select-Object -First 15 | Format-Table -AutoSize | Out-File $Output -Append } } and I

Invoke-Command in a background job

落花浮王杯 提交于 2019-12-02 01:02:25
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 second or less: Invoke-Command -ComputerName $client -ScriptBlock { #do some stuff } But when I run this in a background job, the job doesn't terminate: Start-Job { Invoke-Command -ComputerName $client -ScriptBlock { #do some stuff } } Has someone a idea what the problem could be? Looks like you should do it the other way: http://technet.microsoft

PowerShell Splatting the Argumentlist on Invoke-Command

和自甴很熟 提交于 2019-12-01 18:14:53
How is it possible to use the parameters collected in a hash table for use with ArgumentList on Invoke-Command ? $CopyParams = @{ Source = 'E:\DEPARTMENTS\CBR\SHARE\Target' Destination = 'E:\DEPARTMENTS\CBR\SHARE\Target 2' Structure = 'yyyy-MM-dd' } Invoke-Command -Credential $Cred -ComputerName 'SERVER' -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList @CopyParams Whatever I try, it's always complaining about the 'Source': Cannot validate argument on parameter 'Source'. The "Test-Path $_" validation script for the argument with value "System.Collections.Hashtable" did not return true.

PowerShell Splatting the Argumentlist on Invoke-Command

陌路散爱 提交于 2019-12-01 17:52:17
问题 How is it possible to use the parameters collected in a hash table for use with ArgumentList on Invoke-Command ? $CopyParams = @{ Source = 'E:\DEPARTMENTS\CBR\SHARE\Target' Destination = 'E:\DEPARTMENTS\CBR\SHARE\Target 2' Structure = 'yyyy-MM-dd' } Invoke-Command -Credential $Cred -ComputerName 'SERVER' -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList @CopyParams Whatever I try, it's always complaining about the 'Source': Cannot validate argument on parameter 'Source'. The "Test-Path $_"

How to Invoke-Command and pass path to command as parameter

人走茶凉 提交于 2019-12-01 12:33:14
I'm tearing my hair out trying to invoke-command but pass the path to the exe as a parameter eg: I want to take this command powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($command ) C:\windows\system32\getmac.exe /$command } -ArgumentList ? and translate it into a form like this powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) $path\getmac.exe /$command } -ArgumentList C:\windows\system32,? I've tried all manner of quoting, ampersands and other contortions but can't get it to work. The above attempt results in Unexpected token '

How to Invoke-Command and pass path to command as parameter

给你一囗甜甜゛ 提交于 2019-12-01 10:33:11
问题 I'm tearing my hair out trying to invoke-command but pass the path to the exe as a parameter eg: I want to take this command powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($command ) C:\windows\system32\getmac.exe /$command } -ArgumentList ? and translate it into a form like this powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) $path\getmac.exe /$command } -ArgumentList C:\windows\system32,? I've tried all manner of quoting,

Get script directory in PowerShell when script is invoked with Invoke-Command

放肆的年华 提交于 2019-12-01 05:27:05
I have a set of PowerShell scripts that include a "common" script, located in the same folder, like this: # some-script.ps1 $scriptDir = Split-Path -Parent $myinvocation.mycommand.path . "$scriptDir\script-utils.ps1" This is fine if the script is called directly, e.g. .\some-script.ps1 However, if the script is called with Invoke-Command, this does not work: Invoke-Command -ComputerName server01 -FilePath "X:\some-script.ps1" In this case, infact, $myinvocation.mycommand contains the contents of the script, and $myinvocation.mycommand.path is null. How can I determine the script's directory in

Get script directory in PowerShell when script is invoked with Invoke-Command

我只是一个虾纸丫 提交于 2019-12-01 03:55:41
问题 I have a set of PowerShell scripts that include a "common" script, located in the same folder, like this: # some-script.ps1 $scriptDir = Split-Path -Parent $myinvocation.mycommand.path . "$scriptDir\script-utils.ps1" This is fine if the script is called directly, e.g. .\some-script.ps1 However, if the script is called with Invoke-Command, this does not work: Invoke-Command -ComputerName server01 -FilePath "X:\some-script.ps1" In this case, infact, $myinvocation.mycommand contains the contents

Using Powershell's Invoke-Command to call a batch file with arguments

别说谁变了你拦得住时间么 提交于 2019-12-01 03:41:27
I want to use Powershell in order to call a batch file on remote machines. This batch file has arguments. Here's what I have so far: $script = "\\fileshare\script.cmd" $server = $args[0] $args [string]::join(',',$args[1 .. ($args.count-1)]) Invoke-Command -computername $server {$script + ' ' + $args} After a bit of searching, I found that the Invoke-Command function runs its scriptblock in a whole new process, so you can't put variables in it (they won't get expanded). That's what the -ArgumentList tag is for. So I tried this instead... Invoke-Command -computername $server {\\fileshare\script