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 - Syntax problem with powershell and variables

Reply
 
Old 12-17-2007   #1 (permalink)
Jason


 
 

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 SpecsSystem Spec
Old 12-17-2007   #2 (permalink)
Shay Levi


 
 

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
>

My System SpecsSystem Spec
Old 12-17-2007   #3 (permalink)
Jason


 
 

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 SpecsSystem Spec
Old 12-17-2007   #4 (permalink)
Hal Rottenberg


 
 

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)
My System SpecsSystem Spec
Old 12-17-2007   #5 (permalink)
Jason


 
 

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 SpecsSystem Spec
Reply

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


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