[英]Store encrypted password in xml
我將用戶名和密碼存儲在.xml文件中,密碼已加密。 當我嘗試添加其他用戶名和密碼時,它將覆蓋第一個。 如何追加現有文件?
$username = "user"
$password = "password1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username,($password | ConvertTo-SecureString)
$cred | Export-Clixml C:\file.xml
我很難理解所提供的答案。 我嘗試了幾種不同的值,應該使用什么代替Node?
<Objs Version="1.1.0.1"xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Management.Automation.PSCredential</T>
<T>System.Object</T>
</TN>
<ToString>System.Management.Automation.PSCredential</ToString>
<Props>
<S N="UserName">Tom</S>
<SS N="Password">01000000d08c9ddf0115d1118c7a00c04fc297eb01000000fbc3e29e463de248a4643396e70cd6670000000002000000000003660000c0000000100000005f32bf40a436abd21b2855a28a7930f10000000004800000a000000010000000f4d23943a843968368a273547091233b1800000061921de936aa6f7952ef38c53de64f22dcad44e75836a01514000000d50a8b31c7c3a242e82f3c8c1c1105f1431f2a01</SS>
</Props>
</Obj>
</Objs>
您的代碼在每次執行時都會覆蓋XML文件。
要修改XML文件中的特定元素,您可以檢索它並將其轉換為[xml]
數據類型,使用良好的點號表示法修改其屬性,然后再次導出文件。
例如:
[xml]$doc = get-content C:\file.xml
$doc.Node = "new value" # where "Node" is an element within your XML structure
$doc.OuterXml > C:\file.xml
同樣,您可以根據需要向XML文檔添加新節點。
[xml]$doc = "<Nodes><Node>1</Node></Nodes>"
$newNode = $doc.CreateElement("Node")
$newNode.InnerXml = 2
$doc.Nodes.AppendChild($newNode)
# $doc.OuterXml is now <Nodes><Node>1</Node><Node>2</Node></Nodes>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.