1

I am new to this Job things. I am getting the error of : Cannot bind parameter 'ScriptBlock'. Cannot convert the | ".\test7\test7-2.ps1" value of type "System.String" to type "System.Management.Automation.ScriptBlock". Can anyone tell me and figure out what is happening here?

$importCsv = ".\testData.csv"
$dir = "test\"
$main = ".\test7\test7-2.ps1"

$csv = GetCsv $importCsv

foreach ($i in $csv){
   Start-Job -ScriptBlock $main -ArgumentList @($dir,$i.ip,$i.openTime,$i.closeTime)
}

the $main is here:


function Main {

    param ($directory, $itemIp, $itemOpen, $itemClose)

    $ipAddress = $itemIp
    $openTime = $itemOpen
    $closeTime = $itemClose

    $date = Get-Date -Format "yyyy-MM-dd"
    $storeFolder = addFolder $directory $ipAddress
    $todayDir = addFolder $storeFolder $date
    $pingFile = addFile $todayDir
    
    Write-Host "Date : $date" 
    Write-Host "Store Folder : $storeFolder" 
    Write-Host "Today Folder : $todayDir" 
    Write-Host "File path : $pingFile." 
    Write-Host "ip address : $ipAddress" 
    Write-Host "Open Time : $openTime" 
    Write-Host "Close Time : $closeTime" 
    
    $time = Get-Date -Format "HH:mm"
    Write-Host "Time : $time"
    Compare-Time $time $openTime $closeTime $timeNow $ping4 $ping15 $ipAddress $pingFile 4 3 15 1

}

Then I also tried like this $main = .\test\test7-2.ps1 but it return Cannot bind argument to parameter 'ScriptBlock' because it is| null.

bah
  • 13
  • 1
  • 5
  • 5
    Scriptblocks are defined by the use of braces `{ code... }`. Have a look [here](https://stackoverflow.com/questions/19834643/powershell-how-to-pre-evaluate-variables-in-a-scriptblock-for-start-job) for some usage examples for Start-Job and [here](https://mcpmag.com/articles/2019/05/08/understanding-powershell-scriptblocks.aspx#:~:text=The%20scriptblock%20is%20a%20unique,anonymous%20function%20or%20a%20lambda.&text=A%20scriptblock%20can%20be%20assigned%20to%20a%20variable%2C%20unlike%20a%20function.) for more about scriptblocks – Daniel Feb 18 '21 at 03:12
  • make sure that the params in your main are in the same order as your argumentlist in the start-job. – Peter the Automator Feb 18 '21 at 08:03

1 Answers1

0

Try following code.

$MyMain = Get-Content ".\test7\test7-2.ps1"
$MainScriptBlock = [Scriptblock]::Create($MyMain)
foreach ($i in $csv){
   Start-Job -ScriptBlock $MainScriptBlock -ArgumentList @($dir,$i.ip,$i.openTime,$i.closeTime)
}

....

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 04 '23 at 16:29