test to learn about jobs

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

test to learn about jobs

Post by Domtar »

hi all,

could someone please explain to me why this works:

Code: Select all

$computername = 'compname'
Start-Job -ScriptBlock {
	Get-WmiObject -ComputerName 'compname' -Class Win32_BIOS | Select-Object *
} -Name "$($computername)bios"

receive-job -name "$($computername)bios" -keep
but this does not?

Code: Select all

$computername = 'compname'
Start-Job -ScriptBlock {
	Get-WmiObject -ComputerName $computername -Class Win32_BIOS | Select-Object *
} -Name "$($computername)bios"

receive-job -name "$($computername)bios" -keep

error: Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or
empty, and then try the command again.

thanks!
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: test to learn about jobs

Post by Domtar »

this doesn't work either, same error.

Code: Select all

$pcname = 'somepc'
$id = (Start-Job -ScriptBlock {
		Get-WmiObject -ComputerName $pcname -Class Win32_BIOS | Select-Object *
	}
).Id
$job = Get-Job -Id $id
Receive-Job -Name $job.Name -Keep 
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 11:13 am
Answers: 39
Been upvoted: 30 times

Re: test to learn about jobs

Post by brittneyr »

This is because in the scope of the scriptblock, your variable does not exist. The script block runs in an independent scope where no variables in the local/calling scope are available. You can pass it with the parameter ArgumentList or use the $using: scope modifier to access local variables.
Brittney
SAPIEN Technologies, Inc.
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: test to learn about jobs

Post by Domtar »

brittneyr wrote: Thu Jan 12, 2023 7:40 am This is because in the scope of the scriptblock, your variable does not exist. The script block runs in an independent scope where no variables in the local/calling scope are available. You can pass it with the parameter ArgumentList or use the $using: scope modifier to access local variables.
sorry, i should have said that the computer name is passed as a parameter.

it works correctly up to the point where i try to use some dynamic variable.

have a look at the last code i posted.
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: test to learn about jobs

Post by Domtar »

OK this seems to be working and do what i need;

Code: Select all

$pcname = 'somepc'
$jobName = "$($pcname)bios"
$sb = {
	Get-WmiObject -Class Win32_BIOS 
}

Invoke-Command -ScriptBlock $sb -ComputerName $pcname -AsJob -JobName $jobName

This topic is 1 year and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked