gpt4 book ai didi

powershell - 从 PowerShell cmdlet 返回错误的最佳做法是什么?

转载 作者:行者123 更新时间:2023-12-03 11:24:55 25 4
gpt4 key购买 nike

我正在为我们的应用程序配置需求编写一个基于 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

-------------------------------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 获取结果以便消费者知道它是否成功完成的标准做法是什么?

最佳答案

你的函数应该 throw 如果遇到错误。让调用者决定如何处理错误(忽略、记录消息、终止等等)。

关于powershell - 从 PowerShell cmdlet 返回错误的最佳做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30214523/

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