From 347cbb42c25b1b319913b2c20025c74cc94143d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20M=E4kinen?= Date: Mon, 10 Dec 2012 13:10:54 +0200 Subject: [PATCH] Initial import. --- vmrun.vbs | 161 ++++++++++++++++++++++++++++++++++++++++++++++ vmware-images.vbs | 128 ++++++++++++++++++++++++++++++++++++ 2 files changed, 289 insertions(+) create mode 100644 vmrun.vbs create mode 100644 vmware-images.vbs diff --git a/vmrun.vbs b/vmrun.vbs new file mode 100644 index 0000000..6322bee --- /dev/null +++ b/vmrun.vbs @@ -0,0 +1,161 @@ + +' Copyright (c) 2009 Timo Makinen + +' All rights reserved. + +' Redistribution and use in source and binary forms, with or without +' modification, are permitted provided that the following conditions +' are met: +' 1. Redistributions of source code must retain the above copyright +' notice, this list of conditions and the following disclaimer. +' 2. Redistributions in binary form must reproduce the above copyright +' notice, this list of conditions and the following disclaimer in the +' documentation and/or other materials provided with the distribution. +' 3. The name of the author may not be used to endorse or promote products +' derived from this software without specific prior written permission. + +' THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +' IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +' OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +' IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +' INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +' NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +' DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +' THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +' (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +' $Id: vmrun.vbs,v 1.13 2012/08/07 11:27:49 root Exp $ + +Option Explicit + +Dim aSharedFolders, aRemoveOptions + +' list of folders that are shared to guests +aSharedFolders = Array("P:\", "Y:\", "Z:\") + +' list of options to remove from source vmx +aRemoveOptions = Array("sharedFolder.*", _ + "isolation\.tools\.hgfs\.disable.*", _ + "tools\.remindInstall.*", _ + "tools\.upgrade\.policy.*", _ + "diskLib.sparseMaxFileSizeCheck.*", _ + "uuid\..*", _ + "ethernet\d+\.generatedAddress.*", _ + "(scsi|ide)\d+:\d+\.mode.*") + + +' check arguments +Dim oArgs, sVMX, iCount +Set oArgs = WScript.Arguments +If oArgs.Length < 1 Then + WScript.Echo "Usage: vmrun.vbs " + WScript.Quit +End If +For iCount=0 To oArgs.Count -1 + sVMX = sVMX & " " & oArgs(iCount) +Next +sVMX = Trim(sVMX) +Set oArgs = Nothing + +' init base objects +Dim oFS, oShell, oNetwork +Set oFS = CreateObject("Scripting.FileSystemObject") +Set oShell = CreateObject("WScript.Shell") +Set oNetwork = WScript.CreateObject("WScript.Network") + +' check that requested vmx file is found +If Not oFS.FileExists(sVMX) Then + WScript.Echo "ERR: Path not found """ & sVMX & """." + WScript.Quit +End If + +' initialize regexps for removing options +Dim sRegexp +For iCount=0 To UBound(aRemoveOptions) + sRegexp = aRemoveOptions(iCount) + Set aRemoveOptions(iCount) = New RegExp + aRemoveOptions(iCount).Pattern = "(" & sRegexp & ")" + aRemoveOptions(iCount).IgnoreCase = True +Next + +' get working directory and create it if it does not exist +Dim sVMFolder, sDestination +sVMFolder = oFS.BuildPath(oShell.ExpandEnvironmentStrings("%TEMP%"), "vmware") +If Not oFS.FolderExists(sVMFolder) Then + oFS.CreateFolder(sVMFolder) +End If +sDestination = oFS.BuildPath(sVMFolder, _ + oFS.GetFolder(oFS.GetFile(sVMX).ParentFolder).Name) +If Not oFS.FolderExists(sDestination) Then + oFS.CreateFolder(sDestination) +End If + + +' copy vmx file +Dim oSource, oDestination, sLine, bSkip, oMatches +Set oSource = oFS.OpenTextFile(sVMX, 1) +Set oDestination = oFS.OpenTextFile(oFS.BuildPath(sDestination, oFS.GetFile(sVMX).Name), 2, True) +Do Until oSource.AtEndOfStream + sLine = oSource.ReadLine + bSkip = False + For Each sRegexp In aRemoveOptions + If sRegexp.Test(sLine) Then + bSkip = True + Exit For + End If + Next + If Not bSkip Then + Set sRegexp = New RegExp + sRegexp.Pattern = "^((scsi|ide)\d+:\d+)\.fileName = ""?(.*\.vmdk)""?.*" + Set oMatches = sRegexp.Execute(sLine) + If oMatches.Count > 0 Then + oDestination.WriteLine oMatches(0).SubMatches(0) & ".fileName = """ & _ + oFS.BuildPath(oFS.GetFile(sVMX).ParentFolder, _ + oMatches(0).SubMatches(2)) & """" + oDestination.WriteLine oMatches(0).SubMatches(0) & _ + ".mode = ""independent-nonpersistent""" + Else + oDestination.WriteLine(sLine) + End If + End If +Loop + +' add computer name running vmware into guestinfo.host option +oDestination.WriteLine("guestinfo.host = """ & oNetwork.ComputerName & """") + +' add fix for running images via unc path +oDestination.WriteLine("diskLib.sparseMaxFileSizeCheck = ""false""") + +' disable complaining of vmware tools +oDestination.WriteLine("tools.remindInstall = ""FALSE""") +oDestination.WriteLine("tools.upgrade.policy = ""Manual""") + +' add shared folders +Dim sPath +iCount = 0 +For Each sPath In aSharedFolders + If oFS.FolderExists(sPath) Then + oDestination.WriteLine("sharedFolder" & iCount & ".present = ""TRUE""") + oDestination.WriteLine("sharedFolder" & iCount & ".enabled = ""TRUE""") + oDestination.WriteLine("sharedFolder" & iCount & ".readAccess = ""TRUE""") + oDestination.WriteLine("sharedFolder" & iCount & ".writeAccess = ""TRUE""") + oDestination.WriteLine("sharedFolder" & iCount & ".hostPath = """ _ + & sPath & """") + oDestination.WriteLine("sharedFolder" & iCount & ".guestName = """ _ + & Left(sPath, 1) & """") + oDestination.WriteLine("sharedFolder" & iCount & ".expiration = ""session""") + iCount = iCount + 1 + End If +Next +If iCount > 0 Then + oDestination.WriteLine("sharedFolder.maxNum = """ & iCount & """") + oDestination.WriteLine("isolation.tools.hgfs.disable = ""FALSE""") +End If + +Set oDestination = Nothing +Set oSource = Nothing + +oShell.Run """" & oFS.BuildPath(sDestination, oFS.GetFile(sVMX).Name) & """" +Set oFS = Nothing +Set oShell = Nothing diff --git a/vmware-images.vbs b/vmware-images.vbs new file mode 100644 index 0000000..d590068 --- /dev/null +++ b/vmware-images.vbs @@ -0,0 +1,128 @@ + +Option Explicit + +Const ForReading = 1 + +Dim oFS, oNetwork, oShell +Set oFS = CreateObject("Scripting.FileSystemObject") +Set oNetwork = CreateObject("WScript.Network") +Set oShell = CreateObject("WScript.Shell") + +Dim sDrive, sVmrunPath +sDrive = "\\st-vmware\vmware" +sVmrunPath = sDrive & "\bin\vmrun.vbs" + +If LCase(Left(oNetwork.ComputerName, 9)) = "stupid-pc" Or _ + LCase(oNetwork.ComputerName) = "ee-pc2" Then + + ' Determine vmware icon path and quit if not found (eg. vmware not installed) + Dim sIconPath + On Error Resume Next + sIconPath = oShell.RegRead("HKCR\VMware.Document\DefaultIcon\") + If Err.Number Then + WScript.Quit + End If + On Error Goto 0 + + ' Init regexps + Dim oReConfig + Set oReConfig = New RegExp + oReConfig.IgnoreCase = True + oReConfig.Pattern = "^\s*([a-z\.0-9]*)\s*=\s*""?([^""]+)""?$" + + ' Get path to users start menu + Dim sShortcutFolder + sShortcutFolder = oShell.SpecialFolders("StartMenu") & _ + "\Programs\VMware Images" + + ' Create shortcut directory if it doesn't exist + If Not oFS.FolderExists(sShortcutFolder) Then + oFS.CreateFolder(sShortcutFolder) + End If + + ' Remove old shortcuts + Dim oFolder, oFile + Set oFolder = oFS.GetFolder(sShortcutFolder) + For Each oFile In oFolder.Files + If Right(Lcase(oFile.Name), 4) = ".lnk" Then + oFS.DeleteFile(oFile.Path) + End If + Next + + ' Find image files and loop through them + Dim oImageFolder, iTest + Set oImageFolder = oFS.GetFolder(sDrive & "\") + For Each oFolder In oImageFolder.SubFolders + On Error Resume Next + iTest = oFolder.Files.Count + If Err.Number = 0 Then + On Error Goto 0 + For Each oFile In oFolder.Files + If Right(Lcase(oFile.Path), 4) = ".vmx" Then + + ' Get image information + Dim sName, oFilePointer, sLine, oMatches + sName = oFile.Name + Set oFilePointer = oFS.OpenTextFile(oFile.Path, ForReading) + Do Until oFilePointer.AtEndOfStream + sLine = oFilePointer.ReadLine + Set oMatches = oReConfig.Execute(sLine) + If oMatches.Count = 1 Then + If oMatches.Item(0).SubMatches.Count = 2 Then + Dim sKey, sValue + sKey = Lcase(oMatches.Item(0).SubMatches.Item(0)) + sValue = oMatches.Item(0).SubMatches.Item(1) + If sKey = "displayname" Then + sName = sValue + End If + End If + End If + Set oMatches = Nothing + Loop + Set oFilePointer = Nothing + + ' Create shortcut + Dim oShortcut + Set oShortcut = oShell.CreateShortcut( _ + sShortcutFolder & "\" & sName & ".lnk") + oShortcut.TargetPath = sVmrunPath + oShortcut.Arguments = oFile.Path + oShortcut.WorkingDirectory = oShell.ExpandEnvironmentStrings("%TEMP%") + oShortcut.IconLocation = sIconPath + oShortcut.Save + + End If + Next + Else + On Error Goto 0 + End If + Next + + ' Fix vmware preferences.ini + Dim sPreferences + sPreferences = oFS.BuildPath(oShell.ExpandEnvironmentStrings("%APPDATA%"), _ + "VMware\preferences.ini") + If Not oFS.FileExists(sPreferences) Then + If Not oFS.FolderExists(oFS.GetParentFolderName(sPreferences)) Then + oFS.CreateFolder(oFS.GetParentFolderName(sPreferences)) + End If + Set oFile = oFS.CreateTextFile(sPreferences) + oFile.WriteLine(".encoding = ""windows-1252""") + oFile.WriteLine("pref.vmplayer.exit.vmaction = ""poweroff""") + oFile.WriteLine("pref.vmplayer.exit.softly = FALSE") + oFile.WriteLine("pref.enableAllSharedFolders = TRUE") + oFile.WriteLine("hint.lsilogic.needDriver = FALSE") + oFile.WriteLine("hint.keyboardHook.hookTimeout = FALSE") + oFile.WriteLine("pref.keyboardAndMouse.maxProfiles = 0") + oFile.WriteLine("pref.vmplayer.dataCollectionEnabled = FALSE") + oFile.WriteLine("pref.autoSoftwareUpdatePermission = ""deny""") + oFile.WriteLine("pref.componentDownloadPermission = ""deny""") + oFile.Close + Set oFile = Nothing + End If + +End If + +Set oShell = Nothing +Set oFS = Nothing +Set oNetwork = Nothing