Window Script Host Permission denied - vbscript

Running this very simple VBS script on a Win7 Professional 64 bit and getting a Permission
denied (800A0046) error message.
Is there a way to change the OS permissions to allow for that?
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "C:\Users\Desktop\Source\1.txt", "C:\Users\Desktop\Destination"
FSO.Close

Related

VBScript permission denied error 800A0046 in CreateTextFile suddenly started occurring

I have been using a particular VBScript for a long time and it suddenly broke today with a permission denied error. The line number referenced as the error points to a call to CreateTextFile as the problem. I have simplified the script to 4 lines and still get the error. Here is the simplified 4 line script:
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFS.CreateTextFile("test.txt", True)
objOutputFile.WriteLine("test")
objOutputFile.Close
When I run this script I get:
Line: 2
Char: 1
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
This is on a stand-alone Windows 10 PC, and I have changed nothing to do with user or file system permissions on my computer and have not modified the script prior to its breaking.
Just for grins, I verified that System, Administrators, and my user ID all have Full, Modify, Read & execute, List folder contents, Read, and Write permissions. That all looks good. I can copy files, delete files, open notepad and create or edit files, etc., so it doesn't seem to be a permissions problem.
It only seems to affect writing to my user profile. For example, if I run the following code, it fails in line 5, not line 2:
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFS.CreateTextFile("C:\Temp\test.txt", True)
objOutputFile.WriteLine("test")
objOutputFile.Close
Set objOutputFile = objFS.CreateTextFile("C:\Users\Username\Documents\Test\test.txt", True)
objOutputFile.WriteLine("test")
objOutputFile.Close
I can work around the problem by doing the following, but I should really not have to do this:
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFS.CreateTextFile("C:\Temp\test.txt", True)
objOutputFile.WriteLine("test")
objOutputFile.Close
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.run "cmd.exe /C copy ""C:\Temp\test.txt"" ""C:\Users\Username\Documents\Test\test.txt"" "
What gives? Has Microsoft changed something that broke writing files with VBScript?
Thanks!

Failed to write to file error - ADODB.stream

I want to download files from the internet with a .vbs file. I got the code here: Is it possible to download using the Windows command line?
It uses ADODB.stream to extract the file to my C drive. I ran it on Windows XP SP3 and it worked fine. Then when I tried to run it on Windows 10 I got a "could not write to file" error. I also tried it in CMD with "cscript.exe download.vbs" and the same thing happened. What is the difference between Windows XP and Windows 10 that makes this error? Can it be fixed?
Here is the exact code:
strFileURL = "http://www.it1.net/images/it1_logo2.jpg"
strHDLocation = "c:\logo.jpg"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
While this might appear to be a programming issue it is in fact related directly to the Operating System. The change you've encountered between Windows XP and Windows 10 is known as User Account Control (first introduced with Windows Vista).
As stated in How do I get permissions to save in a folder that Windows 7 denies me from?
This is due to UAC (User Access/Account Control) On Windows Vista / 7, and trying to write to a protected folder.
In Windows 10 the root of the system drive (usually the drive where the OS is installed) is classed as a Protected Folder.
Accessing Protected Folders requires elevated privileges which when running VBScript through the Windows Scripting Host can be done by starting the command prompt in Administrator Mode (available from the program context menu) and calling the wscript.exe or csscript.exe with the required script path.

Permission denied when calling VBScript Scripting.FileSystemObject Folder.size

I seem to have a peculiar issue when I call the folder.size of a folder that I have modify rights to. The files and folders with in the parent folder have inherit modify permissions. Yet when I call the size on the folder object I get a permission denied error.
Set mobjFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set objTargetFolder = mobjFileSystemObject.GetFolder("D:\data")
Wscript.Echo(objTargetFolder) 'Prints the folder path "D:\data"
Wscript.Echo("size: " & objTargetFolder.Size) 'Permission denied runtime error
Under special permissions, I have read access to the attributes and Read extend Attributes.
Note: I'm running the script on a Windows 2003 server.

Windows Server 2012 VBScript gets permission denied deleting files

I have a folder on my server (Windows Server 2012 r2) that is shared with Everyone. All my users upload a plain text inventory file each day. I then have a vbscript that I scheduled to run under my Administrator credentials which deletes these files after reading them. However, I get permission denied errors when trying to delete or move the files.
Here is some simple code that fails:
Dim PathtoInventoryFiles
Dim myFSO
Dim myFile
Dim colFiles
PathtoInventoryFiles = "D:\Inventory$"
Set myFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = myFSO.GetFolder(PathtoInventoryFiles).Files
For Each myFile In colFiles
wscript.echo myFile.path
'Tried both of the following (only one at a time of course)
myFSO.DeleteFile myFile.Path 'Permission denied
myFile.Delete 'Permission denied
Next
Set myFSO = Nothing
Set colFiles = Nothing
The echo produces a correct path to a good and existing file. So I'm thinking I have a permissions issue? This is a pretty plain vanilla installation of Server 2012.
Getting a "permission denied" error on a file where the permissions already allow access usually means that the file is (still) opened by someone/something. You can check that with net file for remote connections, or handle for local processes.

Cannot write to proper place in Windows registry

I use following code to create Windows registry entry.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKLM\Software\NewsReader\ConnectionString1", "Server=myServerName3\myInstanceName1;Database=myDataBase1;User Id=myUsername1;Password=myPassword1;", "REG_SZ"
Set WshShell = Nothing
Somehow it is writing in a wrong place.
HKEY_USERS\S-1-5-21-3289046681-1693842953-402210132-1123\Software\Classes\VirtualStore\MACHINE\SOFTWARE\NewsReader
I execute that script under admin domain account and also that account has Admin privileges locally.
What do I am missing here?
P.S. I found this Why is registry written in different location than expected? but it does not clear how I have to change my code...
Even though the account has admin privileges, the script must still explicitly elevate privileges due to UAC. See http://www.server-world.info/en/note?os=Other&p=vbs for some ideas on how to do this.
I found that is a correct behavior of the Windows.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa965884(v=vs.85).aspx
And I checked it with another code. So I could read proper value.
Dim WSHShell, value
On Error Resume Next
Set WSHShell = CreateObject("WScript.Shell")
value = WSHShell.RegRead("HKLM\Software\NewsReader\ConnectionString1")
If err.number <> 0 Then
MsgBox("Error")
Else
MsgBox(value)
End If
Set WSHShell = nothing

Resources