简体   繁体   中英

How to generate multiple files and then move created files into individual folders

I am trying to generate a PowerShell script that will either take a file from C:\*FOLDER*\users\ or just create a file that will have an individual information in it. Here is a snip of contents. The DB name is the item that will need to be changed.

<?xml version="1.0" encoding="UTF-8"?>
<Connections>
    <Connection>
        <Server>*SERVERNAME\INSTANCE*</Server>
        <db>rduser1</db>
    </Connection>
</Connections>

The second part would be to take the generated file and move it to C:\*FOLDER*\Users\rduser* but to rduser 1-350. The script below is what I started on to get the file to move but unsure if it will work with a new file generated. Any help is appreciated!!

$source="C:\rfms\users\dbconnect.xml"
$target="C:\rfms\users\%username%"

foreach ($directory in $(get-childitem $target).Name)  
{ 
  $targetpath= join-path -path $target -childpath $directory 
  copy-item -path $source -Destination $targetpath 
}

I think I got it now..

$DirectoryNames=get-childitem "C:\users" -Name

foreach($directory in $DirectoryNames){

Set The Formatting

$xmlsettings = New-Object System.Xml.XmlWriterSettings

$xmlsettings.Indent = $true

$xmlsettings.IndentChars = " "

Set the File Name Create The Document and move file

$rduser="C:\users" + $directory + "\dbconnect.xml"

$XmlWriter = [System.XML.XmlWriter]::Create($rduser, $xmlsettings)

Start the XML Generation

$xmlWriter.WriteStartElement("Connections")

$xmlWriter.WriteStartElement("Connection") # <-- Start <Object>

    $xmlWriter.WriteElementString("Server","OC-RFMS-SQL")

    $xmlWriter.WriteElementString("db",$directory)

$xmlWriter.WriteEndElement() # <-- End <Object>

$xmlWriter.WriteEndElement() # <-- End

End, Finalize and close the XML Document

$xmlWriter.WriteEndDocument()

$xmlWriter.Flush()

$xmlWriter.Close()

} '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM