SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Powershell SMO works in cmd, not from script


Powershell SMO works in cmd, not from script

Author
Message
joseph.uher
joseph.uher
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 129
Hope this is a correct forum.

My code (Server 2003 Enterprise, SQL Server 10.0.5512.0, Powershell v1.0 ISE) is as follows:

function Script-Database_01 {
# Created: 09/06/2013
# Edited: 09/06/2013

[CmdletBinding()]
param(
[Parameter(
Mandatory=$true,
ValueFromPipeline=$true,
HelpMessage="Local directory to save build-scripts output.")]
[Alias('fp')]
[string[]] $Filepath,

[Parameter(
Mandatory=$true,
ValueFromPipeline=$true,
HelpMessage ="Server or instance name.")]
[Alias('ds')]
[string[]] $DataSource,

[Parameter(
Mandatory=$true,
ValueFromPipeline=$true,
HelpMessage="The database to be scripted.")]
#[Alias('db')]
[string[]] $DatabaseName
)
BEGIN{
Write-Output $Filepath
Write-Output $DataSource
Write-Output $DatabaseName
Get-PSSession
}
PROCESS{
# Set 'Option Explicit' to catch subtle errors:
set-psdebug -strict

#Load SMO assembly, and SMOExtended and SQLWMIManagement libraries:
$ms = "Microsoft.SqlServer"
$v = [System.Reflection.Assembly]::LoadWithPartialName("$ms.SMO")
[System.Reflection.Assembly]::LoadWIthPartialName("$ms.SMOExtended") | out-null

$my = "$ms.Management.SMO"
$source = new-object("$my.Server") $DataSource
if ($Source.Version -eq $null) {Throw "Cant find the instance $DataSource."}

$db = $source.Databases[$DatabaseName]
if ($db.name -ne $DatabaseName) {Throw "Cant find the database '$DatabaseName' in $DataSource."}

$transfer = new-object("$my.Transfer") $db
$transfer.Options.ScriptBatchTerminator = $true # This only goes to file
$transfer.Options.ToFileOnly = $true # This only goes to file
$transfer.Options.Filename = "$($Filepath)\$($DatabaseName)_Build_01b.sql"

# Do it:
$transfer.ScriptTransfer()
}
END{}
}

When I execute everything in the PROCESS{} section, the commands succeed, and the file is correctly output. When I run the script (as .\Script-Database_01.psq [+parameters] I get the follow error:

New-Object : Cannot find an overload for "Server" and the argument count: "2".
At C:\Powershell\Scripts\Script-Database_01.ps1:44 char:29
+ $source = new-object <<<< ("$my.Server") $DataSource
+ CategoryInfo : InvalidOperation: (Smile [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

I assume this has something to do with the import of SMOExtended, but I can't see it.

Can anyone point me in the "write" direction?
joseph.uher
joseph.uher
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 129
Well, I guess I'm an idiot! My script file is not a script, but a function. I loaded the script file into memory, and then called the function, and it works fine -- just like the command line execution! Problem solved. Thanks to Don Jones / Jeffery Hicks and "Learn Powershell Toolmaking in a month of lunches" (p. 36)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search