2017-04-25 3 views
0

알아낼 수없는 문제가 있습니다. 그것은 풀 수없는 구문 문제입니다.Powershell에서 함수 매개 변수에 여러 매개 변수 전달

function putStudentCourse ($userName, $courseId) 
{   
    $url = "http://myurl/learn/api/public/v1/courses/courseId:" + $courseId + "https://stackoverflow.com/users/userName:" + $userName 

    $contentType = "application/json"  
    $basicAuth = post_token 
    $headers = @{ 
       Authorization = $basicAuth 
      } 
    $body = @{ 
       grant_type = 'client_credentials' 
      } 
    $data = @{ 
      courseId = $courseId 
      availability = @{ 
       available = 'Yes' 
       } 
      courseRoleId = 'Student' 
     } 
    $json = $data | ConvertTo-Json 

    $putStudent = Invoke-RestMethod -Method Put -Uri $url -ContentType $contentType -Headers $headers -Body $json 

    return $json 
} 

그리고 내 주요 방법 :

#MAIN 

$userName = "user02"; 
$courseId = "CourseTest101" 

$output = putStudentCourse($userName, $courseId) 
Write-Output $output 

이 지금 그냥 처음 베일 ($ 사용자 이름)를 반환하지만,이 같은 출력을 보여줍니다

의 난이 기능이 있다고 가정 해 봅시다
{ 
    "availability": { 
         "available": "Yes" 
        }, 
    "courseRoleId": "Student", 
    "courseId": null 
} 

어쨌든 $ courseId는 채워지지 않으며 이유가 확실하지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 도움이 될 것입니다.

+0

추가 참고 사항 : [strict mode 사용] (https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/set-strictmode)을 사용하면이 잘못된 함수 호출 사용. – briantist

답변

3

구문 문제입니다. 함수를 정의 할 때 올바르게 여기처럼, 당신은 괄호 안에 매개 변수를 넣어 :

function putStudentCourse ($userName, $courseId) 

을하지만 함수를 호출 때, 당신은하지는 괄호 안에 인수를 배치해야합니까. "

$output = putStudentCourse $userName $courseId 

PowerShell을 인터프리터가

$output = putStudentCourse($userName, $courseId) 

가 의미하는 원래의 코드를 해석 ($ 이름, $ courseId)의 목록을 작성하고 같이 되었 : 다음과 같이 읽을 코드를 변경 첫 번째 매개 변수는 putStudentCourse입니다. "

+0

대단히 감사합니다. – SPedraza