2017-04-25 22 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

その他の注意:[厳密モードを使用する](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のインタプリタは「意味($ユーザ名、$ courseId)のリストを作成し、としてそれを渡すには、元のコード

$output = putStudentCourse($userName, $courseId) 

を解釈しますputStudentCourseの最初のパラメータです。 "

+0

ありがとうございました! – SPedraza

関連する問題