Monday, August 20, 2012

Multi-App WSP Install.bat

@echo Deploy my.package.wsp
:usage - install.bat url1 url2 ...
@echo off

if "%1" == "" goto NoInputErrorMessage

setlocal

set SPAdminTool=%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN\stsadm.exe

@echo on

"%SPAdminTool%" -o addsolution -filename my.package.wsp
"%SPAdminTool%" -o execadmsvcjobs
"%SPAdminTool%" -o deploysolution -name "my.package.wsp" -immediate  -allowGacDeployment -allowCasPolicies -force
"%SPAdminTool%" -o execadmsvcjobs

@echo off
:LOOP
IF "X%1" == "X" GOTO LOOPDONE
echo Deploying solution to %1
@echo on
"%SPAdminTool%" -o activatefeature -name myappfeature -url %1
@echo off
SHIFT
GOTO LOOP
:LOOPDONE

pause
echo The batch file is complete.
GOTO ENDFINAL

:NoInputErrorMessage
@ECHO Please enter these arguments: Application URLs separated by spaces
@echo Sample command: Install.bat https://myapp1 https://myapp2
:ENDFINAL

Wednesday, August 15, 2012

Get Matching DLLs in VS Solution

Option Explicit
' need args for (0) SolutionDir (1) ilmerge executable (2) .snk file (3) /ver (4) output dll (5) Solution Project dir 
'(6) primary assembly file name e.g. "mylib.dll" (7) pubkey string of target DLLs e.g. "abcd123456787651"
'Ex: c:\Temp\>CreateMABat.vbs "C:\MySolution\" "c:\ilmerge\ilmerge.exe" "C:\MySolution\MyMainProject\MyNamespace.snk" 1.0.0.0 "c:\MySolution\MyMainProject\Dlls\CombinedLibrary.dll" "c:\MySolution\MyMainProject\" MyFirstDep.dll abcd123456787651
Dim args, pubKeyStr, matchingDLLs, objFSO, objShell, DictDlls
Dim oMergeBat
Dim oFS
Set args = WScript.Arguments
pubKeyStr = args(7)
matchingDLLs = GetNewDllsThatMatchKey (args(0), pubKeyStr, args(6))

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oMergeBat = oFS.CreateTextFile(args(5) + "MergeAssemblies.bat", true)
oMergeBat.WriteLine("@ECHO OFF")
oMergeBat.WriteLine("@ECHO This script is generated by createMergeAssemblies.vbs")
oMergeBat.WriteLine("")
oMergeBat.WriteLine("@ECHO ...starting the merge program")
oMergeBat.WriteLine("@ECHO ...")
oMergeBat.WriteLine("@ECHO ...")
oMergeBat.WriteLine("")
oMergeBat.WriteLine("""" + args(1) + """ /ndebug /t:library /keyfile:""" + args(2) + """ /ver:" + args(3) + " /out:""" + args(4) + """ " + matchingDLLs)
oMergeBat.WriteLine("")
oMergeBat.WriteLine("@ECHO ...merging process complete")
oMergeBat.WriteLine("")
oMergeBat.WriteLine("Pause")


'WScript.Echo result

' given a root directory find all .dlls in subdirectories (exclude the root directory) 
' for each dll found
'  get its public key
' if it matches a pubkeyparamStr
'  check dllsdict for key matching its filename
'  if not found insert it
'  if found 
'   compare its last modified date against existing file found
'   if newer, replace dict entry
Function GetNewDllsThatMatchKey(ByVal filePath, ByVal snKey, ByVal firstDll)
  
 Dim i, a, resultStr
 Set dictDlls = CreateObject("Scripting.Dictionary")

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objShell = WScript.CreateObject("WScript.Shell")
 GetDictOfDlls filePath, "DLL"
 resultStr = """" & dictDlls.Item(firstDll) & """"
 dictDlls.Remove(firstDll)
 a = dictDlls.Keys
 For i=0 to dictDlls.Count-1
  resultStr = resultStr & " """ & dictDlls.Item(a(i)) & """"
 ' WScript.Echo a(i)
 ' WScript.Echo dictDlls.Item(a(i))
 Next
 GetNewDllsThatMatchKey = resultStr
End Function

Sub GetDictOfDlls(ByVal strDirectory, ByVal strExtSought)
 Dim objFolder, objSubFolder, objFile, filePubKey, objKeyFile
 Set objFolder = objFSO.GetFolder(strDirectory)
 objShell.CurrentDirectory = strDirectory
 For Each objFile in objFolder.Files
  ' WScript.Echo objFile.Path

  If Right(Ucase(objFile.Path),Len(strExtSought)+1) = "." & strExtSought Then
   filePubKey = GetFilePubKeyStr(objFile.Name)
   If pubKeyStr = filePubKey Then
    If dictDlls.Exists(objFile.Name) = True Then
     'WScript.Echo "Exists " & objFile.Path
     Set objKeyFile = objFSO.GetFile(dictDlls.Item(objFile.Name))
     'WScript.Echo "date is " & objKeyFile.DateLastModified & " compared to " & objFile.DateLastModified
     If objKeyFile.DateLastModified < objFile.DateLastModified Then
      dictDlls.Item(objFile.Name) = objFile.Path
     End If
    Else
     dictDlls.Add objFile.Name, objFile.Path
    End If
    'WScript.Echo filePubKey
   End If
  End If
 Next
 For Each objSubFolder in objFolder.SubFolders
  GetDictOfDlls objSubFolder.Path, strExtSought
 Next
End Sub

Function GetFilePubKeyStr(ByVal fileName)
 dim snExe, snCmd, fullCmd, strLine, objExecObject
 snExe = """C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sn.exe"""
 snCmd = "cmd /c """ & snExe & """ -T "
 fullCmd = snCmd & fileName
 Set objExecObject = objShell.Exec(fullCmd)
 Do While Not objExecObject.StdOut.AtEndOfStream
  strLine = objExecObject.StdOut.ReadLine()
  'Wscript.Echo strLine
  If InStr(strLine, "Public key token is ") then
   GetFilePubKeyStr = Right(strLine,16)
   'Wscript.Echo Right(strLine,16)
  end if
 Loop

 Do While Not objExecObject.StdErr.AtEndOfStream
  Wscript.Echo objExecObject.StdErr.ReadLine()
 Loop 
End Function