Removing Child In XML Via PowerShell?
I've Searched all the place to find an answer to this, but I can't seem to find one that works for me and yes I am aware that this has been asked a lot but none work.
This is my code:
$Request = Invoke-RestMethod ($BaseURL + $StatPage) -WebSession $Session -Method Post -Body $StatBody
$Request = $Request | Where-Object {$_.'mac' -ne "00:00:00:00:00:00"}
Instead of removing the node it just prints it out, I've even tried:
foreach($Node in $Request.'ajax-response'.response.'acl-list'.acl.deny)
{
if($Node.'mac' -eq "00:00:00:00:00:00")
{
$Node.ParentNode.RemoveNode($Node)
}
}
Which yields the good old error: Collection was of a fixed size.
.
I've tried a few others but no longer have the code, the MAC is valid and is in the list and have validated this via saving it and checking.
Here is some example XML
I need to use XML as the API only supports that, I've really hit a brick wall and would like some help :)
.net xml powershell
add a comment |
I've Searched all the place to find an answer to this, but I can't seem to find one that works for me and yes I am aware that this has been asked a lot but none work.
This is my code:
$Request = Invoke-RestMethod ($BaseURL + $StatPage) -WebSession $Session -Method Post -Body $StatBody
$Request = $Request | Where-Object {$_.'mac' -ne "00:00:00:00:00:00"}
Instead of removing the node it just prints it out, I've even tried:
foreach($Node in $Request.'ajax-response'.response.'acl-list'.acl.deny)
{
if($Node.'mac' -eq "00:00:00:00:00:00")
{
$Node.ParentNode.RemoveNode($Node)
}
}
Which yields the good old error: Collection was of a fixed size.
.
I've tried a few others but no longer have the code, the MAC is valid and is in the list and have validated this via saving it and checking.
Here is some example XML
I need to use XML as the API only supports that, I've really hit a brick wall and would like some help :)
.net xml powershell
What is the type of$Request
.$Request.GetType()
?
– Prasoon Karunan V
Nov 21 '18 at 6:49
Seems strange that first you filter$Request
to NOT have elements.mac
that have a value of"00:00:00:00:00:00"
and then later on you loop through the request to find an remove exactly those elements (that are not present in the $Request because you have filtered them out..)
– Theo
Nov 21 '18 at 9:34
The document type is Xml Document and I don't control this api and need to keep the structure of the document and can't just take out the mac array
– Jalomba
Nov 21 '18 at 11:14
add a comment |
I've Searched all the place to find an answer to this, but I can't seem to find one that works for me and yes I am aware that this has been asked a lot but none work.
This is my code:
$Request = Invoke-RestMethod ($BaseURL + $StatPage) -WebSession $Session -Method Post -Body $StatBody
$Request = $Request | Where-Object {$_.'mac' -ne "00:00:00:00:00:00"}
Instead of removing the node it just prints it out, I've even tried:
foreach($Node in $Request.'ajax-response'.response.'acl-list'.acl.deny)
{
if($Node.'mac' -eq "00:00:00:00:00:00")
{
$Node.ParentNode.RemoveNode($Node)
}
}
Which yields the good old error: Collection was of a fixed size.
.
I've tried a few others but no longer have the code, the MAC is valid and is in the list and have validated this via saving it and checking.
Here is some example XML
I need to use XML as the API only supports that, I've really hit a brick wall and would like some help :)
.net xml powershell
I've Searched all the place to find an answer to this, but I can't seem to find one that works for me and yes I am aware that this has been asked a lot but none work.
This is my code:
$Request = Invoke-RestMethod ($BaseURL + $StatPage) -WebSession $Session -Method Post -Body $StatBody
$Request = $Request | Where-Object {$_.'mac' -ne "00:00:00:00:00:00"}
Instead of removing the node it just prints it out, I've even tried:
foreach($Node in $Request.'ajax-response'.response.'acl-list'.acl.deny)
{
if($Node.'mac' -eq "00:00:00:00:00:00")
{
$Node.ParentNode.RemoveNode($Node)
}
}
Which yields the good old error: Collection was of a fixed size.
.
I've tried a few others but no longer have the code, the MAC is valid and is in the list and have validated this via saving it and checking.
Here is some example XML
I need to use XML as the API only supports that, I've really hit a brick wall and would like some help :)
.net xml powershell
.net xml powershell
asked Nov 21 '18 at 4:32
JalombaJalomba
154
154
What is the type of$Request
.$Request.GetType()
?
– Prasoon Karunan V
Nov 21 '18 at 6:49
Seems strange that first you filter$Request
to NOT have elements.mac
that have a value of"00:00:00:00:00:00"
and then later on you loop through the request to find an remove exactly those elements (that are not present in the $Request because you have filtered them out..)
– Theo
Nov 21 '18 at 9:34
The document type is Xml Document and I don't control this api and need to keep the structure of the document and can't just take out the mac array
– Jalomba
Nov 21 '18 at 11:14
add a comment |
What is the type of$Request
.$Request.GetType()
?
– Prasoon Karunan V
Nov 21 '18 at 6:49
Seems strange that first you filter$Request
to NOT have elements.mac
that have a value of"00:00:00:00:00:00"
and then later on you loop through the request to find an remove exactly those elements (that are not present in the $Request because you have filtered them out..)
– Theo
Nov 21 '18 at 9:34
The document type is Xml Document and I don't control this api and need to keep the structure of the document and can't just take out the mac array
– Jalomba
Nov 21 '18 at 11:14
What is the type of
$Request
. $Request.GetType()
?– Prasoon Karunan V
Nov 21 '18 at 6:49
What is the type of
$Request
. $Request.GetType()
?– Prasoon Karunan V
Nov 21 '18 at 6:49
Seems strange that first you filter
$Request
to NOT have elements .mac
that have a value of "00:00:00:00:00:00"
and then later on you loop through the request to find an remove exactly those elements (that are not present in the $Request because you have filtered them out..)– Theo
Nov 21 '18 at 9:34
Seems strange that first you filter
$Request
to NOT have elements .mac
that have a value of "00:00:00:00:00:00"
and then later on you loop through the request to find an remove exactly those elements (that are not present in the $Request because you have filtered them out..)– Theo
Nov 21 '18 at 9:34
The document type is Xml Document and I don't control this api and need to keep the structure of the document and can't just take out the mac array
– Jalomba
Nov 21 '18 at 11:14
The document type is Xml Document and I don't control this api and need to keep the structure of the document and can't just take out the mac array
– Jalomba
Nov 21 '18 at 11:14
add a comment |
3 Answers
3
active
oldest
votes
Having the error Collection was of a fixed size.
suggests you may be working with collectionsarrays instead of an XML Element.
You could explicitly cast the Invoke-RestMethod
output to an XmlDocument:
[xml] $Request = Invoke-RestMethod ($BaseURL + $StatPage) -WebSession $Session -Method Post -Body $StatBody
And calling the method RemoveChild()
on the nodes you want to remove:
foreach($Node in $Request.'ajax-response'.response.'acl-list'.acl.deny)
{
if($Node.'mac' -eq "00:00:00:00:00:00")
{
$Node.ParentNode.RemoveChild($Node)
}
}
If you are not interested in the output that RemoveChild()
gives, you can pipe it to null:
$Node.ParentNode.RemoveChild($Node) | Out-Null
This works maginally, however when I output the $Request after the RemoveChild it keeps the supposedly removed element.
– Jalomba
Nov 25 '18 at 21:11
Ah, I think I got it after calling .Save("") to save to a file, ideally I'd like to do this without having to save to a file, but I guess this works well enough :).
– Jalomba
Nov 25 '18 at 21:17
add a comment |
I don't know if this will help you but here's what I did. I used data from your link:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
Then I did the following, just loop through the data and create the new file without the unwanted mac:
[xml]$xml = Get-Content "C:Usersb.moeskopsDesktoptest.xml"
$data = $xml.'ajax-request'.acl.deny
$wrongData = "c0:d3:c0:1c:60:93"
foreach($Node in $data){
if($Node.'mac' -eq $wrongData){
$data = $data | Where-Object {$_.mac -ne $wrongData}
}
}
$data
I hope this helps.
It does! However I need to keep the rest of the xml document intact and not create just a new list
– Jalomba
Nov 21 '18 at 11:13
Do you have any other ideas for me short of doing it this way then just converting to string and padding the rest of the XML around it?
– Jalomba
Nov 22 '18 at 9:45
add a comment |
I might have found something that works...
I used the data from your link again.
$Path = "H:test.xml"
$xml = Get-Content $Path
$array = $xml | ConvertTo-Json | ConvertFrom-Json
foreach($obj in $array.value){
if($obj -match '<deny type="single" mac="c0:bd:d1:14:62:68"/>' -or $obj -match '<deny mac="c0:d3:c0:1c:60:93" type="single"/>'){
$array2 = $array.value | Where-Object {$_ -ne $obj}
}
}
$array2
First the file:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
This file is saved at 'H:test.xml'. After running the script the output for $array2 is:
$array2
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
If you examine the file, you can see that I have removed the content from the file and kept the format intact.
As you can see I removed two lines of the XML file, I did this because there are 2 different formats found inside the file.
Hope this helps!
This isn't as simple as it looks after all, the results of GetType() on the variable holding my XML is XmlDocument, but when I run this code I don't get any output, nor do I get any returned list it seems my powershell wants to make this impossible for me :(
– Jalomba
Nov 25 '18 at 20:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405296%2fremoving-child-in-xml-via-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Having the error Collection was of a fixed size.
suggests you may be working with collectionsarrays instead of an XML Element.
You could explicitly cast the Invoke-RestMethod
output to an XmlDocument:
[xml] $Request = Invoke-RestMethod ($BaseURL + $StatPage) -WebSession $Session -Method Post -Body $StatBody
And calling the method RemoveChild()
on the nodes you want to remove:
foreach($Node in $Request.'ajax-response'.response.'acl-list'.acl.deny)
{
if($Node.'mac' -eq "00:00:00:00:00:00")
{
$Node.ParentNode.RemoveChild($Node)
}
}
If you are not interested in the output that RemoveChild()
gives, you can pipe it to null:
$Node.ParentNode.RemoveChild($Node) | Out-Null
This works maginally, however when I output the $Request after the RemoveChild it keeps the supposedly removed element.
– Jalomba
Nov 25 '18 at 21:11
Ah, I think I got it after calling .Save("") to save to a file, ideally I'd like to do this without having to save to a file, but I guess this works well enough :).
– Jalomba
Nov 25 '18 at 21:17
add a comment |
Having the error Collection was of a fixed size.
suggests you may be working with collectionsarrays instead of an XML Element.
You could explicitly cast the Invoke-RestMethod
output to an XmlDocument:
[xml] $Request = Invoke-RestMethod ($BaseURL + $StatPage) -WebSession $Session -Method Post -Body $StatBody
And calling the method RemoveChild()
on the nodes you want to remove:
foreach($Node in $Request.'ajax-response'.response.'acl-list'.acl.deny)
{
if($Node.'mac' -eq "00:00:00:00:00:00")
{
$Node.ParentNode.RemoveChild($Node)
}
}
If you are not interested in the output that RemoveChild()
gives, you can pipe it to null:
$Node.ParentNode.RemoveChild($Node) | Out-Null
This works maginally, however when I output the $Request after the RemoveChild it keeps the supposedly removed element.
– Jalomba
Nov 25 '18 at 21:11
Ah, I think I got it after calling .Save("") to save to a file, ideally I'd like to do this without having to save to a file, but I guess this works well enough :).
– Jalomba
Nov 25 '18 at 21:17
add a comment |
Having the error Collection was of a fixed size.
suggests you may be working with collectionsarrays instead of an XML Element.
You could explicitly cast the Invoke-RestMethod
output to an XmlDocument:
[xml] $Request = Invoke-RestMethod ($BaseURL + $StatPage) -WebSession $Session -Method Post -Body $StatBody
And calling the method RemoveChild()
on the nodes you want to remove:
foreach($Node in $Request.'ajax-response'.response.'acl-list'.acl.deny)
{
if($Node.'mac' -eq "00:00:00:00:00:00")
{
$Node.ParentNode.RemoveChild($Node)
}
}
If you are not interested in the output that RemoveChild()
gives, you can pipe it to null:
$Node.ParentNode.RemoveChild($Node) | Out-Null
Having the error Collection was of a fixed size.
suggests you may be working with collectionsarrays instead of an XML Element.
You could explicitly cast the Invoke-RestMethod
output to an XmlDocument:
[xml] $Request = Invoke-RestMethod ($BaseURL + $StatPage) -WebSession $Session -Method Post -Body $StatBody
And calling the method RemoveChild()
on the nodes you want to remove:
foreach($Node in $Request.'ajax-response'.response.'acl-list'.acl.deny)
{
if($Node.'mac' -eq "00:00:00:00:00:00")
{
$Node.ParentNode.RemoveChild($Node)
}
}
If you are not interested in the output that RemoveChild()
gives, you can pipe it to null:
$Node.ParentNode.RemoveChild($Node) | Out-Null
answered Nov 23 '18 at 11:07
JoeBigToeJoeBigToe
607
607
This works maginally, however when I output the $Request after the RemoveChild it keeps the supposedly removed element.
– Jalomba
Nov 25 '18 at 21:11
Ah, I think I got it after calling .Save("") to save to a file, ideally I'd like to do this without having to save to a file, but I guess this works well enough :).
– Jalomba
Nov 25 '18 at 21:17
add a comment |
This works maginally, however when I output the $Request after the RemoveChild it keeps the supposedly removed element.
– Jalomba
Nov 25 '18 at 21:11
Ah, I think I got it after calling .Save("") to save to a file, ideally I'd like to do this without having to save to a file, but I guess this works well enough :).
– Jalomba
Nov 25 '18 at 21:17
This works maginally, however when I output the $Request after the RemoveChild it keeps the supposedly removed element.
– Jalomba
Nov 25 '18 at 21:11
This works maginally, however when I output the $Request after the RemoveChild it keeps the supposedly removed element.
– Jalomba
Nov 25 '18 at 21:11
Ah, I think I got it after calling .Save("") to save to a file, ideally I'd like to do this without having to save to a file, but I guess this works well enough :).
– Jalomba
Nov 25 '18 at 21:17
Ah, I think I got it after calling .Save("") to save to a file, ideally I'd like to do this without having to save to a file, but I guess this works well enough :).
– Jalomba
Nov 25 '18 at 21:17
add a comment |
I don't know if this will help you but here's what I did. I used data from your link:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
Then I did the following, just loop through the data and create the new file without the unwanted mac:
[xml]$xml = Get-Content "C:Usersb.moeskopsDesktoptest.xml"
$data = $xml.'ajax-request'.acl.deny
$wrongData = "c0:d3:c0:1c:60:93"
foreach($Node in $data){
if($Node.'mac' -eq $wrongData){
$data = $data | Where-Object {$_.mac -ne $wrongData}
}
}
$data
I hope this helps.
It does! However I need to keep the rest of the xml document intact and not create just a new list
– Jalomba
Nov 21 '18 at 11:13
Do you have any other ideas for me short of doing it this way then just converting to string and padding the rest of the XML around it?
– Jalomba
Nov 22 '18 at 9:45
add a comment |
I don't know if this will help you but here's what I did. I used data from your link:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
Then I did the following, just loop through the data and create the new file without the unwanted mac:
[xml]$xml = Get-Content "C:Usersb.moeskopsDesktoptest.xml"
$data = $xml.'ajax-request'.acl.deny
$wrongData = "c0:d3:c0:1c:60:93"
foreach($Node in $data){
if($Node.'mac' -eq $wrongData){
$data = $data | Where-Object {$_.mac -ne $wrongData}
}
}
$data
I hope this helps.
It does! However I need to keep the rest of the xml document intact and not create just a new list
– Jalomba
Nov 21 '18 at 11:13
Do you have any other ideas for me short of doing it this way then just converting to string and padding the rest of the XML around it?
– Jalomba
Nov 22 '18 at 9:45
add a comment |
I don't know if this will help you but here's what I did. I used data from your link:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
Then I did the following, just loop through the data and create the new file without the unwanted mac:
[xml]$xml = Get-Content "C:Usersb.moeskopsDesktoptest.xml"
$data = $xml.'ajax-request'.acl.deny
$wrongData = "c0:d3:c0:1c:60:93"
foreach($Node in $data){
if($Node.'mac' -eq $wrongData){
$data = $data | Where-Object {$_.mac -ne $wrongData}
}
}
$data
I hope this helps.
I don't know if this will help you but here's what I did. I used data from your link:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
Then I did the following, just loop through the data and create the new file without the unwanted mac:
[xml]$xml = Get-Content "C:Usersb.moeskopsDesktoptest.xml"
$data = $xml.'ajax-request'.acl.deny
$wrongData = "c0:d3:c0:1c:60:93"
foreach($Node in $data){
if($Node.'mac' -eq $wrongData){
$data = $data | Where-Object {$_.mac -ne $wrongData}
}
}
$data
I hope this helps.
answered Nov 21 '18 at 9:55
Bernard MoeskopsBernard Moeskops
24316
24316
It does! However I need to keep the rest of the xml document intact and not create just a new list
– Jalomba
Nov 21 '18 at 11:13
Do you have any other ideas for me short of doing it this way then just converting to string and padding the rest of the XML around it?
– Jalomba
Nov 22 '18 at 9:45
add a comment |
It does! However I need to keep the rest of the xml document intact and not create just a new list
– Jalomba
Nov 21 '18 at 11:13
Do you have any other ideas for me short of doing it this way then just converting to string and padding the rest of the XML around it?
– Jalomba
Nov 22 '18 at 9:45
It does! However I need to keep the rest of the xml document intact and not create just a new list
– Jalomba
Nov 21 '18 at 11:13
It does! However I need to keep the rest of the xml document intact and not create just a new list
– Jalomba
Nov 21 '18 at 11:13
Do you have any other ideas for me short of doing it this way then just converting to string and padding the rest of the XML around it?
– Jalomba
Nov 22 '18 at 9:45
Do you have any other ideas for me short of doing it this way then just converting to string and padding the rest of the XML around it?
– Jalomba
Nov 22 '18 at 9:45
add a comment |
I might have found something that works...
I used the data from your link again.
$Path = "H:test.xml"
$xml = Get-Content $Path
$array = $xml | ConvertTo-Json | ConvertFrom-Json
foreach($obj in $array.value){
if($obj -match '<deny type="single" mac="c0:bd:d1:14:62:68"/>' -or $obj -match '<deny mac="c0:d3:c0:1c:60:93" type="single"/>'){
$array2 = $array.value | Where-Object {$_ -ne $obj}
}
}
$array2
First the file:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
This file is saved at 'H:test.xml'. After running the script the output for $array2 is:
$array2
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
If you examine the file, you can see that I have removed the content from the file and kept the format intact.
As you can see I removed two lines of the XML file, I did this because there are 2 different formats found inside the file.
Hope this helps!
This isn't as simple as it looks after all, the results of GetType() on the variable holding my XML is XmlDocument, but when I run this code I don't get any output, nor do I get any returned list it seems my powershell wants to make this impossible for me :(
– Jalomba
Nov 25 '18 at 20:37
add a comment |
I might have found something that works...
I used the data from your link again.
$Path = "H:test.xml"
$xml = Get-Content $Path
$array = $xml | ConvertTo-Json | ConvertFrom-Json
foreach($obj in $array.value){
if($obj -match '<deny type="single" mac="c0:bd:d1:14:62:68"/>' -or $obj -match '<deny mac="c0:d3:c0:1c:60:93" type="single"/>'){
$array2 = $array.value | Where-Object {$_ -ne $obj}
}
}
$array2
First the file:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
This file is saved at 'H:test.xml'. After running the script the output for $array2 is:
$array2
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
If you examine the file, you can see that I have removed the content from the file and kept the format intact.
As you can see I removed two lines of the XML file, I did this because there are 2 different formats found inside the file.
Hope this helps!
This isn't as simple as it looks after all, the results of GetType() on the variable holding my XML is XmlDocument, but when I run this code I don't get any output, nor do I get any returned list it seems my powershell wants to make this impossible for me :(
– Jalomba
Nov 25 '18 at 20:37
add a comment |
I might have found something that works...
I used the data from your link again.
$Path = "H:test.xml"
$xml = Get-Content $Path
$array = $xml | ConvertTo-Json | ConvertFrom-Json
foreach($obj in $array.value){
if($obj -match '<deny type="single" mac="c0:bd:d1:14:62:68"/>' -or $obj -match '<deny mac="c0:d3:c0:1c:60:93" type="single"/>'){
$array2 = $array.value | Where-Object {$_ -ne $obj}
}
}
$array2
First the file:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
This file is saved at 'H:test.xml'. After running the script the output for $array2 is:
$array2
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
If you examine the file, you can see that I have removed the content from the file and kept the format intact.
As you can see I removed two lines of the XML file, I did this because there are 2 different formats found inside the file.
Hope this helps!
I might have found something that works...
I used the data from your link again.
$Path = "H:test.xml"
$xml = Get-Content $Path
$array = $xml | ConvertTo-Json | ConvertFrom-Json
foreach($obj in $array.value){
if($obj -match '<deny type="single" mac="c0:bd:d1:14:62:68"/>' -or $obj -match '<deny mac="c0:d3:c0:1c:60:93" type="single"/>'){
$array2 = $array.value | Where-Object {$_ -ne $obj}
}
}
$array2
First the file:
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny mac="c0:d3:c0:1c:60:93" type="single"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
This file is saved at 'H:test.xml'. After running the script the output for $array2 is:
$array2
<ajax-request action="updobj" comp="acl-list" updater="blocked-clients">
<acl id="1" name="System" description="System" default-mode="allow" EDITABLE="false">
<deny type="single" mac="c0:bd:d1:14:62:68"/>
<deny type="single" mac="b4:74:43:c0:f3:cf"/>
<deny type="single" mac="c0:ee:fb:25:ca:7c"/>
<deny type="single" mac="48:db:50:ed:71:36"/>
<deny mac="d4:50:3f:fd:48:2f" type="single"/>
<deny type="single" mac="58:48:22:c2:44:2e"/>
</acl>
</ajax-request>
If you examine the file, you can see that I have removed the content from the file and kept the format intact.
As you can see I removed two lines of the XML file, I did this because there are 2 different formats found inside the file.
Hope this helps!
edited Nov 23 '18 at 9:56
answered Nov 23 '18 at 9:38
Bernard MoeskopsBernard Moeskops
24316
24316
This isn't as simple as it looks after all, the results of GetType() on the variable holding my XML is XmlDocument, but when I run this code I don't get any output, nor do I get any returned list it seems my powershell wants to make this impossible for me :(
– Jalomba
Nov 25 '18 at 20:37
add a comment |
This isn't as simple as it looks after all, the results of GetType() on the variable holding my XML is XmlDocument, but when I run this code I don't get any output, nor do I get any returned list it seems my powershell wants to make this impossible for me :(
– Jalomba
Nov 25 '18 at 20:37
This isn't as simple as it looks after all, the results of GetType() on the variable holding my XML is XmlDocument, but when I run this code I don't get any output, nor do I get any returned list it seems my powershell wants to make this impossible for me :(
– Jalomba
Nov 25 '18 at 20:37
This isn't as simple as it looks after all, the results of GetType() on the variable holding my XML is XmlDocument, but when I run this code I don't get any output, nor do I get any returned list it seems my powershell wants to make this impossible for me :(
– Jalomba
Nov 25 '18 at 20:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405296%2fremoving-child-in-xml-via-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the type of
$Request
.$Request.GetType()
?– Prasoon Karunan V
Nov 21 '18 at 6:49
Seems strange that first you filter
$Request
to NOT have elements.mac
that have a value of"00:00:00:00:00:00"
and then later on you loop through the request to find an remove exactly those elements (that are not present in the $Request because you have filtered them out..)– Theo
Nov 21 '18 at 9:34
The document type is Xml Document and I don't control this api and need to keep the structure of the document and can't just take out the mac array
– Jalomba
Nov 21 '18 at 11:14