Jump to content

[Question] "Run As" in a script?


Alphaz

Recommended Posts

Hi,

I would like to know how I can create a script to run a program as the administrator, from a different user account, by encrypting the password in the batch file I will create.

For example, in my company, the users don't have admin rights on their PC, so they can't defragment their hard drives or empty a specific folder on the hard drive...

Any idea on how to do it? I know I can encrypt the admin password in an unattended install of windows, but for my need I don't see how to do it.

Thanks in advance for your help.

Regards

Title edited -- Please, use [TAGS] in your topic's title.

--Sonic

Link to comment
Share on other sites


You can't hide/crypt admin password in a simple .cmd file, except with use credential but you must have to enter once the password in clear.

Compiled VBS can be used I think, wait a moment for masters of vbs ^^

Link to comment
Share on other sites

Hiding the contents of your script can be done once your vbscript is complete via the Microsoft Script Encoder:

http://www.microsoft.com/downloads/details...&displaylang=en

As to using runas in the script, the following simple 4 lines will work:

set WshShell = CreateObject("WScript.Shell")
WshShell.Run "runas /user:User ""C:\runsomething.cmd"""
WScript.Sleep 100
WshShell.Sendkeys "password"

It can be done much cleaner, but the above is an easy 4 lines, and it's a start.

Link to comment
Share on other sites

Hiding the contents of your script can be done once your vbscript is complete via the Microsoft Script Encoder:

http://www.microsoft.com/downloads/details...&displaylang=en

As to using runas in the script, the following simple 4 lines will work:

set WshShell = CreateObject("WScript.Shell")
WshShell.Run "runas /user:User ""C:\runsomething.cmd"""
WScript.Sleep 100
WshShell.Sendkeys "password"

It can be done much cleaner, but the above is an easy 4 lines, and it's a start.

Hello,

Thanks for the above, it leads me close to what i want to do, but i copied the lines above and it still prompts me for the passord

<script LANGUAGE="VBScript">

'Copyright© 1998. XYZ Productions. All rights reserved.

'**Start Encode**

' Your code goes here.

set WshShell = CreateObject("WScript.Shell")

WshShell.Run "runas /user:USERNAME ""del c:\test.txt"""

WScript.Sleep 100

WshShell.Sendkeys "PASSWORD_OF_USER_ABOVE"

</SCRIPT>

Link to comment
Share on other sites

Obviously, if i send the password after running the command, it can't work... If I put the following below

WshShell.Run "runas /user:USERNAME ""del c:\test.txt"""

WScript.Sleep 100

WshShell.Sendkeys "PASSWORD_OF_USER_ABOVE"

WshShell.Run "del c:\test.txt"

Any idea why the file is not deleted ?

Link to comment
Share on other sites

Obviously, if i send the password after running the command, it can't work... If I put the following below

WshShell.Run "runas /user:USERNAME ""del c:\test.txt"""

WScript.Sleep 100

WshShell.Sendkeys "PASSWORD_OF_USER_ABOVE"

WshShell.Run "del c:\test.txt"

Any idea why the file is not deleted ?

You cannot delete the file because you are using the wrong object for it.

The changes I made are

This means the window is hidden and it will not go on to the next until it finished

1:\ WshShell.Run "runas /user:USERNAME", 0, True

Added the correct object to delete the file

2:\ Set Fso = CreateObject("Scripting.FileSystemObject")

Change the File name to a varible

3:\ File = "c:\test.txt"

'/--> Varibles 
Dim Fso, File, WshShell
'/--> Varibles As Objects
Set WshShell = Createobject("Wscript.Shell")
Set Fso = CreateObject("Scripting.FileSystemObject")
File = "c:\test.txt"
WshShell.Run "runas /user:USERNAME", 0, True
WshShell.Sendkeys "PASSWORD_OF_USER_ABOVE"
'/--> Checks For The File Then If It Exists It deletes It
If Fso.FileExists(File) Then Fso.DeleteFile(File) End If

Link to comment
Share on other sites

'/--> Varibles 
Dim Fso, File, WshShell
'/--> Varibles As Objects
Set WshShell = Createobject("Wscript.Shell")
Set Fso = CreateObject("Scripting.FileSystemObject")
File = "c:\test.txt"
WshShell.Run "runas /user:USERNAME", 0, True
WshShell.Sendkeys "PASSWORD_OF_USER_ABOVE"
'/--> Checks For The File Then If It Exists It deletes It
If Fso.FileExists(File) Then Fso.DeleteFile(File) End If

That's fantastic, thanks a million for your help :) With the script encoder from MS I can distribute the file to whoever I want and they won't see the admin password :)

I have another question: What kind of objet I have to create to empty a folder? If i replace File = "c:\test.txt" byt File = "c:\*.txt", the script does not delete the files in my folder.

Alphaz

Link to comment
Share on other sites

'/--> Varibles 
Dim Fso, File, WshShell
'/--> Varibles As Objects
Set WshShell = Createobject("Wscript.Shell")
Set Fso = CreateObject("Scripting.FileSystemObject")
File = "c:\test.txt"
WshShell.Run "runas /user:USERNAME", 0, True
WshShell.Sendkeys "PASSWORD_OF_USER_ABOVE"
'/--> Checks For The File Then If It Exists It deletes It
If Fso.FileExists(File) Then Fso.DeleteFile(File) End If

That's fantastic, thanks a million for your help :) With the script encoder from MS I can distribute the file to whoever I want and they won't see the admin password :)

I have another question: What kind of objet I have to create to empty a folder? If i replace File = "c:\test.txt" byt File = "c:\*.txt", the script does not delete the files in my folder.

Alphaz

VBS does not accept wild cards but here a script that should in theory should delete all the text files

All you have to do is make sure that Dim FolderName : FolderName = "C:\"

the Green text is the correct Drive this also can be "C:\Some_Folder"

'/-> Varible And Objects 
Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Act : Set Act = CreateObject("Wscript.Shell")
'/-> Binds The Folder Or Drive For Collection
Dim FolderName : FolderName = "C:\"
'/-> Binds To The Drive Or Folder
Dim objFolder : Set objFolder = Fso.GetFolder(FolderName)
Dim collectFiles : Set collectFiles = objFolder.Files
'/-> This is The New Varible Name For The Loop
Dim objFile
'/-> This Start A Loop That Collects All The Files In FolderName
For Each objFile in collectFiles
'/-> This Checks For The Extention txt or TXT or Txt This Is Not Case Sensitive
If InStr(objFile.Name,"txt") Then
Fso.DeleteFile(objFile.Path)
End If
Next

Link to comment
Share on other sites

shouldn't this be in the programming forum?

No since it question about Windows XP. and since a mod replied already then it is in the correct form.

You can't hide/crypt admin password in a simple .cmd file, except with use credential but you must have to enter once the password in clear.

Compiled VBS can be used I think, wait a moment for masters of vbs ^^

Link to comment
Share on other sites

Attempting to "hide" the password is futile, as I demonstrated a long time ago by recovering the password stored in a compiled AutoIt script (actually, the entire script, complete with comments and formatting) within a few minutes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...