none
Powershell: scriptblock does not accept my parameter type? RRS feed

  • Question

  • 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?



    • Edited by Dimmts Monday, August 5, 2013 8:30 AM
    • Moved by Bill_Stewart Monday, March 24, 2014 9:42 PM Abandoned
    Monday, August 5, 2013 8:28 AM

All replies

  • does that command run properly without the job?
    Monday, August 5, 2013 2:12 PM
  • 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 {
                Param 
                    (
                        $SiteCode, 
                        $resourcett, 
                        $siteserver, 
                        $cred
                    )
                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

    • Proposed as answer by David Wyatt Monday, August 5, 2013 2:24 PM
    Monday, August 5, 2013 2:20 PM
  • 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

    Monday, August 5, 2013 2:21 PM
  • 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.


    Monday, August 5, 2013 2:37 PM
  • Can you clarify your last comment?

    Hope that helps! Jason

    Monday, August 5, 2013 2:49 PM
  • That I already tried the code that you posted.

    Same error result in both cases.

    Monday, August 5, 2013 2:55 PM
  • was reading this http://stackoverflow.com/questions/11844390/how-do-i-pass-a-scriptblock-as-one-of-the-parameters-in-start-job

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

    • Proposed as answer by Brain2000 Thursday, December 6, 2018 5:07 AM
    Monday, August 5, 2013 2:56 PM
  • 
    

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


    Hope that helps! Jason

    Monday, August 5, 2013 3:08 PM
  • 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

    Monday, August 5, 2013 3:09 PM
  • 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.

    • Edited by Brain2000 Thursday, December 6, 2018 5:08 AM
    Thursday, December 6, 2018 5:06 AM