Returned value

Started by Jeff, January 07, 2020, 11:24:01 AM

Previous topic - Next topic

Jeff

I am working on a script that retrieves the local path of the websites on a Microsoft IIS Server. On my development station, the script returns "c:\inetpub\wwwroot" for the "Default Web Site". On my test servers, the script returns %systemdrive%\inetpub\wwwroot. This is typically not a big deal, but I am using the returned value to copy a file to the returned value, I expect to be c:\inetpub\wwwroot.

When I run the script, it creates a directory called %systemdrive% in the script directory and populates it according to the value Instead of putting the file in "c:".  WinBatch is not returning the value of the %systemdrive% variable, but treating it as a string.

An thoughts on this?



; Load Shell Operations Extender
AddExtender("wwsop34i.dll",0,"wwsop64i.dll")             


rawSelectedSiteName = "Default Web Site"
;SYSTEMDRIVE = Environment("SYSTEMDRIVE")

objService = GetObject(StrCat("winmgmts:\\.\root\webadministration" ))
objVirtualDirectorys = objService.ExecQuery(StrCat("SELECT * FROM VirtualDirectory where SiteName = '",rawSelectedSiteName,"' and ApplicationPath = '/'")) ;SiteName = '%selectedsitename%' and

SelectedSitePhysicalPath=""
SelectedSitePhysicalPathList=""
SiteCount=0

ForEach objVirtualDirectory In objVirtualDirectorys

              SiteCount=SiteCount + 1
              SelectedSitePhysicalPath = objVirtualDirectory.PhysicalPath
              Message(StrCat("SelectedSitePhysicalPath # ", SiteCount) ,SelectedSitePhysicalPath)
              SelectedSitePhysicalPathList = StrCat(SelectedSitePhysicalPathList,@TAB,SelectedSitePhysicalPath)
Next

tabstr = StrSub (SelectedSitePhysicalPathList,1,1)
If tabstr == @Tab
   SelectedSitePhysicalPathList = StrSub (SelectedSitePhysicalPathList,2,-1)
End If
SelectedSitePhysicalPathList = StrReplace (SelectedSitePhysicalPathList,@TAB,@CRLF)

Message("SelectedSitePhysicalPath",SelectedSitePhysicalPath)

If !DirExist(SelectedSitePhysicalPath) Then DirMake(SelectedSitePhysicalPath)
aFileCopy("C:\Windows\System32\drivers\etc\hosts",SelectedSitePhysicalPath,0)
Jeff

td

"%SystemDrive%" is an environment variable.  Type the "set systemdrive" from a command prompt to see its value.  Why MSFT's WMI class object is returning an environment variable in the path is not for me to speculate about but it does happen.  WIL file functions are doing exactly what you are asking them to do. Nothing more and nothing less.  Assuming there is no WMI property you can set to change the behavior, you will need to parse the variable out of the path and then call the "Environment" function to get its value.  Don't forget to remove the percent signs before passing the variable name to the Environment function.   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Jeff

Thanks for the quick response, its is always good.

I have tried to parse it out with the following, but I get the same results.

SelectedSitePhysicalPathList=StrReplace(SelectedSitePhysicalPathList, "%%SYSTEMDRIVE%%",SYSTEMDRIVE)

Do you have any other ideas on how to handle it?
Jeff

td

As previously mentioned "systemdrive" is an environment variable so you need to use the WIL Environment function to get the value the variable represents. See the Environment function's documentation in the Consolidated WIL Help file for details about the function.   The are multiple ways to extract the variable from the path.  The simplest might be to use the ItemExtract function with a delimiter of "'%%"

Here is a simple example that only works for a path with a leading environment variable.

Code (winbatch) Select
strPath = '%%systemdrive%%\temp'
strVar = ItemExtract(2, strPath, '%%')
strNoVarPath = Environment(strVar):ItemExtract(3,strPath,'%%')
Message('Converted E Var in Path', strPath:@lf:strNoVarPath)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade