{"id":91,"date":"2008-01-29T09:25:02","date_gmt":"2008-01-29T09:25:02","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?page_id=91"},"modified":"2008-11-08T09:57:08","modified_gmt":"2008-11-08T09:57:08","slug":"staffwebsites_csextensionvb","status":"publish","type":"page","link":"https:\/\/www.wapshere.com\/missmiis\/code-snippets\/staffwebsites_csextensionvb","title":{"rendered":"StaffWebsites_CSExtension.vb"},"content":{"rendered":"<pre>Imports Microsoft.MetadirectoryServices\r\nImports System.IO\r\nImports System.Data.SqlClient\r\nImports System.DirectoryServices<\/pre>\n<pre>' CSExtension for an MA which manages Virtual Directories in IIS.\r\n' IIS must be installed on the ILM server as code relies on iisVDir.vbs,\r\n' however the websites can be on a remote server.<\/pre>\n<pre>' Written by Carol Wapshere, 17th January 2008<\/pre>\n<pre>Public Class MACallExport_StaffWebsites\r\nImplements IMAExtensibleFileImport\r\nImplements IMAExtensibleCallExport<\/pre>\n<pre>Const IIS_SERVER As String = \"dc01\"\r\nConst PARENT_SHARE As String = \"\\\\dc01\\StaffWebsites\\\"\r\nConst PARENT_FLDR As String = \"C:\\StaffWebsites\\\"\r\nDim LOGFILE_NAME As String = MAUtils.MAFolder &amp; \"\\export.log\"<\/pre>\n<pre>Dim logfile As StreamWriter<\/pre>\n<pre>Public Sub GenerateImportFile(ByVal filename As String, ByVal connectTo As String, ByVal user As String, ByVal password As String, ByVal configParameters As ConfigParameterCollection, ByVal fullImport As Boolean, ByVal types As TypeDescriptionCollection, ByRef customData As String) Implements IMAExtensibleFileImport.GenerateImportFile\r\n\u00c2\u00a0 Dim iisVdirQuery As String = RunCmd(\"cscript iisvdir.vbs \/s \" &amp; IIS_SERVER &amp; \" \/query w3svc\/1\/ROOT\/Staff\")\r\n\u00c2\u00a0 Dim arrVDir(,) As String\r\n\u00c2\u00a0 Dim i, j As Integer<\/pre>\n<pre>\u00c2\u00a0 i\u00c2\u00a0= -1\r\n\u00c2\u00a0 Dim posEnd As Integer = iisVdirQuery.LastIndexOf(\"=\")\r\n\u00c2\u00a0 Dim posStart As Integer = iisVdirQuery.IndexOf(\"\/\", posEnd)<\/pre>\n<pre>\u00c2\u00a0 While iisVdirQuery.Substring(posStart, 1) = \"\/\"\r\n\u00c2\u00a0 \u00c2\u00a0 i\u00c2\u00a0= i + 1\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 ReDim Preserve arrVDir(1, i)\r\n\u00c2\u00a0 \u00c2\u00a0 posEnd = iisVdirQuery.IndexOf(\" \", posStart)\r\n\u00c2\u00a0 \u00c2\u00a0 arrVDir(0, i) = iisVdirQuery.Substring(posStart, posEnd - posStart).Replace(\"\/\", \"\")\r\n\u00c2\u00a0 \u00c2\u00a0 posStart = posStart + 35\r\n\u00c2\u00a0 \u00c2\u00a0 posEnd = iisVdirQuery.IndexOf(vbCrLf, posStart)\r\n\u00c2\u00a0 \u00c2\u00a0 arrVDir(1, i) = iisVdirQuery.Substring(posStart, posEnd - posStart)\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 posStart = posEnd + 2\r\n\u00c2\u00a0 End While<\/pre>\n<pre>\u00c2\u00a0 Dim fw As StreamWriter\r\n\u00c2\u00a0\u00c2\u00a0Try\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 'Open the output file specified in the run profile\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 fw = New StreamWriter(filename, False)\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Catch ex As Exception\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Throw New UnauthorizedAccessException(\"Unable to open file: \" &amp; filename)\r\n\u00c2\u00a0\u00c2\u00a0End Try<\/pre>\n<pre>\u00c2\u00a0 fw.WriteLine(\"alias,path\")\r\n\u00c2\u00a0 If i &gt;= 0 Then\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 For j = 0 To i\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 fw.WriteLine(arrVDir(0, j) &amp; \",\" &amp; arrVDir(1, j))\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Next\r\n\u00c2\u00a0 End If\r\n\u00c2\u00a0 fw.Close()\r\nEnd Sub<\/pre>\n<pre>Public Sub BeginExport(ByVal connectTo As String, ByVal user As String, ByVal password As String, ByVal configParameters As ConfigParameterCollection, ByVal types As TypeDescriptionCollection) Implements IMAExtensibleCallExport.BeginExport\r\n\u00c2\u00a0 logfile = New StreamWriter(LOGFILE_NAME, False)\r\nEnd Sub<\/pre>\n<pre>Public Sub ExportEntry(ByVal modificationType As ModificationType, ByVal changedAttributes As String(), ByVal csentry As CSEntry) Implements IMAExtensibleCallExport.ExportEntry\r\n\u00c2\u00a0 Dim VDirAlias As String = csentry(\"alias\").Value\r\n\u00c2\u00a0 If modificationType = Microsoft.MetadirectoryServices.ModificationType.Add Then\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 CreateVDir(VDirAlias)\r\n\u00c2\u00a0 ElseIf modificationType = Microsoft.MetadirectoryServices.ModificationType.Delete Then\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 DeleteVDir(VDirAlias)\r\n\u00c2\u00a0 ElseIf modificationType = Microsoft.MetadirectoryServices.ModificationType.Replace Then\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 RenameVDir(csentry)\r\n\u00c2\u00a0 End If\r\nEnd Sub<\/pre>\n<pre>Public Sub EndExport() Implements IMAExtensibleCallExport.EndExport\r\n\u00c2\u00a0 logfile.Close()\r\nEnd Sub<\/pre>\n<pre>Private Sub CreateVDir(ByRef VDirAlias As String)\r\n  Dim newSharePath As String = PARENT_SHARE &amp; VDirAlias\r\n  Dim newDrivePath As String = PARENT_FLDR &amp; VDirAlias\r\n  Dim templatePath As String = PARENT_SHARE &amp; \"template\"<\/pre>\n<pre>If Not System.IO.Directory.Exists(newSharePath) Then\r\n  RunCmd(\"xcopy \/I \" &amp; templatePath &amp; \" \" &amp; newSharePath)\r\nElse\r\n  logfile.WriteLine(\"File \" &amp; newDrivePath &amp; \" already exists.\")\r\nEnd If\r\n  RunCmd(\"cacls \" &amp; PARENT_SHARE &amp; VDirAlias &amp; \" \/E \/G \" &amp; VDirAlias &amp; \":C\")\r\n  RunCmd(\"cscript iisvdir.vbs \/s \" &amp; IIS_SERVER &amp; \" \/create w3svc\/1\/ROOT\/Staff \" &amp; VDirAlias &amp; \" \" &amp; PARENT_FLDR &amp; VDirAlias)\r\nEnd Sub<\/pre>\n<pre>Private Sub DeleteVDir(ByRef VDirAlias As String)\r\n  'Only deletes the Virtual Directory from IIS - the folder is not touched.\r\n  RunCmd(\"cscript iisvdir.vbs \/s \" &amp; IIS_SERVER &amp; \" \/delete w3svc\/1\/ROOT\/Staff\/\" &amp; VDirAlias)\r\nEnd Sub<\/pre>\n<pre>Private Sub RenameVDir(ByRef csentry As CSEntry)\r\n'Renames the folder, deletes the old VDir, and creates a new one.\r\n'In a production environment a redirect should also be created for the old address.\r\n  Dim oldDrivePath As String = csentry(\"path\").Value\r\n  Dim oldAlias As String = oldDrivePath.Substring(oldDrivePath.LastIndexOf(\"\\\") + 1)\r\n  Dim oldSharePath As String = PARENT_SHARE &amp; \"\\\" &amp; oldAlias\r\n  Dim newAlias As String = csentry(\"alias\").Value\r\n  Dim newSharePath As String = PARENT_SHARE &amp; \"\\\" &amp; newAlias\r\n  Dim newDrivePath As String = PARENT_FLDR &amp; \"\\\" &amp; newAlias<\/pre>\n<pre>  RunCmd(\"ren \" &amp; oldSharePath &amp; \" \" &amp; newSharePath)\r\n  RunCmd(\"cscript iisvdir.vbs \/s \" &amp; IIS_SERVER &amp; \" \/delete w3svc\/1\/ROOT\/Staff\/\" &amp; oldAlias)\r\n  RunCmd(\"cscript iisvdir.vbs \/s \" &amp; IIS_SERVER &amp; \" \/create w3svc\/1\/ROOT\/Staff \" &amp; newAlias &amp; \" \" &amp; newDrivePath)\r\nEnd Sub<\/pre>\n<pre>Private Function RunCmd(ByRef command As String) As String\r\n  'Run a command in a cmd process and returns the output<\/pre>\n<pre>  logfile.Write(command)\r\n  Dim myProcess As Process = New Process()\r\n  Dim s As String\r\n  myProcess.StartInfo.FileName = \"cmd.exe\"\r\n  With myProcess.StartInfo\r\n    .UseShellExecute = False\r\n    .CreateNoWindow = True\r\n    .RedirectStandardInput = True\r\n    .RedirectStandardOutput = True\r\n    .RedirectStandardError = True\r\n  End With\r\n  myProcess.Start()<\/pre>\n<pre>  Dim sIn As StreamWriter = myProcess.StandardInput\r\n  sIn.AutoFlush = True\r\n  Dim sOut As StreamReader = myProcess.StandardOutput\r\n  Dim sErr As StreamReader = myProcess.StandardError<\/pre>\n<pre>  sIn.Write(command &amp; System.Environment.NewLine)\r\n  sIn.Write(\"exit\" &amp; System.Environment.NewLine)\r\n  s = sOut.ReadToEnd()\r\n  If Not myProcess.HasExited Then\r\n    myProcess.Kill()\r\n  End If<\/pre>\n<pre>  sIn.Close()\r\n  sOut.Close()\r\n  sErr.Close()\r\n  myProcess.Close()\r\n  Return s\r\nEnd Function<\/pre>\n<pre>End Class<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Imports Microsoft.MetadirectoryServices Imports System.IO Imports System.Data.SqlClient Imports System.DirectoryServices &#8216; CSExtension for an MA which manages Virtual Directories in IIS. &#8216; IIS must be installed on the ILM server as code relies on iisVDir.vbs, &#8216; however the websites can be on a remote server. &#8216; Written by Carol Wapshere, 17th January 2008 Public Class MACallExport_StaffWebsites Implements&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":30,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-91","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/Pkp1o-1t","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":2,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/91\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/91\/revisions\/250"}],"up":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/30"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}