2012-02-10 2 views
6

좋아요. 나는 학교 과제를 끝내려고하고 있으며 내 인생을 위해 그것을 이해할 수 없다. powershell을 사용하여 한 함수에서 다른 함수로 값을 전달하여 "모듈 형"유형 스크립트를 작성하려고합니다. $ script : xxxxx를 사용하지 않고 함수의 범위 밖으로 값을 이동하는 방법을 알아낼 수 없습니다. powershell의 값을 정규 인수 매개 변수로 참조로 전달하는 다른 방법이 있습니까?Powershell이 ​​인수 값을 매개 변수로 전달하고 뒤로

여기에 내가 가진 무엇 다음 inputGrams 기능은 스크립트가 실행될 때마다 변경해야하지만이 때문에 범위 문제와 가치를 전달하지

function main 
{ 
inputGrams($carbGrams, $fatGrams) 
$carbGrams 
$carbGrams 
calcGrams 
displayInfo 
} 

function inputGrams([ref]$carbGrams, [ref]$fatGrams) 
{ 
    $carbGrams = read-host "Enter the grams of carbs per day" 
    $fatGrams = read-host "Enter the grams of fat per day" 
} 

function calcGrams 
{ 
    $carbCal = $carbGrams * 4 
    $fatCal = $fatGrams * 9 
} 

function displayInfo 
{ 
    write-host "The total amount of carb calories is $carbCal" 
    write-host "The total amount of fat calories is $fatCal" 
} 

main 

두 값 직후. 누구든지 올바르게 그 값을 주요 기능으로 다시 전달하는 방법을 알고 있습니까?

+0

어느 부분 숙제, 계산 영양 정보 또는 PowerShell 스크립트를 작성입니까? 후자가 내가 학교를 좋아하는 경우 :-) –

답변

1

앤디는 바른 길에 있지만 는 [참조] 까다로운이며, 당신이을 할 수있는 경우에이를 방지하는 것이 좋습니다.

말씀 드린대로 문제는 범위입니다. 모든 함수 (메인 제외)는 main에서 호출되므로 메인의 범위, 즉 부모 범위 인 변수를 설정 변수 또는 새 변수로 설정하여이 함수에서 변수를 사용할 수 있도록해야합니다.

같은 점은 Get-Variable을 사용하여 값을 검색 할 때 유효합니다.

function main 
{ 
    inputGrams 
    $carbGrams 
    $fatGrams 
    calcGrams 
    displayInfo 
} 

function inputGrams 
{ 
    # type constrain as Single because Read-Host returns a String 
    [single]$carbs = read-host "Enter the grams of carbs per day" 
    [single]$fat = read-host "Enter the grams of fat per day" 

    # scope 1 is the parent scope, i.e. main's scope 
    Set-Variable -Name carbGrams -Value $carbs -Scope 1 
    Set-Variable -Name fatGrams -Value $fat -Scope 1 
} 

function calcGrams 
{ 
    # scope 1 is the parent scope, i.e. main's scope 
    Set-Variable -Name carbCal -Value ($carbGrams * 4) -Scope 1 
    Set-Variable -Name fatCal -Value ($fatGrams * 9) -Scope 1 
} 

function displayInfo 
{ 
    # scope 1 is the parent scope, i.e. main's scope 
    $_carbCal = Get-Variable -Name carbCal -Scope 1 -ValueOnly 
    $_fatCal = Get-Variable -Name fatCal -Scope 1 -ValueOnly 

    write-host "The total amount of carb calories is $_carbCal" 
    write-host "The total amount of fat calories is $_fatCal" 
} 

main 

PS : 난 그냥 도와 싶어, 당신의 학교 과제를 망치고하지 않은 희망)

+0

회피 권장 사항에 대한 언급이 있으십니까? –

+1

p. 25 of http://www.manning.com/payette/wpia_errata.pdf – zx38

+0

Bruce의 예는 내가 기대했던 것처럼 좋았습니다 ... 나는 스스로 선택한 REF가 아니었지만 OP의 시나리오에서는 괜찮다고 생각합니다. 다른 한편으로는 일반적으로 데이터의 범위를 쉽게 볼 수 있도록 생성해야하는 경우 함수에서 값을 반환하려고합니다. –

13

몇 가지 문제가 있습니다.

function main 
{ 
    # 1. Create empty variable first. 
    New-Variable -Name carbGrams 
    New-Variable -Name fatGrams 

    # 2. Spaces in between parameters. Not enclosed in parens. 
    # 3. Put REF params in parens. 
    inputGrams ([ref]$carbGrams) ([ref]$fatGrams) 

    $carbGrams 
    $fatGrams 
} 

function inputGrams([ref]$carbGrams, [ref]$fatGrams) 
{ 
    # 4. Set the Value property of the reference variable. 
    $carbGrams.Value = read-host "Enter the grams of carbs per day" 
    $fatGrams.Value = read-host "Enter the grams of fat per day" 
} 

main 

그리고 설명 : 당신은 REF로 전달하기 전에 변수를 만들어야합니다

  1. 먼저 여기에 작업 예입니다.
  2. PowerShell 함수 매개 변수를 괄호로 묶지 마십시오. 공백으로 구분하면됩니다.
  3. REF 인수를 괄호 안에 넣으십시오.
  4. REF 변수의 값을 설정하려면 Value 속성을 설정해야합니다.
+1

"가치"속성은 나에게있어 ... 덕분에 앤디! – slashp

관련 문제