0

Basically I want to make the following concise using the pipeline:

{Remove-Job 27}, {Remove-Job 29} | % { Invoke-Command -Session $s -ScriptBlock $_; };

To something like (conceptually)

@(27, 29) | % {Remove-Job $_;} | % { Invoke-Command -Session $s -ScriptBlock $_; };

What would be a good way to do this?

4 Answers 4

4

Your second example isn't quite right, because you have to emit a ScriptBlock in order to call Invoke-Command on it. Therefore, rather than actually calling Remove-Job on the local machine, you'd want to pass it as a ScriptBlock to the remote computer. Here is the most concise way I can think of at the moment, to achieve what you're after:

27, 29 | % { Invoke-Command -Session $s -ScriptBlock { Remove-Job -Id $args[0]; } -ArgumentList $_; };

Even though you didn't explicitly come out and display the code, it's obvious that you are pre-creating the PowerShell Session (aka. PSSession) object, prior to calling Invoke-Command. You can also simplify things by not pre-creating the PSSession, and simply using Invoke-Command with the -ComputerName parameter. Here is an example:

$ComputerList = @('server01.contoso.com', 'server02.contoso.com', 'server03.contoso.com');
Invoke-Command -ComputerName $ComputerList -ScriptBlock { Remove-Job -Id $args[0]; } -ArgumentList 27,29;

Note: I also moved the Job IDs directly into -ArgumentList, rather than piping them in. I generally try to avoid using the PowerShell pipeline, unless it really makes sense (eg. taking advantage of Where-Object, Get-Member, or Select-Object). Everyone has a different approach.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. I actually tried this previously, but I've got the following error: Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. + CategoryInfo : InvalidData: (:) [Remove-Job], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.Remov eJobCommand + PSComputerName : xxx.xx...
Sorry, I didn't test my code before I posted it :) I noticed what I did wrong, though. Since the ScriptBlock is a separate scope, we have to pass the Job ID in via the -ArgumentList parameter on Invoke-Command, and then reference it via $args[0]. I updated the code appropriately (after testing this time) :)
Thanks again for the help. I will take a look at how -ArgumentList works.
1

Any reason this wouldn't work?

Invoke-Command -Session $s -ScriptBlock { 27,29 |% {Remove-Job -Id $_ } }

3 Comments

Good answer - it depends on where the Job IDs are coming from, though. If he somehow wanted to pass them from the local system, he'd need to use -ArgumentList, as in my answer.
You could use [scriptblock]::create if they're arbitrary IDs. It would simplify the scripting if the jobs were named when they were created.
Thanks for the answer. I want the job ID's to be the leftmost thing of the command since that is the only part I'm editing for different job ID's. Having it there makes editing easier - otherwise I have to press many arrow keys :)
1

Another approach would be to use variable expansion, and the Create() method on the ScriptBlock type.

27, 29 | % { Invoke-Command -Session $s -ScriptBlock $([ScriptBlock]::Create("Remove-Job -Id $_")) };

I'm not sure that is any more concise than Trevor's approach.

Comments

0

The Id parameter of the Remove-Job cmdlet accepts an array of ID's so you could speicfy them like so:

Invoke-Command -Session $s -ScriptBlock { Remove-Job -Id 27,29 }

1 Comment

Thanks for the answer. I want the job ID's to be the leftmost thing of the command since that is the only part I'm editing for different job ID's. Having it there makes editing easier - otherwise I have to press many arrow keys :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.