![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 -d MyDb -E -i:MyScript.sqlSqlcmd: '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 Specs![]() |
| | #2 (permalink) |
| | 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 -d MyDb -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 Specs![]() |
![]() |
| 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 | |||