繁体   English   中英

Powershell Copy-Item PS1脚本

[英]Powershell Copy-Item PS1 Script

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

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

我的首选方法:

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

在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}
}

我可以将这样的东西附加到这些东西的末尾吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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