start-job

Powershell parallel jobs output file

南楼画角 提交于 2020-01-05 12:16:30
问题 I have already done a lot of research, but still can't figure out how to accomplish what I want to do. I want to perform the same tasks parallel on 100 Linux servers. Here is a simplified example of my script: $computer=Get-Content "serverList.txt" $jobArray=@() $script={ $cpuThresh=70 $cpuUsage=<Get CPU usage of the host> Write-Host "CPU Usage: $cpuUsage %" if ($cpuUsage -ge $cpuThresh) { Write-Host "Unexpected CPU Usage" -ForegroundColor Red } } foreach ($system in $computer) { $jobArray +=

Managing the running time of background jobs. Timing out if not completed after x seconds,

[亡魂溺海] 提交于 2019-12-23 10:20:00
问题 I would like to time my background jobs (started with start-job ) and time them out after x seconds. I find it hard however to keep track of the running time on each separate job (I am running aprox 400 jobs). I wish there was a way to time out the job and set it to failed if not completed in X seconds, but I find no timeout-parameter. What would be a good way to track the individual run-time of the jobs? I guess I could create a hashtable with start-time of each job and the job-id and check

“Start-Process -NoNewWindow” within a Start-Job?

随声附和 提交于 2019-12-23 08:29:38
问题 I'm having issues using Start-Process within a Start-Job, specifically when using -NoNewWindow . For example, this test code: Start-Job -scriptblock { Start-Process cmd -NoNewWindow -Wait -ArgumentList '/c', 'echo' | out-null Start-Process cmd # We'll never get here } get-job | wait-job | receive-job get-job | remove-job Returns the following error, that apparently google hasnt heard of: Receive-Job : There is an error processing data from the background process. Error reported: Cannot

How to Receive Errors in Test-Connection with -AsJob switch

扶醉桌前 提交于 2019-12-23 05:33:11
问题 I was setting up a monitoring script that would ping IPs and send an email when a packet fails. I decided to use Test-Connection . Below is a sample code: Code1 $IPList = @("192.168.0.1","172.217.161.15") Test-Connection -ComputerName $IPList -BufferSize 4 -Count 1 -AsJob -ErrorAction Stop Get-Job | Wait-Job | Receive-Job So the result I get is: Source Destination IPV4Address IPV6Address Bytes Time(ms) ------ ----------- ----------- ----------- ----- -------- BLR-AA200906 172.217.161.15 4 55

Background Job in Powershell

北城以北 提交于 2019-12-22 04:48:15
问题 I'm trying to run a job in a background which is a .exe with parameters and the destination has spaces. For example: $exec = "C:\Program Files\foo.exe" and I want to run this with parameters: foo.exe /param1 /param2, etc. I know that Start-Job does this but I've tried tons of different combinations and it either gives me an error because of the white space or because of the parameters. Can someone help me out with the syntax here? I need to assume that $exec is the path of the executable

How to handle errors for the commands to run in Start-Job?

落爺英雄遲暮 提交于 2019-12-20 05:29:07
问题 I am writing an automation script. I had a function which takes either a command or an executable. I had to wait until the command or executable has completed running and return if failed or passed. I also want to write the output to file. I am trying with the Start-Job cmdlet. My current code: $job = Start-Job -scriptblock { Param($incmd) $ret = Invoke-Expression $incmd -ErrorVariable e if ($e) { throw $e } else { return $ret } } -ArumentList $outcmd Wait-Job $job.id "write the output to

Start-Job including custom cmdlet terminates with strange error

半腔热情 提交于 2019-12-20 02:23:33
问题 I developed some custom cmdlets that serve for different importing tasks to a SharePoint system. Currently all those cmdlets are being run in a serial kind in a single PowerShell script. I want to change this so that each cmdlet gets executed in a separate task (job). The main script starts a new job with Start-Job relating to a separate script that contains the call to the cmdlet. The script starts and executes the cmdlet. I also debugged the code of the cmdlet that gets executed. So far so

Start-Job including custom cmdlet terminates with strange error

此生再无相见时 提交于 2019-12-20 02:23:06
问题 I developed some custom cmdlets that serve for different importing tasks to a SharePoint system. Currently all those cmdlets are being run in a serial kind in a single PowerShell script. I want to change this so that each cmdlet gets executed in a separate task (job). The main script starts a new job with Start-Job relating to a separate script that contains the call to the cmdlet. The script starts and executes the cmdlet. I also debugged the code of the cmdlet that gets executed. So far so

Powershell - how to pre-evaluate variables in a scriptblock for Start-Job

萝らか妹 提交于 2019-12-18 14:14:39
问题 I want to use background jobs in Powershell. How to make variables evaluated at the moment of ScriptBlock definition? $v1 = "123" $v2 = "asdf" $sb = { Write-Host "Values are: $v1, $v2" } $job = Start-Job -ScriptBlock $sb $job | Wait-Job | Receive-Job $job | Remove-Job I get printed empty values of $v1 and $v2. How can I have them evaluated in (passed to) the scriptblock and so to the background job? 回答1: One way is to use the [scriptblock]::create method to create the script block from an

PowerShell Start-Job Working Directory

半世苍凉 提交于 2019-12-18 10:56:36
问题 Is there a way to specify a working directory to the Start-Job command? Use-case: I'm in a directory, and I want to open a file using Emacs for editing. If I do this directly, it will block PowerShell until I close Emacs. But using Start-Job attempts to run Emacs from my home directory, thus having Emacs open a new file instead of the one I wanted. I tried to specify the full path using $pwd, but variables in the script block are not resolved until they're executing in the Start-Job context.