【问题标题】:Powershell script for creating local users and write details to XML file用于创建本地用户并将详细信息写入 XML 文件的 Powershell 脚本
【发布时间】:2014-06-22 23:26:45
【问题描述】:

我创建了一个小脚本,用于创建 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 循环中,但没有成功。

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

有人可以帮我找出问题出在哪里,我应该如何正确编码? 提前谢谢!

【问题讨论】:

    标签: xml windows powershell


    【解决方案1】:

    你可以试试这样的:

    根据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>
    

    【讨论】:

    • 这是一个优雅而有效的解决方案,非常感谢 JPBlanc!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2020-12-19
    相关资源
    最近更新 更多