Powershell Copy-Item PS1脚本

时间:2013-11-14 22:36:17

标签: powershell

Function CopyTwolocations ($from, $to)
{
Copy-Item -Path $from $to -Recurse -Force
$?
if($false) {Return}
}

CopyTwolocations -from "C:\Test1\Subtest1\*" -to "\\Testserver\Back-Ups\TEST\%date:~0,3%\"
CopyTwolocations -from "C:\TESTMYSQL\*" -to "\\Testserver\Back-Ups\TEST\%date:~0,3%\"

我遇到了麻烦。我想能够每天制作一个子文件夹,但我所拥有的只有%date:~0.3%\

但我知道powershell会这样做,但我不太确定如何将它复制到你复制它的那一天。

$a = Get-Date
$a.DayOfWeek

3 个答案:

答案 0 :(得分:2)

最直接的方法:-to "\\Testserver\Back-Ups\TEST\$(get-date -format 'dddd')"

我的首选方法:

$Destination = '\\Testserver\Back-Ups\TEST\{0:dddd}' -f (get-date);
... -to "$Destination"

答案 1 :(得分:0)

在PowerShell中有多种方法可以做。另一种选择是:

CopyTwolocations -from "C:\TESTMYSQL\*" -to "\\Testserver\Back-Ups\TEST\$(get-date -f ddd)\"

由于PowerShell允许将位置参数简化为:

CopyTwolocations C:\TESTMYSQL\* "\\Testserver\Back-Ups\TEST\$(get-date -f ddd)\"

修改您的函数以创建目标目录(如果它不存在):

Function CopyTwolocations ($from, $to)
{
    if (!(Test-Path $to)) {
        md $to
    }
    Copy-Item -Path $from $to -Recurse -Force
    $?
   if($false) {Return}
}

答案 2 :(得分:0)

我可以将这样的内容附加到这些内容吗?

CopyTwolocations -from "C:\Test1\Subtest1\*" -to "\\Testserver\Back-Ups\TEST\%date:~0,3%\" -name ("Docs-"+ (Get-Date -format yyyyMMdd)) -type directory
CopyTwolocations -from "C:\TESTMYSQL\*" -to "\\Testserver\Back-Ups\TEST\%date:~0,3%\" -name ("Docs-"+ (Get-Date -format yyyyMMdd)) -type directory