Running a job for ps1 with parameters.

I’ve been trying to use start-job with arguments to run my script.

I’m using the following to start the job:

$servernames = @("server1","server2","server3","server4")
foreach ($servername in $servernames)
{Start-Job -name $servername -FilePath "C:\Users\blah\Desktop\getJava-ver1.ps1" -ArgumentList $servername}

Calling the following script:

param ($servername)
$script = {
$drives = (Get-WmiObject Win32_LogicalDisk -Filter DriveType=3).deviceid
foreach ($drive in $drives)
{
$d = $drive + '\'
Get-ChildItem -File -Recurse -Path $d -ErrorAction SilentlyContinue -Include 'filename.exe' | Select-Object versioninfo
}}
Write-Output $servername | Out-File -filepath e:\temp\output.txt -force -append
Invoke-Command -ComputerName $ServerName -ScriptBlock $script | Out-File -filepath e:\temp\output.txt -force -append

Unfortunately it seems like I’m missing something there, the jobs are created but I’m not getting any results, if I run the scrip by itself and specify the parameter, it works fine - what am I missing?
Thank you

# Cleanup any running jobs
Get-Job | Remove-Job -Force

# Start jobs
$ServerList = @('server1','server2','server3','server4')
foreach ($ServerName in $ServerList) {
    Invoke-Command -computername $ServerName -ScriptBlock {
        $DriveList = (Get-WmiObject Win32_LogicalDisk -Filter DriveType=3).deviceid
        foreach ($Drive in $DriveList) {
            Get-ChildItem -File -Recurse -Path "$Drive\" -ErrorAction SilentlyContinue -Include ‘ProcessKiller.exe’ | Select-Object versioninfo
        }
    } -AsJob
}

# Wait jobs:
$JobList = Get-Job 
while ($JobList.State -match 'Running') {
    $JobList
    "Job(s) still running, waiting 10 sec.."
    Start-Sleep -Seconds 10
    $JobList = Get-Job 
}

# Receive job data, store in file
foreach ($Job in $JobList) {
    "Server Name: $($Job.Location)" | Out-File -filepath e:\temp\output.txt -force -append
    Receive-Job -Job $Job -Keep | FL VersionInfo | Out-File -filepath e:\temp\output.txt -force -append
} 

Ah, so it looks like i’ve missed the receive-job part (apart from couple other things) - thank you!

Now, another question, is there an easy way to throttle this? I’ve tried adding -ThrottleLimit to the job but that does not seem to work.

Thank you