-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathAdd data to XML 2.ps1
57 lines (48 loc) · 1.24 KB
/
Add data to XML 2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<#
<?xml version="1.0" encoding="utf-8"?>
<HelpBox>
<Categorys>
<Category Selected="True" Name="Admin Tools"></Category>
<Category Selected="False" Name="Scripts"></Category>
<Category Selected="False" Name="Clipboard"></Category>
</Categorys>
<Actions></Actions>
</HelpBox>
#>
<#
.SYNOPSIS
Write data to an xml file
.EXAMPLE
AddEntryInConfig -Selector "//tutorials[@name='tutorial:start']" -Path "C:\Desktop\test.xml" -NewNode "PowerShell"
.NOTES
https://social.technet.microsoft.com/Forums/scriptcenter/en-US/eb4f4d97-1c6e-41e5-830f-244b50bf722c/append-to-an-xml-file-with-powershell
#>
function AddEntryInConfig
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]
$Selector,
[Parameter(Mandatory = $true)]
[string]
$Path,
[Parameter(Mandatory = $true)]
[string]
$Fragment
)
[xml]$xml = Get-Content -Path $Path
$xmlFrag = $xml.CreateDocumentFragment()
$xmlFrag.InnerXml = $Fragment
$node = $xml.SelectSingleNode($Selector)
$node.AppendChild($xmlFrag)
$xml.Save($Path)
}
$Fragment = @'
<Item Name="CMD" Icon="27" TaskType="Launch" Category="Scripts">
<File>C:\Windows\System32\cmd.exe</File>
<Arg></Arg>
</Item>
'@
AddEntryInConfig -Selector "//Actions" -Path "C:\Desktop\test.xml" -Fragment $Fragment