Powershell: scriptblock does not accept my parameter type?


  • Hi,

    i try to query Device ID collection membership for SCCM2012

    So I created a job in powershell to get all the device collections where my Device is member of.

    My error:

    Start-Job : Cannot bind parameter 'InitializationScript'. Cannot convert the "16777289" value of type "System.Int32" to type "System.Management.Automation.ScriptBlock".

    the number 16777289 is my Resource ID

    the code I run:

    	$job1 = start-job -scriptblock {Get-WmiObject -Namespace root\sms\site_$SiteCode -Class sms_collectionmember -Filter "ResourceID = '$resourcett'" -ComputerName $siteserver -Credential $cred} -ArgumentList $SiteCode $resourcett $siteserver $cred

    I've tried already a some different aproaches but I keep getting this error.

    anybody here know what is going wrong?

    • Diedit oleh Dimmts Senin, 05 Agustus 2013 08.30
    • Dipindahkan oleh Bill_Stewart Senin, 24 Maret 2014 21.42 Abandoned
    Senin, 05 Agustus 2013 08.28

Semua Balasan

  • does that command run properly without the job?
    Senin, 05 Agustus 2013 14.12
  • I don't know about your actual code but before you can use variables inside your scriptblock, either with jobs or with remoting, you need to catch them using a param block:

    $job1 = start-job -scriptblock {
                Get-WmiObject -Namespace root\sms\site_$SiteCode -Class sms_collectionmember -Filter "ResourceID = '$resourcett'" -ComputerName $siteserver -Credential $cred
            } -ArgumentList $SiteCode $resourcett $siteserver $cred

    Also work on formatting your code, it's really hard to work with a giant line in a codeblock.

    Hope that helps! Jason

    • Disarankan sebagai Jawaban oleh David Wyatt Senin, 05 Agustus 2013 14.24
    Senin, 05 Agustus 2013 14.20
  • And you can do the same thing with the builtin $args variable if you don't like the Param block:

    $job1 = start-job -scriptblock {
                Get-WmiObject -Namespace root\sms\site_$Args[0] -Class sms_collectionmember -Filter "ResourceID = '$args[1]'" -ComputerName $args[2] -Credential $args[3]
            } -ArgumentList $SiteCode $resourcett $siteserver $cred

    Hope that helps! Jason

    Senin, 05 Agustus 2013 14.21
  • Sorry for the formatting, just copy/pasted the line from my editor.

    Already tried does, keep getting that error discribed in my question.

    And Yes the command works perfectly without the scriptblock, and the scriptblock works when I don't use variables.

    Senin, 05 Agustus 2013 14.37
  • Can you clarify your last comment?

    Hope that helps! Jason

    Senin, 05 Agustus 2013 14.49
  • That I already tried the code that you posted.

    Same error result in both cases.

    Senin, 05 Agustus 2013 14.55
  • was reading this

    might be similar to your issue eventhough example is a string not an int

    • Disarankan sebagai Jawaban oleh Brain2000 Kamis, 06 Desember 2018 05.07
    Senin, 05 Agustus 2013 14.56

    Can you post more of your code?  I'd be curious to see how you're filling the variables.

    Hope that helps! Jason

    Senin, 05 Agustus 2013 15.08
  • Alternatively if you are worried about sharing too much info if I got a gettype() off of all of them that might be enough.

    Hope that helps! Jason

    Senin, 05 Agustus 2013 15.09
  • The problem is, powershell tries to serialize objects being sent to Start-Job. Your solution tells me that you don't know how this works.

    • Diedit oleh Brain2000 Kamis, 06 Desember 2018 05.08
    Kamis, 06 Desember 2018 05.06