你好不知道这里有什么问题...我需要这个脚本为robocopy日志创建一个文件夹,然后创建robocopy txt文件。工作正常,但它不会创建\日志文件夹...请帮忙吗?
$sourceFolder = "C:\x\y\z"
$destFolder = "C:\x\y\zz"
{
{
New-Item -ItemType Directory -Force -Path "$DestFolder\Log"
}
foreach ($folder in (Get-ChildItem -Directory $sourceFolder)) {
"robocopy `"$($sourceFolder)\$($folder)\zArchive\Data files to Nov 13`" `"$($destFolder)\$($folder)\folder`" /E /MOVE /DCOPY:T /log+:`"$DestFolder\Log\log.olog`"" | Out-File zArchiveMove.txt -Append
"robocopy `"$($sourceFolder)\$($folder)\Data files to Nov 13`" `"$($destFolder)\$($folder)\folder`" /E /MOVE /DCOPY:T /log+:`"$DestFolder\Log\log.olog`"" | Out-File zArchiveMove2.txt -Append
}
}
答案 0 :(得分:0)
为什么它用大括号括起来?我不能说我对Powershell有很多了解,但只是在测试窗口中重复你的代码就会让我回想起来。所以,如果我写:
{{new-item -itemtype Directory -Force -Path "$DestFolder\Log"}}
它会回响。将会:
{new-item -itemtype Directory -Force -Path" $ DestFolder \ Log"}
如果我抛弃花括号,它就可以了。 所以我建议你摆脱大括号,除非他们需要什么东西。如果他们在与条件陈述相关的代码块中,请在此复制。
所以:
$sourceFolder = "C:\x\y\z"
$destFolder = "C:\x\y\zz"
New-Item -ItemType Directory -Force -Path "$DestFolder\Log"
foreach ($folder in (Get-ChildItem -Directory $sourceFolder)) {
"robocopy `"$($sourceFolder)\$($folder)\zArchive\Data files to Nov 13`" `"$($destFolder)\$($folder)\folder`" /E /MOVE /DCOPY:T /log+:`"$DestFolder\Log\log.olog`"" | Out-File zArchiveMove.txt -Append
"robocopy `"$($sourceFolder)\$($folder)\Data files to Nov 13`" `"$($destFolder)\$($folder)\folder`" /E /MOVE /DCOPY:T /log+:`"$DestFolder\Log\log.olog`"" | Out-File zArchiveMove2.txt -Append
}
应该有效。请测试并报告。另请考虑缩进初始代码。我知道Powershell用户不会经常这样做,但是当你嵌套代码块时,它就更容易阅读。