从PowerShell cmdlet返回错误的最佳做法是什么?
我正在为我们的应用程序配置需求编写一个基于PowerShell的XML模块。以下是其中一个函数从PowerShell cmdlet返回错误的最佳做法是什么?,powershell,Powershell,我正在为我们的应用程序配置需求编写一个基于PowerShell的XML模块。以下是其中一个函数 <# .Synopsis To update an XML attribute value .DESCRIPTION In the XML file for a particular attribute, if it contains valueToFind then replace it with valueToReplace .EXAMPLE -----------------
<#
.Synopsis
To update an XML attribute value
.DESCRIPTION
In the XML file for a particular attribute, if it contains valueToFind then replace it with valueToReplace
.EXAMPLE
-------------------------------Example 1 -------------------------------------------------------------------
Update-XMLAttribute -Path "C:\web.Config" -xPath "/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior/serviceMetadata" -attribute "externalMetadataLocation" -valueToFind "http:" -ValueToReplace "https:"
Look for the XPath expression with the attribute mentioned and search whether the value contains "http:". If so, change that to "https":
.EXAMPLE
-------------------------------Example 2 -------------------------------------------------------------------
Update-XMLAttribute -Path "C:\web.Config" -xPath "/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior/serviceMetadata" -attribute "externalMetadataLocation" -valueToFind "http:" -ValueToReplace "https:"
Same as Example 1 except that the attribute name is passed as part of the XPath expression
#>
function Update-XMLAttribute
{
[CmdletBinding()]
[OutputType([int])]
Param
(
# Web configuration file full path
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string]$Path,
# XPath expression up to the parent node
[string] $xPath,
# This parameter is optional if you mentioned it in xPath itself
[string] $attribute,
[string] $valueToFind,
[string] $ValueToReplace
)
Try
{
If (Test-path -Path $Path)
{
$xml = New-Object XML
$xml.Load($Path)
# If the xPath expression itself contains an attribute name then the value of attribute will be processed and taken
If ($xPath.Contains("@")) {
$xPath, $attribute = $xPath -split '/@', 2
}
# Getting the node value using xPath
$Items = Select-Xml -XML $xml -XPath $xPath
ForEach ($Item in $Items)
{
$attributeValue = $Item.node.$attribute
Write-Verbose "Attribute value is $attributeValue "
if ($attributeValue.contains($valueToFind)) {
Write-Verbose "In the attribute $attributeValue - $valueToFind is to be repalced with $ValueToReplace"
$Item.node.$attribute = $attributeValue.replace($valueToFind, $ValueToReplace)
}
}
$xml.Save($Path)
Write-Verbose " Update-XMLAttribute is completed successfully"
}
Else {
Write-Error " The $path is not present"
}
}
Catch {
Write-Error "$_.Exception.Message"
Write-Error "$_.Exception.ItemName"
Write-Verbose " Update-XMLAttribute is failed"
}
} # End Function Update-XMLAttribute
由于这个cmdlet将被许多人使用,我不认为简单地写入控制台是正确的方法
现在在我的脚本中如果没有错误,我可以假设我的脚本已经成功完成
从PowerShell cmdlet获取结果以使使用者知道其是否已成功完成的标准做法是什么?如果函数遇到错误,则应选择该函数。由调用方决定如何处理错误忽略、记录消息、终止等等。标准做法是抛出异常。每个不同类型的错误都有一个单独的异常类型,可用于进一步诊断
假设文件未表示,则执行以下操作:
if (-not (Test-Path $file))
{
throw [System.IO.FileNotFoundException] "$file not found."
}
您的cmdlet应该记录它将引发的所有可能的异常,以及何时引发。虽然您可以引发异常,PowerShell将捕获该异常并将其包装到ErrorRecord中,但您可以更灵活地使用该方法。这是基于C的cmdlet的典型方法
ThrowTerminatingError(new ErrorRecord(_exception, _exception.GetType().Name, ErrorCategory.NotSpecified, null));
这允许您选择错误类别并提供目标对象。顺便说一句,上面提到的不是我所说的cmdlet。cmdlet通常是用C语言编译的。您拥有的是一个高级功能:-
您可以通过高级功能访问此方法,如下所示:
$pscmdlet.ThrowTerminatingError(...)