我構建了一個腳本,將檔案從本地 C 驅動器復制到網路驅動器。我在 C:\www\root 中有一個名為“images”或“C:\www\root\images”的檔案夾腳本在“F:\powershell_wwwroot\root_” $TimeStamp”中創建了一個名為“images”的檔案夾目標檔案夾中的“images”目錄為空。腳本將所有內容從 C:\www\root\images\ 檔案夾復制到基本目標檔案夾“F:\powershell_wwwroot\root_” $TimeStamp”。
所以我在 F:\powershell_wwwroot\root_02_12_2021 中看到 C:\www\root\images\ 中的所有內容,但是 F:\powershell_wwwroot\root_02_12_2021\images 是空的。我想在“F:\powershell_wwwroot \root_02_12_2021\images”中查看“C:\www\root\images”中的所有內容
$TimeStamp = get-date -f dd_MM_yyyy
$Destination = "F:\powershell_wwwroot\root_" $TimeStamp
New-Item -ItemType directory -Path $Destination -Force
gci "C:\www\root\" -recurse | %{
$file = $_.Fullname
Write-Host $file
Copy-Item -Path $file -Destination $Destination -Force
}
uj5u.com熱心網友回復:
A ForEach-Loopfor this 不應該被需要,讓我們Copy-Item處理它。以下應該保持檔案夾/檔案層次結構不變。
$TimeStamp = Get-Date -Format dd_MM_yyyy
$Destination = "F:\powershell_wwwroot\root_" $TimeStamp
New-Item -ItemType directory -Path $Destination -Force
$Source = "C:\www\root\"
Copy-Item -LiteralPath $source -Destination $destination -Recurse -Force
uj5u.com熱心網友回復:
您可以使用復制專案或位傳輸。我撰寫了一個使用位傳輸 cmdlet 的腳本。如果你更新我的腳本,創建你家的目標檔案夾干。如果您對我的腳本大喊大叫有任何問題 :o)

Import-Module BitsTransfer
#Get Script Location Path
$ScriptPath = Get-Location
$ScriptPath = $ScriptPath.ToString()
$dirName=[System.IO.Path]::GetDirectoryName($ScriptPath)
#Build Source Server Location Path
#Source Server
$Sourceserver = $env:COMPUTERNAME
#Source Server Folder Path
$Sourcefolderpath = "C:\Temp"
#Switch Colon for Dollar Sign
$Sourcepath = $Sourcefolderpath.Replace(':','$')
#Build Target Folder Path
$ServerSourceFolderpathTest = "\\" $SourceServer "\" $Sourcepath
#Print to screen to view path
#Write-Host $ServerSourceFolderpathTest
#Test Path
$testsourcefolderpath = Test-Path -LiteralPath $ServerSourceFolderpathTest
IF ($testsourcefolderpath -eq $False) {
Write-Host -Fore Red "Source Folder Path Cannot Be Established.
Path is - $testsourcefolderpath"
Write-Host "Script Will Exit"
Exit
} ELSE {
Write-Host -Fore Green "Source Folder Path Established.
Path Is - $testsourcefolderpath"
}
#Build Target Server Location Path
#Target Server
$Targetserver = $env:COMPUTERNAME
#Target Server Folder Path
$Targetfolderpath = "C:\Test"
#Switch Colon for Dollar Sign
$Targetpath = $Targetfolderpath.Replace(':','$')
#Build Target Folder Path
$ServerTargetFolderpathTest = "\\" $TargetServer "\" $Targetpath
#Print to screen to view path
#Write-Host $ServerTargetFolderpathTest
#Test Path
$testTargetfolderpath = Test-Path -LiteralPath $ServerTargetFolderpathTest
IF ($testTargetfolderpath -eq $False) {
Write-Host -Fore Red "Target Folder Path Cannot Be Established.
Path is - $testTargetfolderpath"
Write-Host "Script Will Exit"
Exit
} ELSE {
Write-Host -Fore Green "Target Folder Path Established.
Path Is - $testTargetfolderpath"
}
#Build Source Target Paths for Bit Transfer
$SourceFolder = $ServerSourceFolderpathTest
$PreTargetFolder = $ServerTargetFolderpathTest
$Slash = "\"
$TargetFolder = $PreTargetFolder $Slash
#Test Target Folder Path
#$TargetFolder
#Get All Text Files From Source Folder
$GetFiles = (gci $SourceFolder -Filter *.txt)
$GetFilesFileFullName = $GetFiles.FullName
#Test View All Full File Names
#$GetFilesFileFullName
ForEach($File in $GetFilesFileFullName) {
$Discript = "Backing Up $File from $Sourceserver to $Targetserver -- " (Get-Date)
$Discript
Start-BitsTransfer -Source $File -Destination $TargetFolder -Display $Discript -Priority 'Low' -Asynchronous -WhatIf
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372233.html
標籤:电源外壳
