Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Run executable with commandline arguments

Reply
 
Old 10-23-2007   #1 (permalink)
dircums


 
 

Run executable with commandline arguments

I'm new to PowerShell.. I am creating a script to run a set of database
scripts using sqlcmd, but I can't get it to pass the command line arguments
correctly.

I have tried Invoke-Item and using sqlcmd as a command in itself but it
doesn't work.

The script below gives the following sample result:
sqlcmd -S:localhost -dMyDb -E -i:MyScript.sql
Sqlcmd: 'localhost': Unexpected argument. Enter '-?' for help.

Full script below:

$Server = ""
$Database = ""
$User = ""
$Pwd = ""
$TrustedConnection = ""

$args | foreach-object {
$currentArg = $_
switch($currentArg.SubString(0,2))
{
/S {
Write-Host "Server argument found: " $currentArg
$Server = $currentArg.SubString(3)
}
/D {
Write-Host "Database argument found: " $currentArg
$Database = $currentArg.SubString(3)
}
/U {
Write-Host "User argument found: " $currentArg
$User = $currentArg.SubString(3)
}
/P {
Write-Host "Password argument found: " $currentArg
$Pwd = $currentArg.SubString(3)
}
/E {
Write-Host "TruestedConnection argument found: " $currentArg
$TrustedConnection = $currentArg.SubString(3)
}
}
}

Write-Host "Commandline parsed. The following have been set:"
Write-Host "Server: " $Server
Write-Host "Database: " $Database
Write-Host "User: " $User
Write-Host "Trusted Connection: " $TrustedConnection

#Build the command based on the parameters

Get-Content files.txt | foreach {
if($TrustedConnection.Length -gt 0)
{
Write-Host "sqlcmd -S:$Server -d:$Database -E -i:$_"
sqlcmd -S:$Server -d:$Database -E -i:$_
}
else
{
Write-Host "sqlcmd -S:$Server -d:$Database -U:$User -P:$Pwd -i:$_"
sqlcmd -S:$Server -d:$Database -U:$User -P:$Pwd -i:$_
}
}


My System SpecsSystem Spec
Old 10-23-2007   #2 (permalink)
Marco Shaw [MVP]


 
 

Re: Run executable with commandline arguments

dircums wrote:
Quote:

> I'm new to PowerShell.. I am creating a script to run a set of database
> scripts using sqlcmd, but I can't get it to pass the command line arguments
> correctly.
>
> I have tried Invoke-Item and using sqlcmd as a command in itself but it
> doesn't work.
>
> The script below gives the following sample result:
> sqlcmd -S:localhost -dMyDb -E -i:MyScript.sql
> Sqlcmd: 'localhost': Unexpected argument. Enter '-?' for help.

Many different ways to do this... Here's one that seems to work:

sqlcmd `-S localhost `-d pMyDb -E `-i MyScript.sql

PowerShell is trying to interpret the "-" and ":", so I'm escaping them.

Give that a try.

(I used echoargs.exe from PSCX--http://www.codeplex.com/powershellcx/)

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
System.Environment.Commandline not available .NET General
Build Exchange CommandLine PowerShell
Simple one - modify app's commandline arguments dynamically PowerShell
Getting arguments from STDIN when command line arguments are missing PowerShell
ImageX Commandline Tool Vista installation & setup


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46