Skip to content

Instantly share code, notes, and snippets.

@iwa4
Last active December 28, 2015 18:09
Show Gist options
  • Select an option

  • Save iwa4/7541539 to your computer and use it in GitHub Desktop.

Select an option

Save iwa4/7541539 to your computer and use it in GitHub Desktop.
PowelShell で UNC パスのフォルダをプログレスバー付きでコピーする
#================================
# Properties
#================================
Properties {
$target_server = "\\" + $target_server
$target_username = $target_server + "\username" # 指定されたログオン セッションは存在しません。
# そのセッションは既に終了している可能性があります。
# の エラー回避のため、domain name を付与してユーザー名を指定する
$target_password = "password"
$target_src = "$target_server\c$\hoge"
$target_dest = "$target_server\c$\fuga"
}
#================================
# Tasks
#================================
task make-Backup {
net use $target_server $target_password /USER:$target_username
try
{
removeItem-if-exist $target_dest
copy-with-progress $target_src $target_dest #Copy-Item でもいいけど、レスポンスが見たいので。
Write-Host "[make-Backup] 正常に処理が終了しました。"
}
catch [System.Exception] {
Write-Host "エラーが発生しました。 $_.Exception.Message" -type Error
exit -1 #Jenkins でエラーにしたいので。
}
finally {
net use $target_server /delete
}
}
#================================
# Functions
#================================
function copy-with-progress($src, $dest) {
$files = Get-ChildItem $src -Recurse
$counter = 1
Foreach($file in $files)
{
$status = "Copy files {0} on {1}: {2}" -f $counter, $files.Count, $file.Name
Write-progress -Activity "Copy data" $status -PercentComplete ($counter / $files.count * 100)
$destpath = $file.fullname.replace($src, $dest)
If($file.PSIsContainer)
{
New-Item $destpath -type directory -Force
}
else
{
Copy-Item $file.fullname $destpath -Force
}
$counter++
}
}
function removeItem-if-exist($targetDir) {
If(Test-Path $targetDir) {
Remove-Item $targetDir -Recurse -Force
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment