在Powershell

时间:2018-05-14 03:42:17

标签: powershell

我有一个包含2个功能的powershell脚本:

function Stonebrach.Connect {
param(
    [Parameter(Mandatory=$true)]
    $Sb_uri,
    [Parameter(Mandatory=$true)]
    $Auth_filename,
    [Parameter(Mandatory=$true)]
    $Method,
    [Parameter(Mandatory=$false)]
    $Body
)

$basicAuth=Get-Content "C:\$auth_filename"
$headers = @{
        "Authorization" = $basicAuth
        "Content-Type"="application/json"
}
$RSP=$null

try{
    if ( $body -eq $null ) {
        $response = invoke-restmethod -Uri $Sb_uri -Method $Method -Headers $headers
    }
    else {
        $response = invoke-restmethod -Uri $sb_uri -Method $method -Headers $headers -Body $body
    }
    return $response
}
catch{
   $RSP = $_.Exception.Message
   return $RSP
}
}

function Stonebranch.Create.Task.Windows {
param(
    [Parameter(Mandatory=$true)]
    $Sb_base_uri,
    [Parameter(Mandatory=$true)]
    $Auth_filename,
    [Parameter(Mandatory=$true)]
    $Method,
    [Parameter(Mandatory=$true)]
    $Body
)
    Stonebrach.Connect -SB_uri $Sb_base_uri/task -Auth_filename $Auth_filename -Method $Method -Body $Body

}

身体参数的硬编码如下:

$body = '
{
"type" : "taskWindows",
"actions" : {
  "abortActions" : [],
  "emailNotifications" : [],
  "setVariableActions" : [],
  "snmpNotifications" : [],
  "systemOperations" : []
},
"agent" : "test",
"name" : "Test Create",
"summary" : "Test Create"
}
'

调用该函数:

$response = Stonebranch.Create.Task.Windows -Sb_base_uri $Sb_base_uri -Auth_filename $Auth_filename -Method POST -Body $body

以下是问题:我想将$ body分成不同的文件(更喜欢YAML文件)。任何人都知道如何分离并将param列表传递给脚本,以便我可以使用它们?我发现很难传递参数列表并调用函数。

请帮帮我,谢谢。

0 个答案:

没有答案