none
Start-Job Pass String Variable RRS feed

  • Question

  • Hello,

    Am testing using the PowerShell Start-Job commend, I have been successful in what I need but have run into an issue.

    $id = '58*'
    $jobScript = { Get-ADUser -Properties employeeID, mail -Filter {employeeID -like $args} | Out-String }
    
            try
    	{
    		$job = Start-Job -Name "Pro" -InitializationScript {Import-Module ActiveDirectory} -ArgumentList $id -ScriptBlock $jobScript
    	}
    	catch [Exception]
    	{
    		Write-Debug $_
    	}
    
    $jobdata = Receive-Job -Name "Pro"

    But I get this error;

    Invalid type 'System.Object[]'.

    Parameter name: employeeID   

    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException   

    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser   

    + PSComputerName        : localhost

    Am I able to pass the string $id into the Start-Job?!?

    Thanks for any help.

    Karl

    Tuesday, October 27, 2020 11:30 AM

Answers

  • Hello,

    I tried this and it didn't work.

    I ended up creating a scriptblock as below and that worked.

    Thanks for looking.

    $userLookup = $txtLookupUser
    						$ADUserLookupjobScript = [scriptblock]::create("Get-ADUser -Properties employeeID, mail -Filter {employeeID -like ""$userLookup""} | Out-String")
    
    try{
    								
    									$ADUserLookupjob = Start-Job -Name "ADUserLookup" -InitializationScript {Import-Module ActiveDirectory} -ScriptBlock $ADUserLookupjobScript
    								
    								}
    								catch [Exception]{
    									Write-Debug $_
    								
    								}


    • Marked as answer by karl_009 Thursday, October 29, 2020 2:36 PM
    Thursday, October 29, 2020 2:36 PM

All replies

  • $args[0] instead of $args

    The opinion expressed by me is not an official position of Microsoft

    • Proposed as answer by jrv Tuesday, October 27, 2020 9:22 PM
    Tuesday, October 27, 2020 2:34 PM
  • Hello,

    I tried this and it didn't work.

    I ended up creating a scriptblock as below and that worked.

    Thanks for looking.

    $userLookup = $txtLookupUser
    						$ADUserLookupjobScript = [scriptblock]::create("Get-ADUser -Properties employeeID, mail -Filter {employeeID -like ""$userLookup""} | Out-String")
    
    try{
    								
    									$ADUserLookupjob = Start-Job -Name "ADUserLookup" -InitializationScript {Import-Module ActiveDirectory} -ScriptBlock $ADUserLookupjobScript
    								
    								}
    								catch [Exception]{
    									Write-Debug $_
    								
    								}


    • Marked as answer by karl_009 Thursday, October 29, 2020 2:36 PM
    Thursday, October 29, 2020 2:36 PM