VBScript permission denied error 800A0046 in CreateTextFile suddenly started occurring - vbscript

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!

Related

Find/Replace - Not working when called from batch file. OK if run independently

I have a question based on a previus question asked here.
I need to change registry key entries on multiple computers where REG GUIDs of said registry keys are different on every machine. I have found that I can pull the regkey into a .reg or .txt file, find the “Device_State” values, and replace them with what I want using a .vbs script similar to the one in the link I referenced above.
I cannot use any 3rd party solutions for this. It must be organic to Windows, by nature (Windows 7 and windows 10 is currently my env).
Problem
My Test.vbs script appears to work when I just have the .reg file in a directory, and run (double-click) the .vbs script (from same directory).
The script finds and replaces the strings I want and writes it all back to the .reg file (in this case, "replace.reg"). However, when I call it from my batch file, the .vbs script returns an error:
Line: 15 Char: 1 Error: Invalid procedure call or argument
Code: 800A0005 Source: Microsoft VBScrpt runtime error
line 15, 1 in my code is: objFile.Write strNewText.
When I check the .reg to see what has changed, the file is now blank. It seems like the script is reading the content of the file into the strText variable, finding my values, replacing values, then erroring out and not writing back to the .reg file.
Batch file
My batch file does the following:
creates a directory for backup and to work out of
md c:\windows\patches
reg export "hkey_name" C:\windows\patches\backup\backup.reg
exports the registry key to a .reg file (and a separate .txt for reference) for actual manipulation of file content:
reg export “hkey_name” c:\windows\patches\replace.reg
reg export "hkey_name" c:\windows\patches\replace.txt
calls the .vbs script
#call cscript “%~dp0Test.vbs”
I have also tried
runas /user:localadmin /savecred "wscript.exe c:\windows\Patches\Test.vbs"
imports the new .reg into the registry (this is the plan; have not gotten this far yet).
reg import c:\windows\patches\replace.reg
VBScript
The Test.vbs script is as follows:
Option Explicit
Dim objFSO, objFile, strText, strNewText
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpentTextFile(“C:\Windows\Patches\backup.reg”, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, ”HKEY”, ”BLAH-BLAH”) '<—– this is just for testing function!
strNewText = Replace(strText, "LOCAL", "EAT_SOME_FOOD") '<—-just for testing function!
Set objFile = objFSO.OpenTextFile("C:\Windows\Patches\backup.reg", ForWriting)
objFile.Write strNewText
objFile.Close
MsgBox "Done"
So, again, to reiterate, the VBScript does exactly what I want when I run it independently. But when I call it from a batch file, it "wipes out" (or fails to write to) the file I need changed.
I am running this as an administrator of the local system, with full rights to the files in question.
I am running this from an elevated CMD prompt/window.
Question
Does anyone know why this would work independently of the batch file, yet not work when called by the batch file? I'm thinking it has to have something to do with permissions or authority to open/close the file for reading/writing while "inside" a batch routine. I just don't know enough to figure it out.
***************************** UPDATE ************************************
I have narrowed this down even further and here is what I'm finding:
-
1) I run my batch file that creates a backup of the registry keys I need to edit. The batch file creates a .reg file and a .txt file of the same registry keys (this is just for my own security to have both). I'm also granting full rights to everyone, users, administrators on this folder/files
2) I then run the find/replace text vbScript. It errors out on the line "objFile.Write strNewText". When it errors out it appears to wipe out the contents of the .reg file.
3) I found that if I take everything in the .txt file (same exact text) and paste into the .reg file (AFTER the vbscript errors out) and THEN run my script, it works.
I have tried manually deleting the .reg contents and replacing it with the .txt content and trying to run the script, and it errors out the same way and again appears to wipe out the contents of the .reg file. It's ONLY AFTER THE VBSCRIPT errors out on writing to the file, appears to wipe out the contents, that I can paste the contents of the .txt file into the .reg file and run the VBScript ... and it works. I'm so confused by this. I can't imagine what's going on. Please, I'm hoping someone else can help me. Thanks.
There is likely an error being thrown that you can't see. In your batch file, at the end of each line, add > output.txt, like so:
md c:\windows\patches
reg export "hkey_name" C:\windows\patches\backup\backup.reg > outputExport1.txt
reg export "hkey_name" c:\windows\patches\replace.reg > outputExport2.txt
reg export "hkey_name" c:\windows\patches\replace.txt > outputExport3.txt
cscript "%~dp0Test.vbs" > outputVBS.txt
This will dump any error information you would have seen in the command prompt to the output file. Alternatively, you can manually run each line of your batch file in a command prompt and see what it says there.
Inside your script, you can add additional debugging outputs (and they will also either be dumped into your output file or display in the command prompt if you run it manually):
Option Explicit
Dim objFSO, objFile, strText, strNewText
Const ForReading = 1
Const ForWriting = 2
WScript.Echo "Reading the file"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpentTextFile("C:\Windows\Patches\backup.reg", ForReading)
strText = objFile.ReadAll
WScript.Echo "File Contents: " & strText
objFile.Close
strNewText = Replace(strText, "HKEY", "BLAH-BLAH") '<—– this is just for testing function!
WScript.Echo "Did first replace: " & strNewText
strNewText = Replace(strNewText, "LOCAL", "EAT_SOME_FOOD") '<—-just for testing function!
WScript.Echo "Did second replace: " & strNewText
Set objFile = objFSO.OpenTextFile("C:\Windows\Patches\backup.reg", ForWriting)
WScript.Echo "Opened the File for Writing"
objFile.Write strNewText
WScript.Echo "Wrote the File"
objFile.Close
WScript.Echo "Closed the File"
WScript.Echo "Done"
Note that in the code above I corrected a bug you have in the second Replace - in the original code you are reusing the strText variable, which means the changes done by the first Replace are overwritten.

Having issues Simple VBScript to check if file exists, then copies if not

First off I am not a coder at all so I am probably missing something very basic.
I am writing a script that will run via GPO when our users login to some of our terminal servers to check if a specific file exists in the users home folder, if it does it should exit, if not then it should copy an .ini file from another location. Here is the script I have that is failing.
On Error Resume Next
dim shell
set shell=createobject("wscript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\%USERPROFILE%\AppData\Roaming\Microsoft Dynamics SL\solomon.ini") Then
Wscript.Quit
Else
objFSO.CopyFile "\\Server\sharepath\file.ini" , "C:\%USERPROFILE%\AppData\Roaming\Microsoft Dynamics SL\"
Wscript.Echo "File Copied."
End If
It just displays the File Copied echo when I run the script, no file is copied if the file is not present, and if the file is there it still ticks past it and displays the file copied text.
Edit: Tried to clean up the way it is displayed here on the forum.
Try this:
On Error Resume Next
Dim strAppDataPath
strAppDataPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%appdata%")
With CreateObject("Scripting.FileSystemObject")
If .FileExists(strAppDataPath & "\Microsoft Dynamics SL\solomon.ini") Then
Wscript.Quit
Else
.CopyFile "\\Server\sharepath\file.ini", strAppDataPath & "\Microsoft Dynamics SL\"
Wscript.Echo "File Copied."
End If
End With
you cannot use system environment variables within the VB Strings.
expand them beforehand as specified by omegastripes and use concatenate operator to append them to your path.

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.

Window Script Host Permission denied

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

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