xml - 用于创建本地用户并将详细信息写入 XML 文件的 Powershell 脚本

标签 xml windows powershell

我创建了一个小脚本,用于创建 Windows 本地用户及其 FTP 访问环境。该脚本运行良好,但它应该将详细信息写入 XML 文件。该脚本一次只创建一个用户。它将用户名存储在名为$accountName 的变量中,全名存储在$fullName 中,生成的密码存储在$password 中,主目录存储在$directoryPath 和变量 $date 中的创建日期。 下面的代码有效,但它每次都会用上次创建的用户的详细信息覆盖文件,而不会将其附加到 XML 文件。

#Writing out details to XML file
Write-Host -fore yellow "Writing out details to XML..."

# create a template XML to hold data
$template = @'
<FtpUsers>
    <account>
        <accountName></accountName>
        <fullName></fullName>
        <password></password>
        <directoryPath></directoryPath>
        <date></date>
    </account>
</FtpUsers>
'@

$template | Out-File C:\ProgramData\InstanceLogFiles\FtpUsers.xml -encoding UTF8

# load template into XML object
$xml = New-Object xml
$xml.Load("C:\ProgramData\InstanceLogFiles\FtpUsers.xml")

# grab template user
$newuser = (@($xml.FtpUsers.account)[0]).Clone()

# use template to add local user accounts to xml 
$newuser = $newuser.clone()
$newuser.accountName = $accountName
$newuser.fullName = $fullName
$newuser.password = $password$newuser.directoryPath = $directoryPath
$newuser.date = $date
$xml.FtpUsers.AppendChild($newuser) > $null

# remove users with undefined name (remove template)
$xml.FtpUsers.account | 
Where-Object { $_.accountName -eq "" } | 
ForEach-Object  { [void]$xml.FtpUsers.RemoveChild($_) }

# save xml to file
$xml.Save("C:\ProgramData\InstanceLogFiles\FtpUsers.xml")
Write-Host -fore yellow "...Done!"

我看到“$template...”部分每次都会覆盖文件,但是:

-我试图将“使用模板...”部分放入 for 循环但没有成功。

-我试图将模板写入一个单独的文件,但如果它不存在,脚本会报错。

有人能帮我解决问题出在哪里吗?我应该如何正确编码? 提前致谢!

最佳答案

你可以尝试这样的事情:

根据 c:\temp\ftpusers.xml 包含的事实:

<?xml version="1.0" encoding="utf-8"?>
<FtpUsers>
</FtpUsers>

以下代码(您必须用您的变量替换硬编码字符串并围绕它创建一个函数):

# Open Document
[xml]$xmlDoc = [xml](get-content "C:\temp\ftpusers.xml")

# Get our root node
$root = Select-Xml -Xml $xmlDoc -XPath "FtpUsers"

# Creation of a node and its text
$xmlElt = $xmlDoc.CreateElement("account")

# Creation of a sub node
$xmlSubElt1 = $xmlDoc.CreateElement("accountName")
$xmlSubText1 = $xmlDoc.CreateTextNode("user1")
$xmlSubElt1.AppendChild($xmlSubText1) | Out-Null
$xmlElt.AppendChild($xmlSubElt1) | Out-Null

$xmlSubElt2 = $xmlDoc.CreateElement("fullName")
$xmlSubText2 = $xmlDoc.CreateTextNode("Fullname user1")
$xmlSubElt2.AppendChild($xmlSubText2) | Out-Null
$xmlElt.AppendChild($xmlSubElt2) | Out-Null

$xmlSubElt3 = $xmlDoc.CreateElement("password")
$xmlSubText3 = $xmlDoc.CreateTextNode("password user1")
$xmlSubElt3.AppendChild($xmlSubText3) | Out-Null
$xmlElt.AppendChild($xmlSubElt3) | Out-Null

$xmlSubElt4 = $xmlDoc.CreateElement("directoryPath")
$xmlSubText4 = $xmlDoc.CreateTextNode("directoryPath user1")
$xmlSubElt4.AppendChild($xmlSubText4) | Out-Null
$xmlElt.AppendChild($xmlSubElt4) | Out-Null

$xmlSubElt5 = $xmlDoc.CreateElement("fullName")
$xmlSubText5 = $xmlDoc.CreateTextNode("12/05/2014")
$xmlSubElt5.AppendChild($xmlSubText5) | Out-Null
$xmlElt.AppendChild($xmlSubElt5) | Out-Null

# Add the node to the document
$root.Node.AppendChild($xmlElt) | Out-Null

# Store to a file 
$xmlDoc.Save("C:\temp\ftpusers.xml")

给出:

<?xml version="1.0" encoding="utf-8"?>
<FtpUsers>
  <account>
    <accountName>user1</accountName>
    <fullName>Fullname user1</fullName>
    <password>password user1</password>
    <directoryPath>directoryPath user1</directoryPath>
    <fullName>12/05/2014</fullName>
  </account>
</FtpUsers>

关于xml - 用于创建本地用户并将详细信息写入 XML 文件的 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23493176/

相关文章:

windows - 使用联合用户登录 Windows 设备

windows - 在 windows 上使用 zlib 1.2.8 编译时出错

c++ - 如何在 Windows 中以编程方式从调用堆栈帧中读取函数参数?

powershell - Azure 如何使用任何 CLI/API 接收可用性集中虚拟机的分配容错域 ID?

sql-server - 如何使用powershell将用户添加到SQL Server角色?

java - 使用java更改xml标签的值

c++ - 在 C++ 中使用 xerces 的 XML SAX 解析器

android - 如何使图像完整作为背景图像?

c# - XDocument 的简单搜索

powershell 写调试空行