Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查PowerShell中的Rest调用是否成功_Rest_Powershell_Error Handling - Fatal编程技术网

检查PowerShell中的Rest调用是否成功

检查PowerShell中的Rest调用是否成功,rest,powershell,error-handling,Rest,Powershell,Error Handling,我目前正在编写一个PowerShell脚本,该脚本将通过RESTAPI从工具中删除所有用户。当前脚本按预期工作,但缺少错误处理。我唯一要检查的是Rest调用是否成功完成。在谷歌搜索之后,我真的找不到太多,或者可能我在搜索错误的术语。任何想法或方向都非常感谢 下面是我成功使用的代码示例(假设您已经定义了Uri、头和主体(如果需要的话)-还要注意这里的方法设置为Post): 请看在try{}catch{}块中包装该命令。Microsoft提供了文档来解释这一点。非常感谢!我一定会调查的 tr

我目前正在编写一个PowerShell脚本,该脚本将通过RESTAPI从工具中删除所有用户。当前脚本按预期工作,但缺少错误处理。我唯一要检查的是Rest调用是否成功完成。在谷歌搜索之后,我真的找不到太多,或者可能我在搜索错误的术语。任何想法或方向都非常感谢

下面是我成功使用的代码示例(假设您已经定义了Uri、头和主体(如果需要的话)-还要注意这里的方法设置为Post):


请看在
try{}catch{}
块中包装该命令。Microsoft提供了文档来解释这一点。非常感谢!我一定会调查的
    try
    {
        Write-Verbose "Calling $Uri"
        Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body $Json -ContentType 'application/json' -ErrorAction Stop
    }
    catch
    {
        throw $_
    }