![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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 |
| | #2 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Syntax problem with powershell and variables Hi
$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
| ||||||||||||||||||||||||
| | #3 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | 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:
| ||||||||||||||||||||||||||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Syntax problem with powershell and variables Jason, Jason wrote:
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:
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) | ||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | 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:
| ||||||||||||||||||||||||||||||||||||
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| VBScripter trying to understand PowerShell syntax | Jan | PowerShell | 5 | 07-07-2008 07:37 AM |
| Help with Powershell syntax | DaveKingston | PowerShell | 26 | 10-25-2007 10:57 AM |
| Powershell syntax highlighting for SciTE? | Robin Moffatt | PowerShell | 1 | 07-19-2007 04:58 AM |
| Powershell.exe command-line syntax | EnigmaticSoul | PowerShell | 4 | 03-29-2007 08:40 PM |
| PowerShell syntax file (ver 0.91) for TextPad | Alex K. Angelopoulos [MVP] | PowerShell | 9 | 11-23-2006 08:06 AM |