start-job

Background Job in Powershell

北城以北 提交于 2019-12-05 05:04:09
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 because it is part of a configuration file and could change later on. One way to do this is use a script

How to call a powershell function within the script from Start-Job?

Deadly 提交于 2019-12-05 00:41:54
I saw this question and this question but couldn't find solution to my problem. So here is the situation: I have two functions DoWork and DisplayMessage in a script (.ps1) file. Here is the code: ### START OF SCRIPT ### function DoWork { $event = Register-EngineEvent -SourceIdentifier NewMessage -Action { DisplayMessage($event.MessageData) } $scriptBlock = { Register-EngineEvent -SourceIdentifier NewMessage -Forward $message = "Starting work" $null = New-Event -SourceIdentifier NewMessage -MessageData $message ### DO SOME WORK HERE ### $message = "Ending work" $null = New-Event

Powershell Start-job synchronous output

六眼飞鱼酱① 提交于 2019-12-04 04:10:29
I have a powershell script that starts a job start-job -scriptblock { while($true) { echo "Running" Start-Sleep 2 } } and then it continues executing the rest of the script. That job, is kind of a monitoring one for the PID of that process. I would like to synchronously print the PID every n seconds, without having to end the job. For example, as the rest of the script is being executed, i would like to see output in my console. Is something like that possible in powershell? Thanks. Neolisk Yes, you can use events: $job = Start-Job -ScriptBlock { while($true) { Register-EngineEvent

Get-WmiObject with credential fails when within Start-Job scriptblock

杀马特。学长 韩版系。学妹 提交于 2019-12-03 21:44:55
I am successfully retrieving some information from Windows 2000 machines using the Get-WmiObjet cmdlet. These machines are not part of our domain so I am using the -Credential parameter to pass local administrator credentials. I am now trying to run several WMI queries in parallel using Start-Job but I can't get even one query to work. When I run the following: Start-Job -initializationscript {$cred = get-credential -credential administrator} -scriptblock {gwmi win32_computersystem -ComputerName 10.1.2.3 -Credential $cred} a job is created, I am prompted for the credentials, but the job never

Invoke-Command in a background job

霸气de小男生 提交于 2019-12-02 03:39:39
问题 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

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

Start-Job including custom cmdlet terminates with strange error

风格不统一 提交于 2019-12-01 22:22:12
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 fine. But after around 15-20 seconds the job just gets terminated with the following error message:

How can I call many URLs from a list asynchronously

邮差的信 提交于 2019-12-01 04:02:24
I have a few hundred thousand URLs that I need to call. These are calls to an application server which will process them and write a status code to a table. I do not need to wait for a response (success/fail), only that the server got the request. I also want to be able to specify how many concurrent jobs can be running at once as I haven't worked out how many concurrent requests tomcat can handle. Here's what I've got so far, basically taken from someone's else's attempt to do something similar, just not with url calls. The text file contains each url on its own line. The url looks like this:

How can I call many URLs from a list asynchronously

浪子不回头ぞ 提交于 2019-12-01 01:12:36
问题 I have a few hundred thousand URLs that I need to call. These are calls to an application server which will process them and write a status code to a table. I do not need to wait for a response (success/fail), only that the server got the request. I also want to be able to specify how many concurrent jobs can be running at once as I haven't worked out how many concurrent requests tomcat can handle. Here's what I've got so far, basically taken from someone's else's attempt to do something

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

喜欢而已 提交于 2019-11-30 17:24:12
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? One way is to use the [scriptblock]::create method to create the script block from an expanadable string using local variables: $v1 = "123" $v2 = "asdf" $sb = [scriptblock]::Create("Write-Host