Windows Server Backupではマウントされている仮想ボリュームはバックアップされない の運用をするための、特定フォルダ内にあるVHDファイルをまとめてattach,detachするスクリプトです。
まとめてattachするスクリプト
<#
.SYNOPSIS
指定されたフォルダにあるVHDファイルを全部attachします。
#>
[CmdletBinding()]
Param(
[string]
#VHDファイルがあるフォルダ
$myPath,
[string]
#処理ログを出力するフォルダ
$logDirPath
)
#script
cd $myPath
$erroractionpreference = "SilentlyContinue"
remove-item (join-path $myPath "VHDAttach.txt")
$erroractionpreference = "Continue"
ls *.vhd | %{
$scriptFilePath = (join-path $myPath "VHDAttach.txt")
"select vdisk file=`"" + $_.fullname + "`"" | Out-File -FilePath $scriptFilePath -Append -Encoding Default
"attach vdisk" | Out-File -FilePath $scriptFilePath -Append -Encoding Default
}
$logFilePath = (Join-Path $logDirPath "VHDAttach.log")
Get-Date >> $logFilePath
diskpart /s (join-path $myPath "VHDAttach.txt") >> $logFilePath
まとめてdetachするスクリプト
<#
.SYNOPSIS
指定されたフォルダにあるVHDファイルを全部detachします。
#>
[CmdletBinding()]
Param(
[string]
#VHDファイルがあるフォルダ
$myPath,
[string]
#処理ログを出力するフォルダ
$logDirPath
)
#script
cd $myPath
$erroractionpreference = "SilentlyContinue"
remove-item (join-path $myPath "VHDDetach.txt")
$erroractionpreference = "Continue"
ls *.vhd | %{
$scriptFilePath = (join-path $myPath "VHDDetach.txt")
"select vdisk file=`"" + $_.fullname + "`"" | Out-File -FilePath $scriptFilePath -Append -Encoding Default
"detach vdisk" | Out-File -FilePath $scriptFilePath -Append -Encoding Default
}
$logFilePath = (Join-Path $logDirPath "VHDDetach.log")
Get-Date >> $logFilePath
diskpart /s (join-path $myPath "VHDDetach.txt") >> $logFilePath
オプション指定:
-myPath … VHDファイルがあるフォルダを指定します。
-logDirPath … 動作ログを出力するフォルダを指定します。