Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Permission Denied 800A0046

Status
Not open for further replies.

tekmbrad

Technical User
Aug 18, 2011
9
0
0
US
Thanks for taking the time to read my question.

I have been searching for a solution, but have not found one online.

I have a script that copies a folder from a PC to the server. It works well on my laptop if I'm logged on as me and on the local domain. The problem I'm up against is the PC that I want to run it on will be logged on to the local machine, and not on to the domain. I tested this on my laptop and it fails when I log on to the local machine instead of on to our network.



Is there a way I can put my credentials and domain information into the script so that it can authenticate and then complete the copy?

Code:
Code:
'Get the Computer Name
set shell = WScript.CreateObject( "WScript.Shell" )
computername = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
wscript.echo computername


'Make sure destination folder exists and if not make it
dim filesys, newfolder, newfolderpath 
newfolderpath = "\\caws0059\site data\LSTMBackUp\" & computername & "\" 
wscript.echo newfolderpath
set filesys=CreateObject("Scripting.FileSystemObject") 
If Not filesys.FolderExists(newfolderpath) Then 
   '***Code errors out on the next line***
   Set newfolder = filesys.CreateFolder(newfolderpath) 
   wscript.echo "A new folder has been created at: " & newfolderpath 
Else
	wscript.echo "It exists"
End If



'Copy Folders
dim filesys1
set filesys1=CreateObject("Scripting.FileSystemObject")
If filesys1.FolderExists("c:\P1464") Then
   filesys1.CopyFolder "c:\P1464", "\\caws0059\site data\LSTMBackUp\" & computername & "\"
End If


'Confirm Copy completed
If filesys.FolderExists(newfolderpath) Then 
	wscript.echo "Copy compete"
Else
	wscript.echo "Copy did not compete"
End If

Thanks,
Brad
 
This has been a thorn in my side as well when running configuration scripts on thin clients. There are two options that I am aware of.

1) Open a network path prior to running the script. This will force you to enter your credentials.

2) Run the script as you. Open the Run box and enter "runas /user:domain\username C:\path\to\script.vbs". When prompted, enter your password.

To automate, make a parent script to run the runas command and echo the password when prompted.

-Geates



"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Excellent suggestions. I'll see if I can make them work.

Thanks so much for the very quick reply.

Have a great day,

Brad
 
Connecting to a sever using WMI should provide the authentications you need to run the script as it satisfies my first suggestion.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
The only issue is that the site where I need the code run is 13 hrs drive away, and I don't want to provide my network creds for the folks to type in. I just want to put the script on the PC, get the local users to run it then delete it.

Plus when you do option one, the creds are saved until the PC is restarted. I don't want the users to fish around on the server.

I found this, but can't seem to get it to work.
Here is what I have:
Code:
Option explicit
Dim oShell
set oShell= Wscript.CreateObject("WScript.Shell")
'Replace the path with the program you wish to run c:\program files...
oShell.Run "RunAs /noprofile /user:abcdef""cscript.exe C:\Documents and Settings\bmcintyr\My Documents\Scripts\CopyMFiles.vbs"
WScript.Sleep 100
WScript.echo "Here"
'Replace the string --> yourpassword~ with the
'password used on your system. Include the tilde "~"
oShell.Sendkeys "123456ABc~"
WScript.echo "Here2"
Wscript.Quit
 
Ah so. Good call.

It might just be a typo, but you seemed to forget the closing quotes

Code:
oShell.Run "RunAs /noprofile /user:abcdef ""cscript.exe C:\Documents and Settings\bmcintyr\My Documents\Scripts\CopyMFiles.vbs[red]""[/red]"

-Geates

NOTE: The user can easily right click the script and choose edit - exposing your credentials. Bury the script somewhere and add a reg key to launch the script upon logon.

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
That doesn't quite make sense to me...

The first open quote is on "RunAs
The close to that quote is after abcdef "

The next open quote is on "cscript.exe
The close to that quote is after .vbs"

Why do I need the extra 2 at the end?

Thanks again for your help and direction I really appreciate it.
Brad
 
A quote is an escape character. If you want to use a quote within a quotation, it must be escaped. Similar to a '\' in PHP.

Code:
oShell.Run [blue]"RunAs /noprofile /user:abcdef [red]""cscript.exe C:\Documents and Settings\bmcintyr\My Documents\Scripts\CopyMFiles.vbs""[/red]"[/blue]

- Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
oooohhhh I get it. I stared at it for a while. It was kind of like those pictures that you have to stare at until the hidden picture comes out.

Thanks.

Brad
 
I'm still doing testing to get it to work

This works:
Code:
oShell.Run "cscript.exe ""C:\Documents and Settings\bmcintyr\My Documents\Scripts\CopyMarcotteFiles.vbs"""

but this won't
Code:
oShell.Run "RunAs /noprofile /user:bmcintyr ""cscript.exe ""C:\Documents and Settings\bmcintyr\My Documents\Scripts\CopyMarcotteFiles.vbs"""""

There must be something wrong with my RunAs.

Thoughts?
 
because the "" after cscript.exe closes the "" before it.

Code:
oShell.Run [blue]"RunAs /noprofile /user:bmcintyr [red]""cscript.exe ""[/red]C:\Documents and Settings\bmcintyr\My Documents\Scripts\CopyMarcotteFiles.vbs""[/blue][green]"""[/green]

Take cscript out altogether and add ,0 to your run command. This will hide the window.

Code:
oShell.Run [blue]"RunAs /noprofile /user:bmcintyr [red]""C:\Documents and Settings\bmcintyr\My Documents\Scripts\CopyMarcotteFiles.vbs""[/red]"[/blue], 0

-Geates



"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
if you ever get confused with escaping quotes, wscript.echo the string. It will clearly show if they are escaped correctly.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thank you for your help. I really appreciate how you put the colors in the code examples to show me what I'm doing. That really helps.

I copied your code into my script and nothing happened. I have some WScript.echo's in the second script that is being called and they don't pop up. When I mentioned above that I got it to work without the RunAs, those messages pop up. For some reason with the RunAs, the second script does not fire.

To your idea of taking out the cscript.exe, in the scriping guy page they say the RunAs only works on an .exe that's why you need the cscript.exe or wscript.exe in there.

I'll keep trying to figure this one out.

Thanks again for your help and ideas.

Brad
 
I forgot. Yes, that is true. Apparently you can escape quote in a RunAs command with \.

Code:
oShell.Run [blue]"RunAs /noprofile /user:bmcintyr [red]""cscript.exe [green]\""C:\Documents and Settings\bmcintyr\My Documents\Scripts\CopyMarcotteFiles.vbs\""[/green]""[/red]"[/blue]

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Or is the \ just for DOS? in that article it says to do the RunAs in the Start - Run, or at a command prompt.

Brad
 
it works in the Start->Run box as well. Remove all single double quotes and change all double double quotes to single double.

RunAs /noprofile /user:bmcintyr "cscript.exe \"C:\Documents and Settings\bmcintyr\My Documents\Scripts\CopyMarcotteFiles.vbs\""

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top