嗨朋友们我想将<Option Name="Activate">1</Option>
中的某些文字值修改为<Option Name="Activate">0</Option>
。我无法使用PowerShell找到节点值<Option Name="Activate">1</Option>
。下面是我想要修改文本的完整代码部分。请帮忙。
<User Name="Richard">
<Option Name="Pass">b56e0b4ea4962283bee762525c2d490f</Option>
<Option Name="Containers">MasterVal</Option>
<Option Name="Max Limit Allowed">2</Option>
<Option Name="Limit Allowed">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="Activate">1</Option>
<Option Name="Feedback"></Option>
<Option Name="ForceSsl">2</Option>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations />
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="2" UlType="0" UlLimit="10" ServerUlLimitBypass="2">
<Download />
<Upload />
</SpeedLimits>
</User>
到目前为止,我已经尝试了以下代码,但无法完成。 :(
$xml = New-Object XML
$XML.Load("D:\PSScript\BookStore.xml")
$xml.bookstore.Users.user |
Where-Object { ($_.Name -eq 'Richard')} |
Select-Object -Property Option
**注意:我想在这里使用一个条件,例如我想仅为特定用户更改1 Tag文本值,简而言之,我想根据用户ID修改文本,比如name = Richard更改其{{ 1}}到<Option Name="Activate">1</Option>
**
<Option Name="Activate">0</Option>
<BookStore>
<Settings>
<Item name="Admin port" type="numeric">88881</Item>
</Settings>
<Container>
<Containers Name="MasterVal">
<Option Name="Max Limit Allowed">0</Option>
<Option Name="Limit Allowed">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="Activate">1</Option>
<Option Name="Feedback">
</Option>
<Option Name="ForceSsl">0</Option>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations>
<Authorization path="E:\BookMaster\RootFolder">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">0</Option>
<Option Name="FileDelete">0</Option>
<Option Name="FileAppend">0</Option>
<Option Name="pathCreate">0</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">1</Option>
<Option Name="AutoCreate">0</Option>
</Authorization>
<Authorization path="E:\BookMaster\RootFolder\MST\reports">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">1</Option>
<Option Name="FileDelete">1</Option>
<Option Name="FileAppend">1</Option>
<Option Name="pathCreate">1</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">0</Option>
<Option Name="AutoCreate">0</Option>
</Authorization>
<Authorization path="E:\BookMaster\RootFolder\MST\NT">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">1</Option>
<Option Name="FileDelete">1</Option>
<Option Name="FileAppend">1</Option>
<Option Name="pathCreate">1</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">0</Option>
<Option Name="AutoCreate">0</Option>
</Authorization>
<Authorization path="E:\BookMaster\RootFolder\MST\WIN">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">1</Option>
<Option Name="FileDelete">1</Option>
<Option Name="FileAppend">1</Option>
<Option Name="pathCreate">1</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">0</Option>
<Option Name="AutoCreate">0</Option>
</Authorization>
<Authorization path="E:\BookMaster\RootFolder\MST\Winsxs">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">1</Option>
<Option Name="FileDelete">1</Option>
<Option Name="FileAppend">1</Option>
<Option Name="pathCreate">1</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">0</Option>
<Option Name="AutoCreate">0</Option>
</Authorization>
<Authorization path="E:\BookMaster\RootFolder\SXS">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">1</Option>
<Option Name="FileDelete">1</Option>
<Option Name="FileAppend">1</Option>
<Option Name="pathCreate">1</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">0</Option>
<Option Name="AutoCreate">1</Option>
</Authorization>
</Authorizations>
<SpeedLimits DlType="1" DlLimit="10" ServerDlLimitBypass="0" UlType="1" UlLimit="10" ServerUlLimitBypass="0">
<Download />
<Upload />
</SpeedLimits>
</Containers>
<Containers Name="DMPL(9990)-D">
<Option Name="Max Limit Allowed">0</Option>
<Option Name="Limit Allowed">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="Activate">1</Option>
<Option Name="Feedback">
</Option>
<Option Name="ForceSsl">0</Option>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations>
<Authorization path="E:\BookMaster\RootFolder\MST">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">0</Option>
<Option Name="FileDelete">0</Option>
<Option Name="FileAppend">0</Option>
<Option Name="pathCreate">0</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">1</Option>
<Option Name="AutoCreate">0</Option>
</Authorization>
<Authorization path="E:\BookMaster\RootFolder\MST\NT\Report_9990">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">1</Option>
<Option Name="FileDelete">1</Option>
<Option Name="FileAppend">1</Option>
<Option Name="pathCreate">0</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">0</Option>
<Option Name="AutoCreate">0</Option>
</Authorization>
</Authorizations>
<SpeedLimits DlType="1" DlLimit="10" ServerDlLimitBypass="0" UlType="1" UlLimit="10" ServerUlLimitBypass="0">
<Download />
<Upload />
</SpeedLimits>
</Containers>
</Container>
<Users>
<User Name="leonardo">
<Option Name="Pass">05538eb07b4dbf66b1da4ea9374ac8f5</Option>
<Option Name="Containers">MasterVal</Option>
<Option Name="Max Limit Allowed">2</Option>
<Option Name="Limit Allowed">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="Activate">1</Option>
<Option Name="Feedback">
</Option>
<Option Name="ForceSsl">2</Option>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations />
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="2" UlType="0" UlLimit="10" ServerUlLimitBypass="2">
<Download />
<Upload />
</SpeedLimits>
</User>
<User Name="repturet">
<Option Name="Pass">e076fc6c4c8a245beec106646719f738</Option>
<Option Name="Containers">
</Option>
<Option Name="Max Limit Allowed">0</Option>
<Option Name="Limit Allowed">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="Activate">1</Option>
<Option Name="Feedback">
</Option>
<Option Name="ForceSsl">0</Option>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations>
<Authorization path="E:\BookMaster\RootFolder\MST\NT">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">1</Option>
<Option Name="FileDelete">0</Option>
<Option Name="FileAppend">1</Option>
<Option Name="pathCreate">0</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">0</Option>
<Option Name="AutoCreate">0</Option>
</Authorization>
<Authorization path="E:\BookMaster\RootFolder\MST">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">0</Option>
<Option Name="FileDelete">0</Option>
<Option Name="FileAppend">0</Option>
<Option Name="pathCreate">0</Option>
<Option Name="pathDelete">0</Option>
<Option Name="pathList">1</Option>
<Option Name="pathSubdirs">1</Option>
<Option Name="IsHome">1</Option>
<Option Name="AutoCreate">0</Option>
</Authorization>
</Authorizations>
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="0" UlType="0" UlLimit="10" ServerUlLimitBypass="0">
<Download />
<Upload />
</SpeedLimits>
</User>
<User Name="Anuj">
<Option Name="Pass">270a8eaf5877a08bc69bdde947f8e8b6</Option>
<Option Name="Containers">DMPL(9990)-D</Option>
<Option Name="Max Limit Allowed">2</Option>
<Option Name="Limit Allowed">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="Activate">1</Option>
<Option Name="Feedback">
</Option>
<Option Name="ForceSsl">2</Option>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations />
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="2" UlType="0" UlLimit="10" ServerUlLimitBypass="2">
<Download />
<Upload />
</SpeedLimits>
</User>
<User Name="pallavi">
<Option Name="Pass">8d644bb6af9f1822de1d668d0ea6c36a</Option>
<Option Name="Containers">MasterVal</Option>
<Option Name="Max Limit Allowed">2</Option>
<Option Name="Limit Allowed">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="Activate">1</Option>
<Option Name="Feedback">
</Option>
<Option Name="ForceSsl">2</Option>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations />
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="2" UlType="0" UlLimit="10" ServerUlLimitBypass="2">
<Download />
<Upload />
</SpeedLimits>
</User>
<User Name="piyush">
<Option Name="Pass">db699a999e4a6c8bd867720850d3007c</Option>
<Option Name="Containers">MasterVal</Option>
<Option Name="Max Limit Allowed">2</Option>
<Option Name="Limit Allowed">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="Activate">1</Option>
<Option Name="Feedback">
</Option>
<Option Name="ForceSsl">2</Option>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations />
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="2" UlType="0" UlLimit="10" ServerUlLimitBypass="2">
<Download />
<Upload />
</SpeedLimits>
</User>
<User Name="Richard">
<Option Name="Pass">b56e0b4ea4962283bee762525c2d490f</Option>
<Option Name="Containers">MasterVal</Option>
<Option Name="Max Limit Allowed">2</Option>
<Option Name="Limit Allowed">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="Activate">1</Option>
<Option Name="Feedback">
</Option>
<Option Name="ForceSsl">2</Option>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations />
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="2" UlType="0" UlLimit="10" ServerUlLimitBypass="2">
<Download />
<Upload />
</SpeedLimits>
</User>
</Users>
答案 0 :(得分:1)
您的主要问题是您需要将选项元素视为数组。我将你的xml片段复制到一个名为test.xml的文件中,这个代码可以工作(我将解释我是如何在每一步中解析xml文件以显示Option作为数组显示的位置):
[xml] $info = Get-Content test.xml #Shorthand for New-Object..Load
#This command isn't necessary but is helpful when looking for an attribute
$info | Format-List *
#From above command saw that $info.User is the base document element
$info.User
#showed Attribute in Option
$info.User.Option
#displayed an array element with Attribute as the 5 (6th cause you start at 0) index
$info.User.Option[5]
#displayed Name and #text attributes with #text = 1
$info.User.Option[5].'#text' = "0"
#updated #text attribute to 0
通过这种方式,您应该能够修改脚本以使用完整的xml文件。
由于您仍有问题,更好的解决方案是调整您的xml,以便您可以作为完整对象访问每个用户,而不是通过属性进行解析:
<User>
<name>Richard</name>
<pass>b56e0b4ea4962283bee762525c2d490f</pass>
<containers>MasterVal</containers>
<maxLimit>2</maxLimit>
<minLimit>0</minLimit>
<userLimit>0</userLimit>
<activate>1</activate>
<feedback></feedback>
<forceSsl>2</forceSsl>
<FileAllowed>
<Disallowed />
<Allowed />
</FileAllowed>
<Authorizations />
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="2" UlType="0"
UlLimit="10" ServerUlLimitBypass="2">
<Download />
<Upload />
</SpeedLimits>
</User>
然后,您可以$info.User.Richard
访问每个用户,并使用$info.User.Richard.activate = 0
更改每个激活标记。如果你不能这样做,你需要使用foreach:
[xml] $info = Get-Content test.xml
foreach ($i in $info.BookStore.Users.User) {
if ($i.Name -eq "Richard") {
foreach ($e in $i.Option) {
if ($e.Name -eq "Activate") {
$e.'#text' = "0"
}
}
}
}
然后将xml写回文件。注意:如果您想稍后简化,请查看select和select-xml的使用
更新:给出xml文件的完整答案