Failed to create SQL script transfer with powershell - sql-server

I've got next error when I'm trying to create database script:
Exception calling "EnumScriptTransfer" with "0" argument(s): "Script
transfer failed. " At C:\temp\Test.ps1:126 char:5
+ $transfer.EnumScriptTransfer();
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : FailedOperationException
Same script is working properly on the other computer. So I assume that the reason is in SQL server version, or rights.
Any ideas?
Code:
echo "Scripting database $dbname .."
set-psdebug -strict
$ErrorActionPreference = "stop" # you can opt to stagger on, bleeding, if an error occurs
# Load SMO assembly, and if we're running SQL 2008 DLLs load the SMOExtended and SQLWMIManagement libraries
$ms='Microsoft.SqlServer'
$v = [System.Reflection.Assembly]::LoadWithPartialName( "$ms.SMO")
$ver = (($v.FullName.Split(','))[1].Split('='))[1];
$vtmp=$ver.Split('.')[0];
echo " SMO version is $ver"
if ($vtmp -ge 9) {
echo " Running SQL 2008, load the SMOExtended .."
[System.Reflection.Assembly]::LoadWithPartialName("$ms.SMOExtended") | out-null
}
$My="$ms.Management.Smo" #
$s = new-object ("$My.Server") $DataSource
if ($s.Version -eq $null ){Throw "Can't find the instance $Datasource"}
$ver = $s.Version
echo " SQL Server version is $ver"
$db= $s.Databases[$Database]
if ($db.name -ne $Database){Throw "Can't find the database '$Database' in $Datasource"};
$transfer = new-object ("$My.Transfer") $db
if ($transfer -eq $null ){Throw "Can't create transfer object"}
$transfer.Options.ScriptBatchTerminator = $true # this only goes to the file
$transfer.Options.ToFileOnly = $true # this only goes to the file
$transfer.Options.ScriptData = $true;
$transfer.Options.FileName = $BackupFile;
$tmp = $transfer.Options
echo " Transfer options are: $tmp"
echo " $transfer"
$transfer.EnumScriptTransfer();
Output:
Scripting database MyDbName ..
SMO version is 14.0.0.0
SQL Server version is 14.0.1000
Transfer options are: ScriptingOptions: ScriptBatchTerminator, ToFileOnly, SchemaQualify, WithDependencies, AllowSystem
Objects, AgentJobId, NoVardecimal, ScriptSchema, ScriptData, ScriptDataCompression, PrimaryObject
Microsoft.SqlServer.Management.Smo.Transfer
Exception calling "EnumScriptTransfer" with "0" argument(s): "Script transfer failed. "
At C:\temp\Test.ps1:126 char:5
+ $transfer.EnumScriptTransfer();
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : FailedOperationException
Tried to add try-catch block, but this does not work. No idea why:
Try
{
$transfer.EnumScriptTransfer();
}
Catch
{
$msg = $_.Exception.Message;
$innerMsg = $_.Exception.InnerException.Message;
echo $msg
echo $innerMsg
throw;
}

Related

Issue with Powershell SMO SQL Server restore

I am running into an issue with doing a Powershell SQL restore. I have this code
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
$backupFile = 'C:\Temp\User_20191029152532.bak'
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") "(local)"
$backupDevice = New-Object("Microsoft.SqlServer.Management.Smo.BackupDeviceItem") ($backupFile, "File")
$smoRestore = new-object("Microsoft.SqlServer.Management.Smo.Restore")
$smoRestore.NoRecovery = $false;
$smoRestore.ReplaceDatabase = $true;
$smoRestore.Action = "Database"
$smoRestorePercentCompleteNotification = 10;
$smoRestore.Devices.Add($backupDevice)
$smoRestoreDetails = $smoRestore.ReadBackupHeader($server)
"Database Name from Backup Header : " +$smoRestoreDetails.Rows[0]["DatabaseName"]
$smoRestore.Database =$smoRestoreDetails.Rows[0]["DatabaseName"]
$smoRestoreFile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile")
$smoRestoreLog = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile")
$smoRestoreFile.LogicalFileName = $smoRestoreDetails.Rows[0]["DatabaseName"]
$smoRestoreFile.PhysicalFileName = $server.Information.MasterDBPath + "\" + $smoRestore.Database + "_Data.mdf"
$smoRestoreLog.LogicalFileName = $smoRestoreDetails.Rows[0]["DatabaseName"] + "_Log"
$smoRestoreLog.PhysicalFileName = $server.Information.MasterDBLogPath + "\" + $smoRestore.Database + "_Log.ldf"
$smoRestore.RelocateFiles.Add($smoRestoreFile)
$smoRestore.RelocateFiles.Add($smoRestoreLog)
$smoRestore.SqlRestore($server)
Everything works fine if I run this. However, I would like to provide a variable for which backup to restore. So I changed the top part of the code to this:
$path = 'C:\Temp'
$db_name = 'User'
$file = Get-ChildItem $path '*.bak' | Select-Object basename | Where-Object {$_.basename -like $db_name + '*' }
$backupFile = $path + '\' + $file
When I make this change, I start getting the below error.
Exception calling "ReadBackupHeader" with "1" argument(s): "An exception occurred while executing a Transact-SQL statement or batch."
At line:23 char:1
+ $smoRestoreDetails = $smoRestore.ReadBackupHeader($server)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ExecutionFailureException
Not really sure why it will not accept the $file variable that I am passing. If I look at the variable, it is returning the right value.
Stupid issue on my part. $file did not include the extension. Adding the .bak to $file fixed the issue.

Can connect to SQL Server from SSMS from other computer but Can't connect to SQL Server in a network from powershell

i am trying to connect to a sql server from powershell and getting this error
Exception calling "Open" with "0" argument(s): "The target principal
name is incorrect. Cannot generate SSPI context." At
C:\Users\Musawwir\Downloads\remotely access db.ps1:10 char:1
+ $Connection.Open()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ( [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException Exception calling "Fill" with "1" argument(s):
I can connect to SQL SERVER from other machines through SSMS but can not connect through powershell, also the script runs fine on local machine.
Here is the code i am using :
[string] $Server= "tcp:DESKTOP-J9UQ90E,1433"
[string] $Database = "Eshop"
[string] $SQLQuery= "[dbo].[usp_getCustomerAge]"
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';Connection Timeout=300;Integrated Security=TRUE;UID=ali;PWD=1234"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Command.CommandTimeout=500
$adp = New-Object System.Data.SqlClient.SqlDataAdapter $Command
$data = New-Object System.Data.DataSet
$adp.Fill($data) | Out-Null
$data2 = ''
$emp = ''
foreach($Row in $data.Tables.Rows){
[string]$C_name = $Row[0]
$C_name
$data2= $data2+ "|"+$C_name
$C_name = $C_name+"|"
$C_name | out-file "d:\\test4.txt" -Append
}
$Connection.Close()
#$data2 | out-file "d:\\test4.txt"
The user ali wasn't having access to stored procedure .. changed user from ali to sa and integrated security to false solved the problem

Suspended Invoke-SQLcmd Query - Database in use

I am trying to automate the process of importing GNAF (location dataset for Australia) into a new database.
The problem I am having is that when I try to the database and recreate it I get an error saying the database is in use:
Invoke-Sqlcmd : Cannot drop database "GNAF" because it is currently in use. At line:11 char:5
+ Invoke-Sqlcmd -Query "IF EXISTS (SELECT name FROM master.dbo.sysd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand Invoke-Sqlcmd : Database 'GNAF' already exists. Choose a different database name. At line:15 char:5
+ Invoke-Sqlcmd -Query "CREATE DATABASE GNAF" -serverinstance $Serv ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
In the activity monitor for SSMS it shows:
This is the function that is being called:
function importQNAF {
$ScriptPath = "C:\Users\owain.esau\Scripts\PowerShell\SQL-Server\TRIS Database Anon\" # change to pwd for live
$QNAFpath1 = ( $ScriptPath + "GNAF\G-NAF\G-NAF AUGUST 2017\Standard\" )
$QNAFpath2 = ( $ScriptPath + "GNAF\G-NAF\G-NAF AUGUST 2017\Authority Code\" )
$GNAF_ImportData_Path = ( $ScriptPath + "\SQLScripts\GNAF_ImportData.sql" )
(Get-Content $GNAF_ImportData_Path).replace("Powershell To Change#1", $QNAFpath1) | Set-Content $GNAF_ImportData_Path
(Get-Content $GNAF_ImportData_Path).replace("Powershell To Change#2", $QNAFpath2) | Set-Content $GNAF_ImportData_Path
## Drop and Create Table
Invoke-Sqlcmd -Query "IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='GNAF')
BEGIN
ALTER DATABASE GNAF SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE GNAF;
END
;" -serverinstance $ServerAddress;
Start-Sleep -s 5;
Invoke-Sqlcmd -Query "CREATE DATABASE GNAF" -serverinstance $ServerAddress;
Start-Sleep -s 5;
Invoke-Sqlcmd -InputFile ( $ScriptPath + "SQLScripts\GNAF_CreateTables.sql" ) -serverinstance $ServerAddress;
try {
Invoke-Sqlcmd -InputFile ( $ScriptPath + "SQLScripts\GNAF_ImportData.sql" ) -serverinstance $ServerAddress -querytimeout 0;
} catch {
if ($error -match "Access is denied.") {
Write-Host "[!] ERROR: Access denied to GNAF folder."
Write-Host "----------------------------------------"
Write-Host "You have two options: "
Write-Host ""
Write-Host " 1) Give MSSQLSERVER full access to" $ScriptPath
Write-Host " 2) Copy the1 folder: " $ScriptPath"GNAF to the MSSQL Source folder (C:\Program Files\SQL Server)"
Do {
switch(Read-Host "Please choose an action (1 or 2)") {
1 {
$Acl = (Get-Item $ScriptPath).GetAccessControl('Access');
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("NT SERVICE\MSSQLSERVER", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow");
$Acl.SetAccessRule($Ar);
Set-Acl $ScriptPath $Acl;
importQNAF;
}
2 { Write-Host "Not coded yet"; }
default { "Invalid Entry, must be 1 or 2"; }
}
} Until ($choice -eq 1 -or $choice -eq 2)
}
}
(Get-Content $GNAF_ImportData_Path).replace($QNAFpath1, "Powershell To Change#1") | Set-Content $GNAF_ImportData_Path
(Get-Content $GNAF_ImportData_Path).replace($QNAFpath2, "Powershell To Change#2") | Set-Content $GNAF_ImportData_Path
}
The script I am using to actually import the data into SQL server is almost identical to the one found on this page:
https://www.rednotebluenote.com/2016/04/importing-psma-geocoded-national-address-file-g-naf-to-sql-server/
It works fine directly in SSMS. Also, I don't think my error match is picking up the correct error, instead it is matching to all errors.
--- Update ----------------------------------------------
Managed to get it to run when i restart the MSSQLSERVER service at the start of the function.
The problem now is that it times out:
Invoke-Sqlcmd : Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
BULK INSERT [MB_2011] FROM 'C:\Users\owain.esau\OneDrive\Scripts\PowerShell\SQL-Server\TRIS Database Anon\GNAF\G-NAF\G-NAF AUGUST 2017\Standard\ACT_MB_2011_psv.psv' WITH ( FIELDTERMINATOR = '|', ROWTERMINATOR =
'\n', FIRSTROW = 2, ROWS_PER_BATCH=100000, TABLOCK)
At line:30 char:9
+ Invoke-Sqlcmd -InputFile ( $ScriptPath + "SQLScripts\GNAF_Imp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
It only runs for around 45 seconds before this error pops up, the actual script being run takes around 3 - 5 minutes run in SSMA.
(The function has been updated a bit)
You need to terminate any existing, open connections to the database.
Here's the easiest method (it basically forces all other connections to be killed)
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='GNAF')
BEGIN
ALTER DATABASE GNAF SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE GNAF;
END
;

Using powershell to restart computers if -LastBootupTime = -gt 10 days

I'm trying to make a script that checks how long the computers on a network are on, and if they are on for more then 10 days they need to restart. I'm planning to run the script automatically with task manager every Sunday.
Thanks to #vonPryz I've got something like this now:
$clients = get-content "C:\Documents\lijstcomputers.txt"
foreach ($client in $clients) {
if (test-connection -computername $client -BufferSize 16 -Count 1 -Quiet) {
write-Host $client is online
$uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUpTime
$startTime = [Management.ManagementDateTimeConverter]::ToDateTime((gwmi Win32_OperatingSystem -computer $client).lastbootuptime)
if( $uptime.days -ge 10) {
restart-computer -computername $client
add-content -path "c:\path\to\log.txt" -value "$client, $startTime, $uptime"
}
}
else {
write-Host $client is offline
}
}
But now I'm getting this error:
gcim : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and
that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM firewall exception for publ
ic profiles limits access to remote computers within the same local subnet.
At line:1 char:25
+ $uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ConnectionError: (root\cimv2:Win32_OperatingSystem:String) [Get-CimInstance], CimException
+ FullyQualifiedErrorId : HRESULT 0x80338126,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand
+ PSComputerName : ASND0042
Cannot find an overload for "op_Subtraction" and the argument count: "2".
At line:1 char:1
+ $uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Since you already got some parts covered, this isn't just a plz give me teh codez and thus warrants help. So let's outline how the whole script should look.
# Write computer names into a file, one per row for easier management
$clients = get-content "c:\ListOfComputers.txt"
foreach ($client in $clients) {
# If computer's not up, there's no need to check uptime
if (test-connection -computername $client -BufferSize 16 -Count 1 -Quiet) {
write-Host $client is online
# Get uptime
$uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUpTime
# Get start time
$startTime = [Management.ManagementDateTimeConverter]::ToDateTime((gwmi Win32_OperatingSystem -computer $client).lastbootuptime)
# Restart the client if uptime's at least 10 days.
if( $uptime.days -ge 10) {
restart-computer -computername $client
# Add client name, start date and uptime into a log file
add-content -path "c:\path\to\log.txt" -value "$client, $startTime, $uptime"
}
}
else {
write-Host $client is offline
}
}
This skeleton can be further improved by adding some error handling and, say, proper CSV export.

Powershell Script Issue with SQL Server 2012/2014

I have a PowerShell script which works well when I run it on server with SQL Server default instance (MSSQLSERVER) but the same script fails on a server with a named instance (MSSQL$instance)
For the default instance (MSSQLSERVER)
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$service = Get-service -name 'MSSQLSERVER'
$status = $service.status
$CreateDB = "db-Test"
if ( $status -eq "Running" )
{
'Success' | Out-File -FilePath c:\sqltest.log -Encoding ASCII
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database($srv, "$CreateDB")
$db.Create()
$db.CreateDate
}
else
{
'Failed' | Out-File -FilePath c:\sqltest.log -Encoding ASCII
}
Above script works very well. But below script throws error :
For named instance :
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$service = Get-service -name 'MSSQL$instancename'
$status = $service.status
$CreateDB = "db-Test"
if ( $status -eq "Running" )
{
'Success' | Out-File -FilePath c:\sqltest.log -Encoding ASCII
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database($srv, "$CreateDB")
$db.Create()
$db.CreateDate
}
else
{
'Failed' | Out-File -FilePath c:\sqltest.log -Encoding ASCII
}
Above script for named SQL instance throws below error :
New-Object : Exception calling ".ctor" with "2" argument(s): "SetParent failed for Database 'Netmagic-Test'. "
At C:\mssql-test.ps1:11 char:7
+ $db = New-Object Microsoft.SqlServer.Management.Smo.Database($srv, "$CreateDB")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
You cannot call a method on a null-valued expression.
At C:\mssql-test.ps1:12 char:1
+ $db.Create()
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Error Screen
Thanks,
Viral

Resources