Üzerinde çalıştığım bir yazılım için bir çok virtual domain oluşturmam gerekiyordu. Şu şekilde bir direk metabase.xml (sadece IIS 6.0+) e kopyalanıp yapışitırılacak bir script yaptım.
Aşağıdaki kod basit olarak bir klasördeki tüm alt klasörlerin isimlerinden IIS' e bir virtual domain açıyor. Domain ayarlarını template.xml den değiştirebilirsiniz.
Imports System.IO
''' <summary>
''' Simple application to generate testsites for new test systems
''' </summary>
''' <remarks></remarks>
Module GenerateConst ROOT_VAR As String = "{ROOT}"
Const NAME_VAR As String = "{NAME}"
Const RANDOM_VAR As String = "{RANDOM}"Sub Main()
Dim Root As String = "c:\VirtualDomains"
If My.Application.CommandLineArgs.Count > 0 Then
Root = My.Application.CommandLineArgs(0)
End If'If there is a second paremeter generate host files
Dim GenerateHostFiles As Boolean = My.Application.CommandLineArgs.Count > 1Dim IPAddress As String
If My.Application.CommandLineArgs.Count > 1 Then
IPAddress = My.Application.CommandLineArgs(1)
End IfDim Template As String = File.ReadAllText("template.xml")
Dim Output As New Text.StringBuilder(2000)
For Each Folder As String In IO.Directory.GetDirectories("C:\SetupFolders")
Dim TestFolder As String = Path.GetFileName(Folder)
If TestFolder.StartsWith(".") Then Continue For
If GenerateHostFiles Then
Output.AppendLine(IPAddress & vbTab & vbTab & TestFolder & ".example.com")Else
Output.AppendLine(ReplaceVariables(Template, Root, TestFolder))End If
Next Folder
Console.Write(Output.ToString)
End Sub
''' <summary>
''' Replaces the variables
''' </summary>
''' <param name="source">The source.</param>
''' <param name="root">The root.</param>
''' <param name="name">The name.</param>
''' <returns></returns>
Function ReplaceVariables(ByVal source As String, ByVal root As String, ByVal name As String) As String
Static counter As Integer
counter += 1Dim RetText As String = source
Dim Ran As String = (New Random(counter).Next(100, 999) + counter).ToString
RetText = RetText.Replace(ROOT_VAR, root)
RetText = RetText.Replace(NAME_VAR, name)
RetText = RetText.Replace(RANDOM_VAR, Ran)Return RetText
End FunctionEnd Module
Bu da template.xml dosyası
<IIsWebServer Location ="/LM/W3SVC/1770007{RANDOM}"
AuthFlags="0"
ServerAutoStart="TRUE"
ServerBindings=":80:{NAME}.example.com"
ServerComment="{NAME}.example.com"
>
</IIsWebServer><IIsWebVirtualDir Location ="/LM/W3SVC/1770007{RANDOM}/root"
AccessFlags="AccessRead | AccessScript"
AppFriendlyName="Default Application"
AppIsolated="2"
AppRoot="/LM/W3SVC/1770007{RANDOM}/Root"
AuthFlags="AuthAnonymous | AuthNTLM"
DirBrowseFlags="DirBrowseShowDate | DirBrowseShowTime | DirBrowseShowSize | DirBrowseShowExtension | DirBrowseShowLongDate | EnableDefaultDoc"
Path="{ROOT}\{NAME}"
>
</IIsWebVirtualDir>
Şöyle Çalıştırabilirsiniz :
X:\GenerateTestSites.exe
Çıktıyı metabase.xml' e yapıştırın.
X:\GenerateTestSites.exe 1 127.0.0.1 > out.txt && out.txt
Çıktıyı hosts dosyasınıza yapıştırın. Normal şartlarında bu işi yapmak ölüm, özellikle de test sitelerinin bulunduğu klasör sürekli değişiyorsa.
