簡體   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