特定フォルダのVHDファイルをまとめてattach,detachするスクリプト

Windows Server Backupではマウントされている仮想ボリュームはバックアップされない の運用をするための、特定フォルダ内にあるVHDファイルをまとめてattach,detachするスクリプトです。

まとめてattachするスクリプト

1<#
2    .SYNOPSIS
3    指定されたフォルダにあるVHDファイルを全部attachします。
4#>
5 
6[CmdletBinding()]
7Param(
8    [string]
9    #VHDファイルがあるフォルダ
10    $myPath,
11    [string]
12    #処理ログを出力するフォルダ
13    $logDirPath
14)
15 
16#script
17cd $myPath
18$erroractionpreference = "SilentlyContinue"
19remove-item (join-path $myPath "VHDAttach.txt")
20$erroractionpreference = "Continue"
21ls *.vhd | %{
22    $scriptFilePath = (join-path $myPath "VHDAttach.txt")
23    "select vdisk file=`"" + $_.fullname + "`"" | Out-File -FilePath $scriptFilePath -Append -Encoding Default
24    "attach vdisk" | Out-File -FilePath $scriptFilePath -Append -Encoding Default
25}
26 
27$logFilePath = (Join-Path $logDirPath "VHDAttach.log")
28Get-Date >> $logFilePath
29diskpart /s (join-path $myPath "VHDAttach.txt") >> $logFilePath

まとめてdetachするスクリプト

1<#
2    .SYNOPSIS
3    指定されたフォルダにあるVHDファイルを全部detachします。
4#>
5 
6[CmdletBinding()]
7Param(
8    [string]
9    #VHDファイルがあるフォルダ
10    $myPath,
11    [string]
12    #処理ログを出力するフォルダ
13    $logDirPath
14)
15 
16 
17#script
18cd $myPath
19$erroractionpreference = "SilentlyContinue"
20remove-item (join-path $myPath "VHDDetach.txt")
21$erroractionpreference = "Continue"
22 
23ls *.vhd | %{
24    $scriptFilePath = (join-path $myPath "VHDDetach.txt")
25    "select vdisk file=`"" + $_.fullname + "`"" | Out-File -FilePath $scriptFilePath -Append -Encoding Default
26    "detach vdisk" | Out-File -FilePath $scriptFilePath -Append -Encoding Default
27}
28 
29$logFilePath = (Join-Path $logDirPath "VHDDetach.log")
30Get-Date >> $logFilePath
31diskpart /s (join-path $myPath "VHDDetach.txt") >> $logFilePath

オプション指定:
-myPath … VHDファイルがあるフォルダを指定します。
-logDirPath … 動作ログを出力するフォルダを指定します。

コメントを残す