How can I use NT Authentication using VBSCRIPT?

Hello Everyone,

I have a script that creates, reads and delete a file from our network drive. I wrote this script to see if I can find out what user can't access our network drive on any given reason. I have been having trouble with some users losing their network permissions off and on this past few days.

What I'm hoping to do is have the vbscript run as the user(s) and see if the user's network permission is in good standing or not.

How can I get the below script to run as a particular user? P.S. we use AD as our NT Authentication. Thank you
'**********************************************************************
'  Visual Basic ActiveX Script
'************************************************************************
 
Function Main()
 
'Step #1 - Delete file if already exist
Set objFSO = CreateObject("Scripting.FileSystemObject")
IF objFSO.FileExists("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt")Then
objFSO.DeleteFile("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt")
End If
 
dtEndDate=DateAdd("s", 3, Now)
   while Now < dtEndDate
'waiting for file deletion to finish
   wend
 
'Step #2 - Create text file 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt")
 
dtEndDate=DateAdd("s", 3, Now)
   while Now < dtEndDate
'waiting for file creation to finish
   wend
 
	Main =DTSTaskExecResult_Success
End Function
 
'Step #3 - Read text file 
Function Main()
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt", ForAppending, True)
Set colServices =  GetObject("winmgmts:").ExecQuery _
    ("Select * from Win32_Service")
For Each objService in colServices    
    objTextFile.WriteLine(objService.DisplayName & vbTab & _
        objService.State)
Next
objTextFile.Close
	Main =DTSTaskExecResult_Success
End Function

Open in new window

redouardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nmcdermaidCommented:
So you are running this in DTS?
I suggest you run it as a standalone VBS script (not inside DTS) and schedule to run everyday using the RUNAS command.
I started converting it to standalone VBScript, but you have two Function Mains(). Why is that?
In actual fact you could run just a DOS command script to try creating a file on a share under different credentials. I strongly suggest you add this to the DOS/Batch Scripting Zone!
0
redouardAuthor Commented:
Hello,

I made the changes you recommended but now when I run the following command nothing happens

C:\>RUNAS /profile /user:ad\XXXX "CScript.exe \"c:\scriptname.vbs"\"

What am I doing wrong?
0
nmcdermaidCommented:
Did you work out why you have two Mains in it? I suggest you also add
 
msgbox "complete"
 
to the end of the code just to tell it finished running.
Lastly... did it actually create the text file?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

redouardAuthor Commented:
I modified the script to have only one Function Main. Here's the modified vbs code:

'**********************************************************************
'  Visual Basic ActiveX Script
'************************************************************************
Function Main()

'Step #1 - Delete file if already exist
Set objFSO = CreateObject("Scripting.FileSystemObject")
IF objFSO.FileExists("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt")Then
objFSO.DeleteFile("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt")
End If
 
dtEndDate=DateAdd("s", 6, Now)
   while Now < dtEndDate
'waiting for file deletion to finish
   wend
       Main =DTSTaskExecResult_Success
End Function

and no it did not create a file.
0
nmcdermaidCommented:
OK, I've now converted that code from VBS that runs inside DTS to VBS that runs outside DTS.
 
Basically you have to explicitly call Main.

Call Main()
 
Sub Main()
 
'Step #1 - Delete file if already exist
Set objFSO = CreateObject("Scripting.FileSystemObject")
IF objFSO.FileExists("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt")Then
objFSO.DeleteFile("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt")
End If
 
dtEndDate=DateAdd("s", 6, Now)
   while Now < dtEndDate
'waiting for file deletion to finish
   wend
 
 
msgbox "Finished"
 
End Function

Open in new window

0
nmcdermaidCommented:
Version 2.... this one is more likley to run without an error. Please disregard prior post.
Call Main()
 
Sub Main()
 
'Step #1 - Delete file if already exist
Set objFSO = CreateObject("Scripting.FileSystemObject")
IF objFSO.FileExists("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt")Then
objFSO.DeleteFile("\\Jcnas01\cms\CMSOPEN\NagiosAccess.txt")
End If
 
dtEndDate=DateAdd("s", 6, Now)
   while Now < dtEndDate
'waiting for file deletion to finish
   wend
 
 
msgbox "Finished"
 
End Sub

Open in new window

0
nmcdermaidCommented:
.... oh and as you are running it as another user, the msgbox may not appear.
I suggest you remove the msgbox line as I recall you will be running this a scheduled batch process.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.