gpt4 book ai didi

function - 将多个参数传递给Powershell中的函数变量

转载 作者:行者123 更新时间:2023-12-02 22:54:43 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How do I pass multiple parameters into a function in PowerShell?

(15 个回答)


4年前关闭。




我有一个我无法弄清楚的问题。这是一个无法解决的语法问题。假设我有这个功能:

function putStudentCourse ($userName, $courseId)
{
$url = "http://myurl/learn/api/public/v1/courses/courseId:" + $courseId + "/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

现在它只是返回第一个值 ($userName) 但输出显示如下:
{
"availability": {
"available": "Yes"
},
"courseRoleId": "Student",
"courseId": null
}

不知何故, $courseId 从未被填满,我不知道为什么。我究竟做错了什么?
任何帮助将不胜感激。

最佳答案

这是一个语法问题。定义函数时,您将参数放在括号中,就像您在此处正确做的那样:

function putStudentCourse ($userName, $courseId)

但是当你调用一个函数时,你会做 不是 把参数放在括号里。将您的代码更改为如下所示:
$output =  putStudentCourse $userName $courseId

powershell解释器解释原始代码
$output =  putStudentCourse($userName, $courseId)

意思是,“创建一个 ($userName, $courseId) 列表,并将其作为第一个参数传递给 putStudentCourse。”

关于function - 将多个参数传递给Powershell中的函数变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43618276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com