powershell-jobs

PowerShell implementing -AsJob for a cmdlet

不问归期 提交于 2021-01-29 05:36:13
问题 Is there a nice way to implement the switch parameter -AsJob in custom cmdlets, like Invoke-Command has? The only way I thought about this is: function Use-AsJob { [CmdletBinding()] [OutputType()] param ( [Parameter(Mandatory = $true)] [string] $Message, [switch] $AsJob ) # Wrap Script block in a variable $myScriptBlock = { # stuff } if ($AsJob) { Invoke-Command -ScriptBlock $myScriptBlock -AsJob } else { Invoke-Command -ScriptBlock $myScriptBlock } } Is there a better approach? I couldn't

Calling workflow from another workflow as a job in powershell

对着背影说爱祢 提交于 2020-07-19 11:10:34
问题 I have to call a long running workflow from another workflow asynchronously. Is there a way to do that in powershell? workflow CalledWorkflow { Start-Sleep -s 100; } workflow CallingWorkflow { CalledWorkflow "CalledWorkFlow Invoked" } Now when i call CallingWorkflow I need the callingworkflow to immediately return printing "CalledWorkflow Invoked" so that i can start continue my work while the calledworkflow is running in backgroud. 回答1: What environment are you running the workflows in? In

$Using:var in Start-Job within Invoke-Command

梦想与她 提交于 2020-07-19 04:52:32
问题 I am using Invoke-Command , and within the -ScriptBlock I am using Start-Job . I have to use $Using:var within Start-Job but the session is looking for the declared variables in the local session (declared before Invoke-Command ). Here's a very brief example of what I'm doing: Invoke-Command -ComputerName $computer -ScriptBlock { $sourcePath = 'C:\Source' $destPath = 'C:\dest.zip' $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal $includeBaseDirectory = $false Start-Job

Can Powershell Receive-Job return a DataSet?

試著忘記壹切 提交于 2020-01-02 05:18:11
问题 Background Info : I have an application that makes several SQL connections to multiple databases which currently takes a very very long time to execute. Powershell (.NET) will wait for each proceeding " SQL-GET " function to finish before it can fire off the next. I am under the impression I can speed this app up dramatically by firing each " SQL-GET " function in their own background job simultaneously!I will then retrieve the data from each job as they finish. Ideally as a DataSet system

How do I debug a PowerShell background job?

吃可爱长大的小学妹 提交于 2019-12-24 10:16:22
问题 Jason Archer helped me solve my last issue, but in testing his solution I stumbled across the real problem. My server won't run chained background jobs correctly, but my laptops will. If I run the scripts in the previous problem on my laptops, they work perfectly. Script A starts Script B as a background job, and Script B calls Script C from within that job, and all output is received. If I run the exact same scripts on my server, Script A calls Script B, and Script B hangs indefinitely. If I

no receive-job results for gci when -path is a variable

谁说我不能喝 提交于 2019-12-24 07:40:33
问题 This returns nothing: $x = "C:\temp" Start-Job -ScriptBlock {get-childItem -path $x} | Wait-Job | Receive-job But providing the path parameter without a variable, like so... Start-Job -ScriptBlock {get-childItem -path C:\temp} | Wait-Job | Receive-job ...returns the contents of that temp folder, durrr.txt in this case. This is on Windows Server 2008R2 SP1 with Powershell $host.version output as follows: Major Minor Build Revision ----- ----- ----- -------- 3 0 -1 -1 Suspecting Powershell's v3

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: passing parameters to functions stored in variables

北城余情 提交于 2019-12-12 09:53:39
问题 I'm trying to get a simple working example of using functions inside of jobs. I've managed to pass my function into the scriptblock used for my job, but I can't seem to get parameters to the function. # concurrency $Logx = { param( [parameter(ValueFromPipeline=$true)] $msg ) Write-Host ("OUT:"+$msg) } # Execution starts here cls $colors = @("red","blue","green") $colors | %{ $scriptBlock = { Invoke-Expression -Command $args[1] Start-Sleep 3 } Write-Host "Processing: " $_ Start-Job

How do I call Start-Job which depends on a function in the same powershell module as the function calling Start-Job?

二次信任 提交于 2019-12-10 12:54:16
问题 I'm writing some powershell to talk to the AWS API, in a single module. I have written one function, Get-CloudFormation , which returns the status of a CloudFormation. I've written another function, Delete-CloudFormation , which after firing off a delete-CF API request, tries to start a job which polls the status of the CloudFormation using my Get-CloudFormation . I call Export-ModuleMember on Get-CloudFormation (but not Delete-CloudFormation ; that's a private function). Get-CloudFormation

Can Powershell Receive-Job return a DataSet?

一曲冷凌霜 提交于 2019-12-05 12:04:09
Background Info : I have an application that makes several SQL connections to multiple databases which currently takes a very very long time to execute. Powershell (.NET) will wait for each proceeding " SQL-GET " function to finish before it can fire off the next. I am under the impression I can speed this app up dramatically by firing each " SQL-GET " function in their own background job simultaneously!I will then retrieve the data from each job as they finish. Ideally as a DataSet system object. The Issues : When retrieving the data from the background job, I can ONLY manage to get a System