![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Syntax problem with powershell and variables I'm having a problem using variables with SMO and powershell here is my example script: write-host $args[0] write-host $args[1] $Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") ($args[0]) write-host "Details of the Server :" $server write-host "-----------------------------------" write-host "Server Version: " $Server.Serverversion write-host "Server Name: " $Server.Information.VersionString # get db # This line returns a null reference # $database = $Server.Databases["$args[1]"] # this line works but hardcodes the database name # $database = $Server.Databases["Northwind"] write-host "Database:" $database.name Any ideas on how to use a variable, in this case $args[1], to specify the database? Thanks |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Syntax problem with powershell and variables Hi Quote: > # This line returns a null reference > # $database = $Server.Databases["$args[1]"] $database = $Server.Databases["$($args[1])"] -or- $database = $Server.Databases[$args[1]] ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: > I'm having a problem using variables with SMO and powershell here is > my example script: > > write-host $args[0] > write-host $args[1] > $Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") > ($args[0]) > > write-host "Details of the Server :" $server > write-host "-----------------------------------" > write-host "Server Version: " $Server.Serverversion > write-host "Server Name: " $Server.Information.VersionString > # get db > # This line returns a null reference > # $database = $Server.Databases["$args[1]"] > # this line works but hardcodes the database name > # $database = $Server.Databases["Northwind"] > write-host "Database:" $database.name > > Any ideas on how to use a variable, in this case $args[1], to specify > the database? > > Thanks > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Syntax problem with powershell and variables Much thanks for the quick reply! I new it was something simple i was missing. "Shay Levi" wrote: Quote: > > Hi > Quote: > > # This line returns a null reference > > # $database = $Server.Databases["$args[1]"] > You should write it as: > > $database = $Server.Databases["$($args[1])"] > > -or- > > $database = $Server.Databases[$args[1]] > > > > ----- > Shay Levi > $cript Fanatic > http://scriptolog.blogspot.com > Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic > > > Quote: > > I'm having a problem using variables with SMO and powershell here is > > my example script: > > > > write-host $args[0] > > write-host $args[1] > > $Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") > > ($args[0]) > > > > write-host "Details of the Server :" $server > > write-host "-----------------------------------" > > write-host "Server Version: " $Server.Serverversion > > write-host "Server Name: " $Server.Information.VersionString > > # get db > > # This line returns a null reference > > # $database = $Server.Databases["$args[1]"] > > # this line works but hardcodes the database name > > # $database = $Server.Databases["Northwind"] > > write-host "Database:" $database.name > > > > Any ideas on how to use a variable, in this case $args[1], to specify > > the database? > > > > Thanks > > > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Syntax problem with powershell and variables Jason, Jason wrote: Quote: > write-host $args[0] > write-host $args[1] > > $Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") ($args[0]) the Param statement. For example, the above could be written as: Param ( $ServerName, $DatabaseName ) $smo = new-object ("Microsoft.SqlServer.Management.Smo.Server") $ServerName And to continue in that vein: Quote: > # $database = $Server.Databases["$args[1]"] And a cool feature of these named parameters is that they are still positional. That means that these are equivalent: myscript.ps1 -ServerName myserver -DatabaseName northwind myscript.ps1 myserver northwind For more detail on this read 'help about_functions'. Just wait till you learn about custom objects. ![]() -- Hal Rottenberg Blog: http://halr9000.com Webmaster, Psi (http://psi-im.org) Co-host, PowerScripting Podcast (http://powerscripting.net) |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Syntax problem with powershell and variables Yes I tried that as my first attempt; but gave up. i can see from your post that you can only have one param block. I was trying to use two like this: (Param, $ServerName) (Param, $DatabaseName ) obviously hilliarity ensued 8) Thanks for the tip... "Hal Rottenberg" wrote: Quote: > Jason, > > Jason wrote: Quote: > > write-host $args[0] > > write-host $args[1] > > > > $Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") ($args[0]) > As you learn more PowerShell you'll want to look into how to use functions and > the Param statement. For example, the above could be written as: > > Param ( $ServerName, $DatabaseName ) > $smo = new-object ("Microsoft.SqlServer.Management.Smo.Server") $ServerName > > And to continue in that vein: > Quote: > > # $database = $Server.Databases["$args[1]"] > $database = $Server.Databases[$DatabaseName] > > And a cool feature of these named parameters is that they are still positional. > That means that these are equivalent: > > myscript.ps1 -ServerName myserver -DatabaseName northwind > myscript.ps1 myserver northwind > > For more detail on this read 'help about_functions'. > > Just wait till you learn about custom objects. ![]() > > -- > > Hal Rottenberg > Blog: http://halr9000.com > Webmaster, Psi (http://psi-im.org) > Co-host, PowerScripting Podcast (http://powerscripting.net) > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| VBScripter trying to understand PowerShell syntax | PowerShell | |||
| Help with Powershell syntax | PowerShell | |||
| Powershell syntax highlighting for SciTE? | PowerShell | |||
| Powershell.exe command-line syntax | PowerShell | |||
| PowerShell syntax file (ver 0.91) for TextPad | PowerShell | |||