2

I have to be missing something incredibly simple here. Here's a very basic script to illustrate what I'm trying:

$Computers = @('comp1', 'comp2')

$ScriptBlock = {
    New-Item "C:\Temp\$C.txt" -Force
}

Foreach ($C in $Computers)
{
    Start-Job -ScriptBlock $ScriptBlock -ArgumentList $C
}

The script runs, but $C is not passed, so i just get a ".txt" file in my folder. What simple thing am I overlooking here?

Tchotchke
  • 399
  • 1
  • 2
  • 18

1 Answers1

3

Replace this:

$ScriptBlock = {
    New-Item "C:\Temp\$C.txt" -Force
}

With this:

$ScriptBlock = 
{
    param($C)
    New-Item "C:\Temp\$C.txt" -Force
}

Note: When you are passing the arguments in the argumentlist , then make sure the same number of arguments have to accepted inside the scriptblock by using param.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45