如何从孩子那里可靠地获得变量

时间:2014-01-24 14:23:21

标签: powershell

我正在Powershell中构建一个SQL Job Generator。我的PS技能不是最好的,

我想获得在函数CreateSqlTask​​中创建的值。我想要的变量是$ job。我从这个函数中得到了一个对象数组。

#My call  
$returnParams = CreateSqlTask ( LIST OF PARAMS)

$ returnParams [0]是jobSchedule的值Creation $ returnParams [1]是我想要的变量,这是$ job的值。

作为一名程序员我不相信它只是假设$ returnParams [1]总是我需要的变量。处理这种情况的正确方法是什么?

 #Here is the function implementation:
function CreateSqlTask
{    

    Param ( LIST OF PARAMS   )

    #Make all errors terminating
    $ErrorActionPreference = "Stop"

    #Create the SQL Job
    $job = CreateSqlJob -serverInstance $serverInstance -jobName $jobName -jobDesc $jobDesc -jobCategory $jobCategory -jobAlertOperator $jobAlertOperator -jobEmailLevel $jobEmailLevel

    #Create the SQL Job Step
    $jobStep = CreateSqlJobStep $job $stepName $stepCmd

    #Alter the Job to tell it what step should execute first
    $job.StartStepID = $jobStep.ID
    $job.Alter()

    #Create the SQL Job Schedule
    CreateSqlJobSchedule $job `
                            $schedName `
                            $schedFreqType `
                            $schedFreqRecurFactor `
                            $schedFreqInterval `
                            $schedFreqSubDayType `
                            $schedFreqSubDayInterval `
                            $startingSchedHour `
                            $startingSchedMinute `
                            $endingSchedHour `
                            $endingSchedMinute

    return $job     
}

3 个答案:

答案 0 :(得分:1)

函数应该只返回一种类型的对象。如果您不需要作业创建返回,则可以将该输出发送到$ null或将其分配给函数中的变量。如果您确实需要它以及其他信息,我将创建一个自定义对象或哈希表,其中包含该信息以及作业信息并返回该信息。

答案 1 :(得分:0)

您拥有的一个选项是在CreateSQLJob函数中返回包含所需相关信息的自定义对象。

例如:

$job = New-Object Object
$obj | Add-Member Noteproperty serverInstance -value $serverInstance
$obj | Add-Member Noteproperty jobName  -value $jobName

然后你就可以调用这些属性:

$obj.jobName

以上是一个人为的例子。

答案 2 :(得分:0)

正如您所指出的,CreateSqlJobSchedulereturn $job都是返回值。如果您需要这两个,我建议您将这些分配给一个新对象:

$jobDetails = @{
    CreateSqlJob = $CreateSqlJob             
    CreateSqlJobSchedule = $CreateSqlJobSchedule
}

return $jobDetails

以上假设您已将两个调用分配给两个变量,然后可以按名称引用它们:

$createSqlTaskResults = CreateSqlTask
$createSqlTaskResults.CreateSqlJob 
$createSqlTaskResults.CreateSqlJobSchedule 

以下是您的示例中的外观:

 #Here is the function implementation:
function CreateSqlTask
{    

    Param ( LIST OF PARAMS   )

    #Make all errors terminating
    $ErrorActionPreference = "Stop"

    #Create the SQL Job
    $CreateSqlJob = CreateSqlJob -serverInstance $serverInstance -jobName $jobName -jobDesc $jobDesc -jobCategory $jobCategory -jobAlertOperator $jobAlertOperator -jobEmailLevel $jobEmailLevel

    #Create the SQL Job Step
    $jobStep = CreateSqlJobStep $job $stepName $stepCmd

    #Alter the Job to tell it what step should execute first
    $job.StartStepID = $jobStep.ID
    $job.Alter()

    #Create the SQL Job Schedule
    $CreateSqlJobSchedule = CreateSqlJobSchedule $job `
                            $schedName `
                            $schedFreqType `
                            $schedFreqRecurFactor `
                            $schedFreqInterval `
                            $schedFreqSubDayType `
                            $schedFreqSubDayInterval `
                            $startingSchedHour `
                            $startingSchedMinute `
                            $endingSchedHour `
                            $endingSchedMinute

    $jobDetails = @{
        CreateSqlJob = $CreateSqlJob             
        CreateSqlJobSchedule = $CreateSqlJobSchedule
    }

    return $jobDetails   
}