start-job

not getting output from receive job

杀马特。学长 韩版系。学妹 提交于 2019-12-14 03:26:00
问题 The variable $var is blank when I run this script: function FOO { write-output "HEY" } $var = Start-Job -ScriptBlock { ${function:FOO} } | Wait-Job | Receive-Job $var How do I get output from receive-job? 回答1: Start-Job spawns a new PowerShell instance in the background and as such has no knowledge of your function FOO which is defined in your initial instance There is an additional parameter InitializationScript which is called upfront executing your script block in the new instance which

Powershell Start-job invoke function argument with parameter

若如初见. 提交于 2019-12-14 03:07:14
问题 I have a function mainFunction that gets 2 parameters - $name will be just a regular string, and $moveFunction will be some function. I want to start a job of a ScriptBlock ( $SB ) that will invoke $moveFunction with $name as his argument. function foo($a){ Write-Output "In function foo with the argument => $a" } $SB = { param($C, $fooFunction) $fooFunction.Invoke($C) } function mainFunction($name, $moveFunction){ Start-Job -Name "currentJob" -ArgumentList $name, ${Function:$moveFunction}

How can I keep the UI responsive and updated with progress while all the work is done in a background job on the local computer?

冷暖自知 提交于 2019-12-14 03:03:22
问题 I have a program that displays a UI , allows the user to pick virtual machine names obtained by querying the Xen pool master server, and then creates snapshots for the selected virtual machines. I want the snapshots to be created in the background so I can keep the UI responsive and update the UI with progress as each snapshot is created. Originally, I connected to the Xen pool master server and then executed the Xen create snapshot cmdlet once per selected VM in the UI thread. As such, the

How to put powershell commands in an array?

回眸只為那壹抹淺笑 提交于 2019-12-13 05:16:52
问题 I have a script that does a bunch of image processing. So far it runs sequentially, uses only one core and takes forever. I have four cores at hand. I want to run four of these commands at the same time. To make this happen, I need to put the commands in an array. These are the original commands: convert.exe -density 200 -quality 80 -delete 0 -scene 1 C:\Users\mles\Desktop\ta2014\v33_1_21_Northland.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg convert.exe -density 200 -quality 80 -delete 0 -scene

PowerShell - Passing expanded arguments to Start-Job cmdlet

柔情痞子 提交于 2019-12-11 06:29:03
问题 We're trying to create an array with variables and then pass this array as expanded to a script, which shall be run by Start-Job. But actually it fails and we are unable to find the reason. Maybe someone can help!? $arguments= @() $arguments+= ("-Name", '$config.Name') $arguments+= ("-Account", '$config.Account') $arguments+= ("-Location", '$config.Location') #do some nasty things with $config Start-Job -ScriptBlock ([scriptblock]::create("& .'$ScriptPath' [string]$arguments")) -Name "Test"

Cannot bind parameter to argument 'command' because it is null. Powershell

孤街浪徒 提交于 2019-12-11 05:58:32
问题 I had a function similar to below code. It receives command and the command arguments. I had to run this command in background and collect the output. But that last statement is bugging me with this error Error: Cannot bind argument to parameter 'Command' because it is null. + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpre ssionCommand +

PowerShell: Start-Job -scriptblock Multi line scriptblocks?

不羁的心 提交于 2019-12-10 21:03:37
问题 Hoping someone can help me to figure out if this is possible. I have a 20 ish lines that scan a log file in a while loop. I need this to happen in parallel with the rest of the script. The log scanning loop is passing the entries into SQLite and other scripts need to act on this information - hence wanting to run them in parallel. If i use the Job-Start command then it seems the -SciptBlock function will only accept one piped line of commands. I have too many commands to want to pipe so i

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

半城伤御伤魂 提交于 2019-12-06 19:15:40
问题 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

Powershell Start-job synchronous output

懵懂的女人 提交于 2019-12-05 23:35:40
问题 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. 回答1: Yes,

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

你离开我真会死。 提交于 2019-12-05 09:14:57
问题 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