2016-11-25 13 views
0

所以我基本上試圖檢索截圖中的信息,但與PowerShell。在第一個障礙難住。如何枚舉給定文件或文件夾的卷影副本?

enter image description here

最好的,我能找到的是https://superuser.com/questions/643536/how-to-find-and-open-previous-versions-of-a-folder-programmatically-using-power

但它不是真的適合。

已經閱讀了上https://msdn.microsoft.com/en-us/library/aa393625(v=vs.85).aspx

但我不能夠做出的量有多大意義。這是正確的道路嗎?

+0

'獲取-WmiObject可以Win32_ShadowCopy'? –

回答

0

PowerShell中有幾個步驟可用於瀏覽卷影副本。 首先,下面的代碼會顯示驅動器列表和它們的卷影副本

$shadowStorageList = @(); 
$volumeList = Get-WmiObject Win32_Volume -Property SystemName,DriveLetter,DeviceID,Capacity,FreeSpace -Filter "DriveType=3" | select @{n="DriveLetter";e={$_.DriveLetter.ToUpper()}},DeviceID,@{n="CapacityGB";e={([math]::Round([int64]($_.Capacity)/1GB,2))}},@{n="FreeSpaceGB";e={([math]::Round([int64]($_.FreeSpace)/1GB,2))}} | Sort DriveLetter; 
$shadowStorages = gwmi Win32_ShadowStorage -Property AllocatedSpace,DiffVolume,MaxSpace,UsedSpace,Volume | 
       Select @{n="Volume";e={$_.Volume.Replace("\\","\").Replace("Win32_Volume.DeviceID=","").Replace("`"","")}}, 
       @{n="DiffVolume";e={$_.DiffVolume.Replace("\\","\").Replace("Win32_Volume.DeviceID=","").Replace("`"","")}}, 
       @{n="AllocatedSpaceGB";e={([math]::Round([int64]($_.AllocatedSpace)/1GB,2))}}, 
       @{n="MaxSpaceGB";e={([math]::Round([int64]($_.MaxSpace)/1GB,2))}}, 
       @{n="UsedSpaceGB";e={([math]::Round([int64]($_.UsedSpace)/1GB,2))}} 

# Create an array of Customer PSobject 
foreach($shStorage in $shadowStorages) { 
    $tmpDriveLetter = ""; 
    foreach($volume in $volumeList) { 
     if($shStorage.DiffVolume -eq $volume.DeviceID) { 
      $tmpDriveLetter = $volume.DriveLetter; 
     } 
    } 
    $objVolume = New-Object PSObject -Property @{ 
     Volume = $shStorage.Volume 
     AllocatedSpaceGB = $shStorage.AllocatedSpaceGB 
     UsedSpaceGB = $shStorage.UsedSpaceGB 
     MaxSpaceGB = $shStorage.MaxSpaceGB 
     DriveLetter = $tmpDriveLetter 
    } 
    $shadowStorageList += $objVolume; 
} 


for($i = 0; $i -lt $shadowStorageList.Count; $i++){ 
    $objCopyList = Get-WmiObject Win32_ShadowCopy | Where-Object {$_.VolumeName -eq $shadowStorageList[$i].Volume} | select DeviceObject, InstallDate 
    $shadowStorageList[$i] | add-member Noteproperty shadowcopies $objCopyList 
    $shadowStorageList[$i] 
} 

輸出示例:

AllocatedSpaceGB:9.17驅動器號:F:卷: \ \ {卷6c974bfe-0525- 11e7-80bf-0050568007f5} \ MaxSpaceGB: 15 UsedSpaceGB:8.46 shadowcopies: {@ {DeviceObject = \?\ GLOBALROOT \ Device \ HarddiskVolumeShadowCopy39; InstallDate = 20170902070009.648986 + 600},@ {DeviceObject = \?\ GLOBALROOT \ Device \ HarddiskVolumeShadowCopy40; InstallDate = 20170903070009.902376 + 600},@ {DeviceObject = \?\ GLOBALROOT \ Device \ HarddiskVolumeShadowCopy41; InstallDate = 20170904070016.340573 + 600},@ {DeviceObject = \?\ GLOBALROOT \ Device \ HarddiskVolumeShadowCopy42; InstallDate = 20170904120031.644419 + 600} ...}

AllocatedSpaceGB:6.28驅動器號:C:體積:? \ \卷{4c22f9da-2b50-11e6-80b3-806e6f6e6963} \ MaxSpaceGB: 6.96 UsedSpaceGB:5.78 shadowcopies: {@ {設備對象= \ \ GLOBALROOT \設備\ HarddiskVolumeShadowCopy3?; InstallDate = 20170921070020.298687 + 600}, @ {DeviceObject = \?\ GLOBALROOT \ Device \ HarddiskVolumeShadowCopy4; InstallDate = 20170921120026.126738 + 600}, @ {DeviceObject = \?\ GLOBALROOT \ Device \ HarddiskVolumeShadowCopy5; InstallDate = 20170922070025.309517 + 600}, @ {DeviceObject = \?\ GLOBALROOT \ Device \ HarddiskVolumeShadowCopy6; InstallDate = 20170922120004.852824 + 600} ...}

要瀏覽一個卷影副本(例如GLOBALROOT \設備\ HarddiskVolumeShadowCopy6),你需要創建一個符號鏈接到它(Windows快捷鍵),然後你就可以在瀏覽Windows資源管理器。下面 示例代碼:

# Load assembly to create symlink 
try { 
    $null = [mklink.symlink] 
} 
catch { 
Add-Type @" 
    using System; 
    using System.Runtime.InteropServices; 

    namespace mklink 
    { 
    public class symlink 
    { 
     [DllImport("kernel32.dll")] 
     public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); 
    } 
    } 
"@ 
} 
# create symlink 
[mklink.symlink]::CreateSymbolicLink('symlink path (example C:\temp\link1)', '\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy4\', 1);