Jump to content
Sign in to follow this  

Automating Admin tasks

Recommended Posts


Hi ,

I'm quite new to autoit scripts however i played with the codes a little bit to try getting some of admin tasks automated in exe file that i could deploy to users machines.

The following code should map a network drive and run a vbs script to defrag the C: of the machine , using admin credentials.

Whats is confusing me is that the drive is being mapped but when i do "net use" it comes up as unavaiable, this only happens when inside the execution of this autoit file , if i go to cmd and do net use J: \\myserver\scripts it does map it properly and it becomes available.

Having the mapped drive unavailable stops me from running the script i want because it won't find it.

any help would be very welcome.


Local $sUserName = "administrator"

Local $sPassword = "mypassword"

Local $sDomain = "mydomain"

Run(@ComSpec&" /c "& 'net use J: \\myserver\scripts', "", @SW_HIDE)

Local $pid = RunAsWait($sUserName, $sDomain , $sPassword, 1,"CSCRIPT J:\XDefrag.vbs /L", @ComSpec)

;Wait for the process to close.


MsgBox(0, "Defrag Finished?",$pid)

Run(@ComSpec & " /c " & 'net use J: /delete', "", @SW_HIDE)

MsgBox(0, "Process Finished", "J Drive unmapped");


Share this post

Link to post
Share on other sites

Run(@ComSpec&" /c "& 'net use J: \\myserver\scripts', "", @SW_HIDE)

I think your problem is most likely that the NET USE command will be asking for a username and password, but of course you never see its console window. You can add ' /user:[username] [password]' to the commandline.

Alternatives are to use the DriveMapAdd() command which is the Autoit-native method, or to:

Use RunAsSet to determine the credentials the process will run under, and then use a UNC path to access the server. If the local credentials match a server-account there should be no need to repeat them.

Share this post

Link to post
Share on other sites

Took me quite a while to find the answer , probably because I'm a newbie but I finally resolved the issue just by doing the command:

Local $pid1 = RunAsWait($sUserName,$sDomain,$sPassword,1, @COMSPEC & " /c \\myserver\scripts\defrag.vbs",@WindowsDir,@SW_HIDE)


Don't even need to map the drive

Of course there is a shared drive called \\myserver\scrpits

Thanks for the attention

Share this post

Link to post
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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.