''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Restore Hyper-V Permissions ' ' ' ' Author: Mike McGuire (mike@mikejmcguire.com) ' ' Updated: 14 OCT 2013 ' ' ' ''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit Dim oFS, oConfigFile, oConfigStream, oSnapshotStream, oScriptFile Dim strHyperVFolder, strVMID, strLine, strLine2, strDisk, strSnapshot Dim bSnapshot Set oFS = CreateObject("Scripting.FileSystemObject") Set oScriptFile = oFS.CreateTextFile("RepairHVPermissions.Bat", True) If WScript.Arguments.Count >= 1 Then strHyperVFolder = WScript.Arguments(0) If Not oFS.FolderExists(strHyperVFolder) Then WScript.Echo "Hyper-V Folder does not exist (" & strHyperVFolder & ")" WScript.Quit End If Else WScript.Echo "Hyper-V Folder not specified" WScript.Quit End If WScript.Echo "Hyper-V Folder: " & strHyperVFolder WScript.Echo "Creating RepairHVPermissions.Bat" oScriptFile.WriteLine "@Echo Off" oScriptFile.WriteLine "icacls """ & strHyperVFolder & """ /grant ""CREATOR OWNER"":(OI)(CI)(IO)F" oScriptFile.WriteLine "icacls """ & strHyperVFolder & """ /grant ""NT AUTHORITY\SYSTEM"":(OI)(CI)F" oScriptFile.WriteLine "icacls """ & strHyperVFolder & """ /grant ""NT VIRTUAL MACHINE\Virtual Machines"":(RC,S,GR,RD,WD,AD,REA,RA)" oScriptFile.WriteLine "icacls """ & strHyperVFolder & """ /grant ""NT VIRTUAL MACHINE\Virtual Machines"":(CI)(IO)(GR,WD,AD)" oScriptFile.WriteLine "icacls """ & strHyperVFolder & "\Virtual Hard Disks"" /grant ""NT VIRTUAL MACHINE\Virtual Machines"":(RC,S,GR,RD,WD,AD,REA,RA)" oScriptFile.WriteLine "icacls """ & strHyperVFolder & "\Virtual Hard Disks"" /grant ""NT VIRTUAL MACHINE\Virtual Machines"":(CI)(IO)(GR,WD,AD)" oScriptFile.WriteLine For Each oConfigFile In oFS.GetFolder(strHyperVFolder & "\Virtual Machines").Files bSnapshot = False If Len(oConfigFile.Name) = 40 Then If UCase(Right(oConfigFile.Name, 4)) = ".XML" Then strVMID = Left(oConfigFile.Name, Len(oConfigFile.Name) - 4) Set oConfigStream = oConfigFile.OpenAsTextStream(1, -1) '1 = ForReading, -1 = Unicode Do Until oConfigStream.AtEndOfStream strLine = oConfigStream.ReadLine If InStr(1, UCase(strLine), "VHDX 0 Then strDisk = Mid(strLine, InStr(1, strLine, ">") + 1, InStrRev(strLine, "") - 1) oScriptFile.WriteLine "icacls """ & strDisk & """ /grant ""NT VIRTUAL MACHINE\" & strVMID & """:(RC,S,GR,GW,RD,WD,AD,REA,WEA,RA,WA)" End If If InStr(1, UCase(strLine), "") > 0 Then bSnapshot = True If InStr(1, UCase(strLine), "") > 0 Then bSnapshot = False If bSnapShot And InStr(1, UCase(strLine), "") Then strSnapshot = Mid(strLine, InStr(1, strLine, ">") + 1, InStrRev(strLine, "") - 1) Set oSnapshotStream = oFS.OpenTextFile(strHyperVFolder & "\Snapshots\" & strSnapshot & ".Xml", 1, , -1) '1 = ForReading, -1 = Unicode Do Until oSnapshotStream.AtEndOfStream strLine2 = oSnapshotStream.ReadLine If InStr(1, UCase(strLine2), "VHDX 0 Then strDisk = Mid(strLine2, InStr(1, strLine2, ">") + 1, InStrRev(strLine2, "") - 1) oScriptFile.WriteLine "icacls """ & strDisk & """ /grant ""NT VIRTUAL MACHINE\" & strVMID & """:(RC,S,GR,GW,RD,WD,AD,REA,WEA,RA,WA)" End If Loop oSnapshotStream.Close Set oSnapshotStream = Nothing End If Loop oConfigStream.Close Set oConfigStream = Nothing End If End If Next oScriptFile.Close Set oScriptFile = Nothing Set oFS = Nothing