Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

Code not working

$
0
0
This used to work for me (with my own servers and dbs, and some other customizations) in PowerGUI.Now, the TransferData() method throws "Exception calling "TransferData" with "0" argument(s): "Object reference not set to an instance of an object."." This happens with the exact code below.When this same script is run from my SQL Servers as a CmdExec job, it succeeds.What am I missing?My local machine is running Powershell v4.Thanks![code="other"]#Connect to the local, default instance of SQL Server.#Get a server object which corresponds to the default instance$srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server#Reference the AdventureWorks2012 database.$db = $srv.Databases["AdventureWorks2012"]#Create a database to hold the copy of AdventureWorks$dbCopy = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Database -argumentlist $srv, "AdventureWorksCopy"$dbCopy.Create()#Define a Transfer object and set the required options and properties.$xfr = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfer -argumentlist $db#Set this objects properties$xfr.CopyAllTables = $true$xfr.Options.WithDependencies = $true$xfr.Options.ContinueScriptingOnError = $true$xfr.DestinationDatabase = "AdventureWorksCopy"$xfr.DestinationServer = $srv.Name$xfr.DestinationLoginSecure = $true$xfr.CopySchema = $true"Scripting Data Transfer"#Script the transfer. Alternatively perform immediate data transfer with TransferData method.#$xfr.ScriptTransfer()$xfr.TransferData()[/code]

Viewing all articles
Browse latest Browse all 467

Latest Images

Trending Articles



Latest Images