Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Syntax problem with powershell and variables

Closed Thread
 
Thread Tools Display Modes
Old 12-17-2007   #1 (permalink)
Jason
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
Old 12-17-2007   #2 (permalink)
Shay Levi
Guest


 

Re: Syntax problem with powershell and variables


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
>

Old 12-17-2007   #3 (permalink)
Jason
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:
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
> >
>
>
>
Old 12-17-2007   #4 (permalink)
Hal Rottenberg
Guest


 

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])
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)
Old 12-17-2007   #5 (permalink)
Jason
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:
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)
>
Closed Thread

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








Vistax64.com 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 2005-2008

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 47 48 49 50