I would like to launch a MS Access macro from the command line (it's being run from another tool) to generate an output. Thing is, I'd prefer it if it could supress access from loading it's interface. I just want it to run the macro and shut down. Any thoughts on how to do this?
I've used a command line something like this so far:
"C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS" "S:\filepath\Database\database.accdb" /x Macro1
You can use COM automation from VBScript to create an Access application instance, hide it from view, run your macro, and then shut down Access.
I saved the following script as RunAccessMacro.vbs and ran it in a command prompt window using this as the command line ...
cscript //NoLogo C:\share\vbscript\RunAccessMacro.vbs
This is RunAccessMacro.vbs ...
Option Explicit
Const cstrDbFile = "C:\share\Access\database1.mdb"
Dim accessApp
Set accessApp = CreateObject("Access.Application")
accessApp.Visible = False
accessApp.OpenCurrentDataBase cstrDbFile, False ' open shared
accessApp.DoCmd.RunMacro "Macro1"
accessApp.Quit
Set accessApp = Nothing
Related
I want to launch a particular URL in chrome every day at the same time. How can I achieve this using command line?
There are two ways to do this. The first being from a single command line. You will want to create a new task in Task Scheduler and have it run a command at a desired time. If you wish to use a different browser that supports command lines, be sure to use _ for any spaces in the directory path.
Command line:
SchTasks /Create /SC DAILY /TN "New_Task" /TR "start "C:\Program_Files_(x86)\Google\Chrome\Application\chrome.exe" www.stackoverflow.com" /ST 09:00
The second option is to use a batch file you store somewhere and it will be called to using the SchTasks. Simply create the site1.bat and place it somewhere safe example C:\Windows\My Tasks.
From Batch:
#ECHO OFF
#set Task_Name=Task1
#Set Time=09:00
#set Site=www.stackoverflow.com
schtasks /query /TN "%Task_Name%" >NUL 2>&1
IF %ERRORLEVEL% EQU 0 (goto :EXISTS) ELSE (goto :CREATE)
:CREATE
Set Folder=%~dp0
Set Name=%~nx0
Echo Task does not already exist, creating it now.
SchTasks /Create /SC DAILY /TN "%Task_Name%" /TR "%Folder%%Name%" /ST %Time%
goto :eof
:EXISTS
start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %Site%
goto :eof
I try to share a folder in powershell with net share command, i can't use group or user name for share permission because this script will be used on the systems with different os languages, for this reason i use group/user SID to set up share permissions.
Here is my script, my function work great outside of command. But my function dont work in "NET SHARE" cmd.
function Get-GroupName {
param ($SID)
$objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
$objUser = $objSID.Translate([System.Security.Principal.NTAccount])
$objUser.Value
}
# Share Folder + Set Share Permission SID Based
cmd /c net share MSI=C:\MSI /GRANT:(Get-GroupName -SID 'S-1-1-0'),READ
This will work:
cmd /c $( "net share MSI=C:\MSI /GRANT:""$(Get-GroupName -SID 'S-1-1-0')"",READ" )
But if you're on Windows 8 or newer, Windows Server 2012 or newer you can use the Set-SmbShare and Grant-SmbShareAccess cmdlets instead:
http://technet.microsoft.com/en-us/library/jj635727
http://technet.microsoft.com/en-us/library/jj635705
I have a script that I need to execute using a batch file. Do I use SQLCMD in the batch file to run the script? Also, the script inserts data to a table in a database. How should I format the SQLCMD in the batch file so it knows what database it is suppose to work with?
First, save your query into an sql text file (text file with .sql extension). Make sure to add the USE statement at the beginning, which tells the server which database you want to work with. Using the example from MSDN:
USE AdventureWorks2008R2;
GO
SELECT p.FirstName + ' ' + p.LastName AS 'Employee Name',
a.AddressLine1, a.AddressLine2 , a.City, a.PostalCode
FROM Person.Person AS p
INNER JOIN HumanResources.Employee AS e
ON p.BusinessEntityID = e.BusinessEntityID
INNER JOIN Person.BusinessEntityAddress bea
ON bea.BusinessEntityID = e.BusinessEntityID
INNER JOIN Person.Address AS a
ON a.AddressID = bea.AddressID;
GO
Then in your batch file, you run SQLCMD and pass it the sql file (with path) as a parameter.
sqlcmd -S myServer\instanceName -i C:\myScript.sql
If you need to authenticate as well, you'll need to add in -U and -P parameters to your SQLCMD command.
Here's an MSDN article dealing with the sqlcmd utility with more details.
Use the -S switch to specify server and instance names, e.g. -S MyDbServer\Database1
SQLCMD documentation found here.
If you want to execute all .sql files (multiple sql scripts in a folder) for multiple database then create a batch file "RunScript-All.bat" with below content
echo "======Start - Running scripts for master database======="
Call RunScript-master.bat
echo "=======End - Running scripts for master database=========="
pause
echo "=====Start - Running scripts for model database========"
Call RunScript-model.bat
echo "=======End - Running scripts for master database=========="
pause
Definition for individual batch file for a specific database i.e. "RunScript-master.bat" can be written as per below
for %%G in (*.sql) do sqlcmd /S .\SQL2014 /U sa /P XXXXXXXXX /d master -i"%%G"
::pause
Create many files for different databases and call them from "RunScript-All.bat".
Now you will be all to run all sql scripts in many database by clicking on "RunScript-All.bat" batch file.
i wrote script for downloading mdb files and reading them due OLEDB provider. All works fine, but if i try to read from table, it throws an exception:
Ms Access: Record(s) cannot be read; no read permission on tblMytable
var cmd = new OleDbCommand("SELECT * FROM tblMytable", conn);
var reader = cmd.ExecuteReader();
I changed permissions directly in Ms Access for user "administrator" and it works. But the problem is, that this script musst run twice a day and it downloads about 20 files. So its impossible manually changing permissions.
Is it possible to change read rights for a table programatically?
Thanks a lot for any ideas!
I solved this by using system.mdw file. I copied this file from "c:\Users\Administrator\AppData\Roaming\Microsoft\Access\" (in Win7) to application directory (App_Data) and modified connection string.
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.MDB;Persist Security Info=True;Jet OLEDB:System Database=|DataDirectory|\System.MDW;
var conn = new OleDbConnection(connectionString);
If still it's impossible to read data, i execute grant command:
"GRANT SELECT ON TABLE tblTable TO PUBLIC"
And it works :)
Is there a way to connect to a network drive that requires a different username/password than the username/password of the user running the package?
I need to copy files from a remote server. Right now I map the network drive in Windows Explorer then do I filesystem task. However, eventually this package will be ran automatically, from a different machine, and will need to map the network drive on its own. Is this possible?
You can use the Execute Process task with the "net use" command to create the mapped drive. Here's how the properties of the task should be set:
Executable: net
Arguments: use \Server\SomeShare YourPassword /user:Domain\YourUser
Any File System tasks following the Execute Process will be able to access the files.
Alternative Method
This Sql Server Select Article covers the steps in details but the basics are:
1) Create a "Execute Process Task" to map the network drive (this maps to the z:)
Executable: cmd.exe
Arguments: /c "NET USE Z: "\\servername\shareddrivename" /user:mydomain\myusername mypassword"
2) Then run a "File System Task" to perform the copy. Remember that the destination "Flat File Connection" must have "DelayValidation" set to True as z:\suchandsuch.csv won't exist at design time.
3) Finally, unmap the drive when you're done with another "Execute Process Task"
Executable: cmd.exe
Arguments: /c "NET USE Z: /delete"
Why not use an FTP task to GET the files over to the local machine? Run SSIS on the local machine. When transferring using FTP in binary, its real fast. Just remember that the ROW delimter for SSIS should be LF, not CRLF, as binary FTp does not convert LF (unix) to CRLF (windows)
You have to map the network drive, here's an example that I'm using now:
profile = "false"
landingPadDir = Dts.Variables("strLandingPadDir").Value.ToString
resultsDir = Dts.Variables("strResultsDir").Value.ToString
user = Dts.Variables("strUserName").Value.ToString
pass = Dts.Variables("strPassword").Value.ToString
driveLetter = Dts.Variables("strDriveLetter").Value.ToString
objNetwork = CreateObject("WScript.Network")
CheckDrive = objNetwork.EnumNetworkDrives()
If CheckDrive.Count > 0 Then
For intcount = 0 To CheckDrive.Count - 1 Step 2 'if drive is already mapped, then disconnect it
If CheckDrive.Item(intcount) = driveLetter Then
objNetwork.RemoveNetworkDrive(driveLetter)
End If
Next
End If
objNetwork.MapNetworkDrive(driveLetter, landingPadDir, profile, user, pass)
From There just use that driveLetter and access the file via the mapped drive.
I'm having one issue (which led me here) with a new script that accesses two share drives and performs some copy/move operations between the drives and I get an error from SSIS that says:
This network connection has files open or requests pending.
at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
at ScriptTask_3c0c366598174ec2b6a217c43470f581.ScriptMain.Main()
This is only on the "2nd run" of the process and if I run it a 3rd time it all works fine so I'm guessing the connection isn't being properly closed or it is not waiting for the copy/move to complete before moving forward or some such, but I'm unable to find a "close" or "flush" command that prevents this error. If you have any solution, please let me know, but the above code should work for getting the drive mapped using your alternate credentials and allow you to access that share.
Zach
To make the package more robust, you can do the following;
In the first Execute Process Task, set - FailTaskIfReturnCodeNotSuccessValue = False
This will let the package run if the last disconnect has not worked.
This is an older question but more recent versions of SQL Server with SSIS databases allow you to use a proxy to execute SQ Server jobs.
In SSMS Under Security<Credentials set up a credential in the database mapped to the AD account you want to use.
Under SQL Server Agent create a new proxy giving it the credential from step 1 and permissions to execute SSIS packages.
Under the SQL Server Agent jobs create a new job that executes your package
Select the step that executes the package and click EDIT. In the Run As dropdown select the Proxy you created in step 2