question

Kiran 1 avatar image
Kiran 1 asked

passing parameters to a vbs file in command line

Hi,

I have a VbScript file that has oSQLServer.Connect statement using windows authentication. I am trying to pass the servername as parameter from command line.

cscript ScriptJobs.vbs KIRAN\SECONDARY

this value KIRAN\SECONDARY should be passed as the servername. I have the following code

Dim ArgObj
Dim ServerName
Set ArgObj=Wscript.Arguments
Set ServerName=ArgObj.Item(0)

Dim oSQLServer
Dim oStream
Set oSQlServer = CreateObject("Wscript.SQLDMO.SQLServer")
Set oStream = CreateObject("ADODB.Stream")
oSQLServer.LoginSecure=True
oSQLServer.Connect "& ServerName"

Dim idStep
Dim ScriptJob
Dim CountJobs
Dim JobName
Dim ScriptAllJobs

For Each oJob In
oSQLServer.JobServer.Jobs
CountJobs = oSQLServer.JobServer.Jobs.Count
Next

For idStep = 1 To CountJobs

JobName = oSQLServer.JobServer.Jobs.Item(idStep).Name
ScriptJob = oSQLServer.JobServer.Jobs.Item(idStep).Script(4, "C:\" & JobName & ".sql")

ScriptAllJobs = ScriptAllJobs & ScriptJob

Next

oStream.Open oStream.WriteText (ScriptAllJobs) oStream.SaveToFile ("C:\SQLAllScripts.sql"), 2

oStream.Close oSQLServer.DisConnect

Set oStream = Nothing
Set oSQLServer = Nothing
Set ArgObj=Nothing

However, the parameter that i pass in command line is not being taken as input into the ServerName.

Please help me out with this

sql-servervbscript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Fatherjack avatar image
Fatherjack answered

Safe to say I'm not the greatest vbscript writer but on the line oSQLServer.Connect "& ServerName" should the & be in the quotes? and also what value are you passing to the script? If the server has a named SQL instance then you might need to wrap it in [] so it might need to be like [MyServer\SQLInstanceName].

Can you connect to the SQL Server by other means from the PC where this problem exists? You might try using a UDL file to test it.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kiran 1 avatar image Kiran 1 commented ·
Thanks for replying. Found the problem with the code. Have to remove set in SET SERVERNAME= statement. Take care
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.