1

I'm a system administrator and I'd like to have only one place to change/check for a NAS path in all web.config or global.asa files, in this way, any time the path change, I do not have to go into all web.config or global.asa files make the necessary change and hoping to avoid any mistake or forgotten file...

My idea is to have an environment variable on the local server that can be set and update.

<!-- metadata type="typelib" file="C:\Program Files\Common Files\System\ADO\msado15.dll" -->
<script language="vbscript" runat="server">

Sub Application_OnStart

dim strNAS
    strNAS = ${MyEnvironmentVar} ???
Application("NAS") =  strNAS

End Sub


' Instead of using an hard-coded path like below
'Application("News")="File Name=\\NAS04.MyDomain.com\mydata$\myfile.udl"
'I'd like to use a more flexible way...

Application("News")="File Name=" + Application("NAS") + "mydata$\myfile.udl"
or 
Application("News")="File Name=" + ${MyEnvironmentVar} + "mydata$\myfile.udl"

...

</script>

$MyEnvironmentVar is the variable set on the local server as environment variable and have the path to the current storage system...

MyEnvironmentVar = "\\NAS04.myDomain.com\" 

I saw around the web something like Environment.getenvironmentvariable(), but I'm not sure how to use it and if it works in the web.config and global.asa files.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Cozzaro Nero
  • 129
  • 1
  • 8

1 Answers1

2

In the Global.asa file you can try following code:

Set objWSH = CreateObject("WScript.Shell")
Set objUserVariables = objWSH.Environment("USER") 
path = objUserVariables("NAS") ' here NAS is name environment variable stored in User variables

If NAS is stored in System variables then change second line accordingly.

If you want to go with web.config approach here it is. However in classic ASP web.config is just another XML file. Your web.config looks like below:

<configuration>
  <appSettings>
     <add key="NAS" value="your path goes here" />
  </appSettings>
</configuration>

And the VBScript code looks like:

set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
set xmlappSettings = server.CreateObject("Microsoft.XMLDOM")
set xmladd = server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xmlDoc.load(server.MapPath("web.config"))
set xmlappSettings = xmldoc.GetElementsByTagName("appSettings").Item(0) 
set xmladd = xmlappSettings.GetElementsByTagName("add")
for each addItem in xmladd 
  if addItem.getAttribute("key") = "NAS" then
    myVar = addItem.getAttribute("value")
  end if
next
MarkKGreenway
  • 8,494
  • 5
  • 34
  • 53
Pankaj Kapare
  • 7,486
  • 5
  • 40
  • 56
  • 2
    Yeah, no. That's what the managed method [`Environment.GetEnvironmentVariable()`](http://msdn.microsoft.com/en-us/library/system.environment.getenvironmentvariable%28v=vs.110%29.aspx) is for. Edit: nevermind, Global.asa isn't for ASP.NET but classic ASP. – CodeCaster Jan 16 '15 at 15:36
  • 3
    But I believe application in question is classic asp application. right? – Pankaj Kapare Jan 16 '15 at 15:38