我有一个 PowerShell 脚本,它必须获取工作区详细信息并运行“ForEach”方法。 运行脚本时,我收到以下错误。
“HandleException:术语“HandleException”未被识别为 cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者 如果包含路径,请验证路径是否正确,然后重试。”
使用的示例 PowerShell 脚本:
Write-Output "Connecting Azure through xxxxx-xxxxx-xxxx-xxxx SPN login"
Connect-AzAccount -ServicePrincipal -Tenant $tenantId -CertificateThumbprint $thumb -ApplicationId $appId
#Get all workspaces from given tenant
$workspace = GetWorkspaceDetails -UCApiBaseUrl $UCApiBaseUrl
Write-Output "Total workspaces found"
$workspace.Count
$workspace
$edpWorkspaces = $workspace |Where-Object { $_.workspace_name -Like "az-dna-*" }
Write-Output "EDP filtered workspaces are "
$edpWorkspaces.Count
try
{
$edpWorkspaces|ForEach-Object{
$workspace_item = $_
$workspace_item
#Get External Locations
$externalLocations= GetExternalLocations -WorkspaceDeploymentName $($workspace.deployment_name)
$externalLocations
}
}
catch {
$ErrorMsg = HandleException -ErrorBody $_
Write-Error $ErrorMsg
exit 1
}
Powershell 术语 HandleException 不被识别为 cmdlet、函数、脚本文件或可操作程序的名称
没有称为
HandleException
的内置函数,但您可以创建用户定义的 HandleException
函数,如下所示:
$ErrorActionPreference = "Stop"
function HandleException {
param(
[string]$ritherror
)
Write-Host "Hello Rithwik Error has been occurred: $ritherror"
}
try {
$out = Get-Item -Path "NoFile.txt"
}
catch {
HandleException -ritherror $_.Exception.Message
}
您应该使用
$ErrorActionPreference = "Stop"
,然后只有当您有命令并且它给出输出/错误时,catch才有效。
输出: