0

Here is my situation. I have an XML that resides in a path that contains 260+ characters. One of the folders in this path will always be random, another one is based on versions so it could be different as well.

I am trying to load the XML, make changes to it, and save it back. So far I haven't been able to do that in place so I am copying it to C:\Windows\Temp, making the changes there, and then trying to copy it back in place, which has worked in manual tests.

My main problem is the file path contains more than 260+ characters so Copy-Item hasn't worked properly and I haven't been able to figure out how to get Robocopy to work since I have to use a variable for the path because of the random folders.

XML modification works, copying out to Temp works, the problem I am having is copying it back to the 260+ character path.

I am very new to Powershell as an FYI. Here is what I have come up with so far:

$folderLocation = Get-ChildItem -Path C:\Windows\System32\config\systemprofile\AppData\Local\ProgramA\ -Filter userConfig.xml -Recurse | 
    Sort-Object LastWriteTime | 
    Select-Object -last 1 | 
    Select-Object Directory | 
    Format-Table -hide

foreach ($f in $folderLocation){
    Copy-Item userConfig.xml C:\Windows\Temp
}


$myXML = New-Object System.Xml.XmlDocument
$myXML.Load("C:\Windows\Temp\userConfig.xml")

$isActive = $myXML.SelectSingleNode("//setting[@name = 'Active']")
$isActive.value = "True"

$myXML.Save("C:\Windows\Temp\userConfig.xml")

$convertFolder = [System.String]$folderLocation

Copy-Item ("C:\Windows\Temp\userConfig.xml") $convertFolder

Here is the Robocopy code I tried, I know it's a hackjob but I tried to imitate some things I saw online:

function copy-stuff
{
param([string]$source = "C:\Windows\Temp",
[string]$destination = $convertFolder,
[string]$options = “/R:0″,”/W:0″,”/COPY:DAT”)
[string]$file = "userConfig.xml"

robocopy $source $destination $file $options

}

I tried a couple different things with robocopy but this is what I still had saved. I haven't studied up on creating and using functions in Powershell so this was a monkey see monkey do attempt.

4

1 回答 1

0

解决代码的 Robocopy 部分:

看起来您没有将任何内容传递给您的函数,而是依赖于默认值。尝试这个:

function Copy-Stuff {
    param (
        [string]$source,
        [string]$destination,
        [string]$file
    )

    robocopy $source $destination $file /R:0 /W:0 /COPY:DAT
}

$convertFolder = "blahblah"
Copy-Stuff -Source "C:\Windows\Temp" -Destination $convertFolder -File "userConfig.xml"

如果选项永远不会改变,那么不要费心把它们变成一个变量。

于 2014-07-18T15:31:31.257 回答