Drive Letters Usage with the FileSystemObject - VBScript - scripting

I am migrating from a Win 2003 server to a Win 2008 R2 server. I'm running a classic ASP application. In the 2003 server the following code worked for creating a file:
CONST SCRATCH_DIR = "E:\Temp\"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TFL = FSO.CreateTextFile(SCRATCH_DIR & "Debug.txt", True)
On the NEW server the Set TFL line gives me this error:
Microsoft VBScript runtime error '800a0046'
Permission denied
/inc/vbutil.inc, line 110
If I remove the Drive designation "E:\" from the CONST, the procedure works just fine; (except it writes to the C:\ drive) so I'm sure the error message is correct.
Could anyone please point me at where/how I'd change permissions for this (E:\Temp) directory?
Thanks very much in advance,
(A dumb application programmer;)

You can change permissions on the folder by right clicking it > Properties > Security > Advanced.
Uncheck "Include inheritable permissions from this object's parent".
Click "Add"
Modify permissions for the user running the script. They will need read, write and create permissions.

Related

Able to write file but not read from a network location

I have following network location
Dim myfolder As String = "\\10.0.0.90\myfolder\"
I am able to create a new file in this folder using following code:
File.Create (myfolder)
But when I try to read contents of this folder using code below I get error.
Code
Dim orderedFiles = New System.IO.DirectoryInfo(myfolder).GetFiles()
Error
The system detected a possible attempt to compromise security. Please
ensure that you can contact the server that authenticated you.
File writing is being done by ASP.Net page while reading is done from Windows Service. Could this be the issue?
Windows Service was running as "Local System". I right click on it, went into properties and changed the "Log on as" to some user account and now it can access network folder.

Complex sql query on BLK insert

I am using following query to load data from text file into database table
bulk insert Test_Training.dbo.test
from 'D:\SSRS\kasthuri.txt'
I have kasthuri.txt file in specified path. But I am getting error when I execute it.
Msg 4861, Level 16, State 1, Line 2
Cannot bulk load because the file "D:\SSRS\kasthuri.txt" could not be opened. Operating system error code 3(The system cannot find the path specified.).
The error message is because the service running your SQL server instance cannot access the file path. Wherever you place the file, you will need to open up the folder where the file resides to the MSSQLSERVER agent:
**I always struggle with this. For me, allowing MSSQLSERVER full permission to the folder where the input file resides always seems to work.*
Right-click the folder (as an admin on the box), go to properties, security, edit, add...
here is where I always get tripped up. For me, the server service account is "NT Service\MSSQLSERVER" and I can never search for that user. I have to type it in manually and check the name to make sure I typed it in correctly. For you, this may not be the service account used by your server. Check your services list from windows administrative tools to see what account is in "Log On As" for SQL Server (MSSQLSERVER).*

SQL Server Operating system error 5: "5(Access is denied.)"

I am starting to learn SQL and I have a book that provides a database to work on. These files below are in the directory but the problem is that when I run the query, it gives me this error:
Msg 5120, Level 16, State 101, Line 1 Unable to open the physical file
"C:\Murach\SQL Server 2008\Databases\AP.mdf". Operating system error
5: "5(Access is denied.)".
CREATE DATABASE AP
ON PRIMARY (FILENAME = 'C:\Murach\SQL Server 2008\Databases\AP.mdf')
LOG ON (FILENAME = 'C:\Murach\SQL Server 2008\Databases\AP_log.ldf')
FOR ATTACH
GO
In the book the author says it should work, but it is not working in my case. I searched but I do not know exactly what the problem is, so I posted this question.
SQL Server database engine service account must have permissions to read/write in the new folder.
Check out this
To fix, I did the following:
Added the Administrators Group to the file security permissions with
full control for the Data file (S:) and the Log File (T:).
Attached the database and it works fine.
An old post, but here is a step by step that worked for SQL Server 2014 running under windows 7:
Control Panel ->
System and Security ->
Administrative Tools ->
Services ->
Double Click SQL Server (SQLEXPRESS) -> right click, Properties
Select Log On Tab
Select "Local System Account" (the default was some obtuse Windows System account)
-> OK
right click, Stop
right click, Start
Voilá !
I think setting the logon account may have been an option in the installation, but if so it was not the default, and was easy to miss if you were not already aware of this issue.
To get around the access denied issue, I started SSMS as administrator and that allowed me to attach a database from my local drive. The database was created in another SQL and windows instance.
This is Windows related issue where SQL Server does not have the appropriate permission to the folder that contains .bak file and hence this error.
The easiest work around is to copy your .bak file to default SQL backup location which has all the necessary permissions. You do not need to fiddle with anything else. In SQL SERVER 2012, this location is
D:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup (SQL 2012)
C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup (SQL 2014)
C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\Backup (SQL 2016)
I had this problem. Just run SQL Server as administrator
Yes,It's right.The first you should find out your service account of sqlserver,you can see it in Task Manager when you press ctrl+alt+delete at the same time;Then,you must give the read/write privilege of "C:\Murach\SQL Server 2008\Databases" to the service account.
I solve this problem by adding Full control permission for both .mdf and .ldf files for Users group.
The problem is due to lack of permissions for SQL Server to access the mdf & ldf files. All these procedures will work :
you can directly change the MSSQLSERVER service startup user account, with the user account who have better privileges on the files. Then try to attach the database.
Or you can assign the user to the file in security tab of the mdf & ldf files properties with read and and write privileges checked.
Startup with windows administrator account, and open SQL Server with run as administrator option and try to login with windows authentication and now try to attach the database.
For me it was solved in the following way with SQL Server Management studio
-Log in as admin (I logged in as windows authentication)
-Attach the mdf file (right click Database | attach | Add )
-Log out as admin
-Log in as normal user
The actual server permissions will not matter at this point; all looks ok.
SQL Server itself needs folder permissions.
depending on your version, you can add SERVERNAME$MSSQLSERVER permissions to touch your folder. Othewise, it has to be in the default BACKUP directory (either where you installed it or default to c:\programfiles(x)\MSSQL\BACKUP.
Even if you do the following steps you COULD get the same error message.
1. login as SA user (SSMS)
2. Edit the file permissions to say "everyone" full access (windows folder)
3. Delete the Log file (Windows Exploring (this was what I had done per advise from some msdn forum)
I still GOT the permission error, but then I noticed that in the Attach screen, the bottom section STILL showed the LOG file, and the error message remained the same.
Hope this helps someone who did the same thing.
It means the SSMS login user does not have permission on the .mdf file. This is how it has worked for me:
I had opened the SSMS (Run as administrator) and login as an administrator user, database right-click attach, click add, select the .mdf file, click Ok. Done.
I had this issue when I try to backup a database.
System.Data.SqlClient.SqlError:
Cannot open backup device 'C:\x\x\xxx.bak'.
Operating system error 5 (Access is denied.). (Microsoft.SqlServer.Smo)
When I had this issue I thought that the user which I'm connecting to database don't have rights to access to the backup location. I gave full control to that user but nothing changed. This is because the service for SQL Server is running with another user.
At this point you may choose changing user of the service to local system account or add access rights for the current user of the service.
I choose the 2nd one.
After that change backup succeeded.
Very Simple Solution.
Login with System admin
copy your mdf and ldf files in "C:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA" Where all other data file recides.
Now attach from there it will work
I used Entity framework in my application and had this problem,I seted any permission in folders and windows services and not work,
after that I start my application as administrator (right click in exe file and select "run as admin") and that works fine.
If you get this error on an .MDF file in the APP_DATA folder (or where ever you put it) for a Visual Studio project, the way I did it was to simply copy permissions from the existing DATA folder here (I'm using SQL Express 2014 to support an older app):
C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS2014\MSSQL\DATA
(note: your actual install path my vary - especially if your instance name is different)
Double click on theDATA folder first as administrator to make sure you have access, then open the properties on the folder and mimic the same for the APP_DATA folder. In my case the missing user was MSSQL$SQLEXPRESS2014 (because I named the instance SQLEXPRESS2014 - yours may be different). That also happens to be the SQL Server service username.
For some reason, setting all the correct permissions did not help in my case. I had a file db.bak that I was not able to restore due to the 5(Access is denied.) error. The file was placed in the same folder as several other backup files and all the permissions were identical to other files. I was able to restore all the other files except this db.bak file. I even tried to change the SQL Server service log on user — still the same result. I've tried copying the file with no effect.
Then I attempted to just create an identical file by executing
type db.bak > db2.bak
instead of copying the file. And voila it worked! db2.bak restored successfully.
I suspect that some other problems with reading the backup file may be erroniously reported as 5(Access is denied.) by MS SQL.
In linux, I went to /var/opt/mssql/data/ folder and opened a terminal with sudo then, changed my *.mdf and *.ldf file permissions as below in which you replace yourDB with your Database file name and myUser to currently logged username:
chmod 755 yourDB.mdf
chown myUser yourDB.mdf
chmod 755 yourDB.ldf
chown myUser yourDB.ldf
After that, it was reconnected without any issue.
If the database you are trying to attach is compressed it may show error message.
First you have to decompress the file. For that go to properties of mdf/ldf file >> then "Advanced" >> Uncheck "Compress Contents to save disk space" >> Press "Apply".
After that give it a try.
Run SQL Server management studio as Administrator
Log in as Windows user
Remove log file if you have only MDF file (haven't log file)
With 3 items in the above checklist, you will remove almost problems related with attach database task.
If you're using Storage Gateway - SMB (S3)
Do this from the management studio
EXEC xp_cmdshell 'net use X: \100.155.16.6\mystoragegatewayfolder xxmysuperpassxx /user:sgw-445577\smbguest /persistent:yes /y'
EXEC XP_CMDSHELL 'Dir X:' (this should show you the Directory info, serial no etc)
Mount the drive (This PC - > Mount network drive, using the same info above)
Test run a backup job using scheduled backup, this will force to use the sql server agent and you can see where it's writing and whats the issue if any.

Cannot attach database SQL Server 2012 error 5

When I type this:
CREATE DATABASE AdventureWorks2012
ON (FILENAME = 'C:\Users\sohm\Desktop\Pohm\AdventureWorks2012_Data.mdf')
FOR ATTACH_REBUILD_LOG ;
I get this:
File activation failure. The physical file name
"C:\Users\sohm\Desktop\AdventureWorks2012_Data_log.ldf" may be incorrect.
Msg 5123, Level 16, State 1, Line 1
CREATE FILE encountered operating system error 5 (Access is denied.) while attempting to
open or create the physical file 'C:\adventuredb\AdventureWorks2012_Data_log.ldf'.
Msg 1813, Level 16, State 2, Line 1
Could not open new database 'AdventureWorks2012'. CREATE DATABASE is aborted.
I know it has to do with permissions. I have the administrator account. I have read a million posts but nothing has worked so far. Also when I try to change the folder containing the mdf file, to full control but it keeps reverting back to read-only.
You should run SSMS as an administrator.
Also it is possible that you might be running query as an 'sa' user(sql server authentication).
Try running the query with windows Authentication.
SHORT VERSION
For allowing MS SQL server 2012 to attach the database files you must grant full access to the mdf and ldf files to:
NT Service\MSSQLSERVER
Full Access
LONG VERSION
SQL Security has been updated a lot in 2012 handling of files.
http://technet.microsoft.com/en-us/library/ms189128.aspx
If you look at the existing attached databases and there security you will probably see that there only are 2 roles allowed with full access. MSSQLSERVER and Administrators, when attached SQL Removes all other roles from the files.
Trying to set the MSSQLSERVER from the fil security Permission will not work.
Looking at your services.msc overview you will in a default install see that NT Service\MSSQLSERVER also runs the SQL server servics.
The service account is not visible, the same issue as with IIS AppPools, a sub group has been created by the system and "hides" the accounts from auto discovery, probably to try and secure the accounts but in my view just obscures the setup.
Regards Jan
Open the SQL Server Management Stdio in Administrator mode(Right click -> Select Run as Administrator. If it asks for User name and password enter your Administrator user name password). Then select Windows Authentication click ok, and now try to attache it will attache without any error.
This has nothing to do with YOUR account. SQL Server is trying to write to that folder under the context of the service account that is running SQL Server. You can see who this is by going to the Services applet and seeing who the instance runs as (probably NT AUTHORITY\SYSTEM or whatever). You can override that or you can place your MDF files in the data folder, which SQL Server already has inherent access to, instead of placing them in this other location on your C:\ drive.
Another way to solve this is to change all the SQL Server services to Local System Account instead of services. Start -> Run ->Services.msc -> Scroll down to SQL Server, you should see five services that start with SQL Server. Right-Click, Properties, Log On, change to Local System Account. Repeat for all the rest of the SQL Server Services and restart them all.
Try This
Go to the folder where you have stored mdf file.
Select file -> Right click -> click on mdf give full permissions to file for logged in user Security.
If still issue persist and it might me because of corrupted transaction logs, you may use tool SQL Database Recovery Tool Repairs to repair your corrupted mdf file.
Figured it out. I went to sql server configuration manager and right clicked SQL Server Service. Then I added my Windows Admin account to run the service and now it works.

Word Automation : could not open macro storage

My application (vb.net windows application deployed via ClickOnce) uses Word to open and fill .dot templates to create new Word documents. I reference Microsoft Word 14 Object Library and uses this code :
Dim oWord As Word.Application = Nothing
Dim oDoc As Word.Document = Nothing
Try
oWord = New Word.Application
Dim strFileName As String = ""
Select Case strType
Case "LettreReception"
strFileName = Path.Combine(GetParam(1), "Template_LettreReception.dot")
If File.Exists(strFileName) Then
oDoc = oWord.Documents.Add(strFileName)
On the last line I receive "could not open macro storage" error on deployed machines (not on my development machine).
I develop with Windows 7 - Office 2010 - VS 2010 (.Net 3.5). My deployment machine is also a Windows 7 with Office 2010 installed.
I tried to remove normal.dotm (I found some links advicing it) without success. The .dot template used contains no macro.
Check the properties of the word document and make sure the files are unblocked. Sometimes when you get the documents from a different computer or download them from the internet they will be blocked which will cause the the throwing of this exception "could not open macro storage"
Because Word Interop is actually running behind the scenes as if it was running in an interactive session, certain permissions are required of the account used during execution.
Are you using Windows Authentication and impersonation in you web app? If so, the user being impersonated must have local log on rights to the server to run Word... In addition, you must actually log on to the server with that account at least once so that a profile exists on that machine for that user so the registry hive can be loaded. I've also found that you may need to actually run Word at least once as that user (to make sure any first-time initialization messages get taken care of before trying to run Word from code).
If not, then the service account that the application is running under (usually NETWORK SERVICE) requires the aforementioned permissions (which I will describe shortly) and you'll have to do something fancy like loading a registry hive dynamically at run-time. Personally, I prefer implementing an in-code temporary impersonation with a user account that has local log on permissions on the server in question.
Local log on permissions can be a bit tricky depending on your network and group policy configurations (if you want to be somewhat secure and not just use a Domain Admin account).
The reason everything works on your computer running in VS is because the context of the web application is YOUR user account - which, of course, has local log on permissions on your machine with a registry hive that can be loaded.
Now for the permissions:
First, you must run "dcomcnfg" on the server and make the following configuration change:
Right click on Component Services\computers\My Computer\DCOM Config\Microsoft Word 97 - 2003 Document and go to Properties
In the Properties screen, go to the Security tab and change the "Launch and Activation Permissions" to Customize.
Click the Edit button and add the local computer NETWORK SERVICE account (If not using impersonation... If using impersonation, add the appropriate user or group) to the list of users and check "Local Launch" and "Local Activation"
Make sure that the local computer NETWORK SERVICE account (If not using impersonation... If using impersonation, then the appropriate user or group) has appropriate read/modify permissions on the folder(s)and file(s) that you will be opening and/or saving to.
Create a "Desktop" directory under: C:\Windows\System32\config\systemprofile\ and give Full permissions to the local NETWORK SERVICE account (or the account that your ASP .NET application is running under) [NOTE: I believe this and the next step only apply if NOT using impersonation]
Give Modify/Read/Execute permissions on the C:\Windows\System32\config\systemprofile folder
Hope that helps some and wasn't too confusing...
Right click on the file that is opened -> Click the Unblock tick box -> Apply
Worked for me at least.
"could not open macro storage" is telling you that VBA is looking for a particular structured storage file such as a .DOT or .DOC, and looking for the storage (a kind of stream within the file) in that file that contains the VBA code. If it can't open it, possible reasons include:
the container (the .doc/.dot) isn't there
the container cannot be opened with the caller's permissions
the container is there but the storage isn't there (e.g. on the target system there is a container with the expected name, but it contains no macros)
the container is there and the storage is there but cannot be opened with the caller's permissions
So one thing to do is to look through your project looking for anything it references (perhaps even other objects or DLLs that you specified via Tools->References) that is not also being delivered with your template.
Go to the Word document (if it's a template, be sure to open it, not create a new document with it) and disable Protected View:
https://casecomplete.zendesk.com/hc/en-us/articles/200685047-Could-not-open-macro-storage