moitasto
moitasto

Reputation: 11

Powershell passing variables to a job

Function start
{
$BackupList = Import-Csv C:\file.csv -Delimiter ";"
    ForEach($computer in $BackupList) 
    {
        $arrayBackup = ($computer.Backup).split(".")
        $backupdate = Get-Date -Day $arrayBackup[0] -Month $arrayBackup[1] -Year $arrayBackup[2]
        $datetoday = Get-Date -format d
        $countingday = (get-date $datetoday).AddMonths(-3)
            if ($Zahl -eq 1) 
            {
                if ($backupdate -le $countingday) 
                {
                $global:client = $computer.computername
                $global:mac = $computer.MAC
                $global:Name = $computer.device
                $global:lastsaved = [int]$computer.lastsavetime
                    if ($computer.computername -match $Client) 
                    {
                                        {
                                $computer.Backup = $datetoday
                                if ($lastsaved -ne 4) 
                                {
                                        $lastsaved++
                                } 
                                else 
                                {
                                    $lastsaved = 1
                                }
                                    if ($lastsaved -eq 1) 
                                    {
                                        $global:savingplace = "\\Savingplace1"
                                    }
                                    elseif ($lastsaved -eq 2) 
                                    {
                                        $global:savingplace = "\\Savingplace2"
                                    }
                                    elseif ($lastsaved -eq 3) 
                                    {
                                        $global:savingplace = "\\Savingplace3"
                                    }
                                    elseif ($lastsaved -eq 4) 
                                    {
                                        $global:savingplace = "\\Savingplace4"
                                    }
                                $computer.lastsavetime = $lastsaved
                            }   
                        }
                        Start-Job -InitializationScript $functions -ScriptBlock {NASLOGIN} -argumentlist $_ | Receive-Job
                    }
    }   
}
}

I am reading some information out of a csv-file. i want to pass some of this information read to a job. in the job im doing quiet a lot of function calls and operations using this variables. but at the moment there is no variable passed to the job. how can i fix this?

Upvotes: 1

Views: 675

Answers (2)

moitasto
moitasto

Reputation: 11

Start-Job -ScriptBlock {NASLOGIN $args[0]$args[1]$args[2]$args[3]$args[4]} -argumentlist $var1,$var2,$var3,$var4,$var5 -InitializationScript $functions } } } $functions = { Function NASLogin { $var1 = $args[0] $var2 = $args[1] $var3 = $args[2] $var4 = $args[3] $var5 = $args[4] Have done it like this and it works. Thanks for your help.

Upvotes: 0

Keith Hill
Keith Hill

Reputation: 201702

You are passing $_ to the ArgumentList parameter but I don't see anywhere in where you code where there is a pipeline active - hence $_ is not defined. To pass variables to the job, specify valid variables in the ArgumentList e.g.:

$foo = 'foo'
$bar = 'bar'
Start-Job {param($a, $b) "a is $a, b is $b"} -Arg $foo,$bar | Receive-Job -Wait

Upvotes: 1

Related Questions