Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

Powershell - Out-File Error on collection of table objects

$
0
0

Hello all,

Getting error on scripting out table objects...works fine with one table but, not many...

Error:

Out-File : The specified wildcard pattern is not valid: Object[]

At P:\Work\Powershell\script out table objects from sql server.ps1:96 char:54

+                                     $ScriptCreate.Script($smoObjects) | out-File <<<<  -Force $scriptfile    

    + CategoryInfo          : NotSpecified: (:) [Out-File], WildcardPatternException

    + FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.OutFileCommand

				#The first thing you need to do is load the appropriate .NET assemblies used by SMO.						
				[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null	
				$serverInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "myserver"
				#String date variable
				$DateFolder = (get-Date).tostring("yyyyMMddHHssmm")							

				$ScriptCreate = new-object ("Microsoft.SqlServer.Management.Smo.Scripter")$serverInstance			
			    #Loop through databases
				foreach($sqlDatabase in $serverInstance.databases) 
				{	
					# Set which database we are on
					$sqlDatabaseName = $sqlDatabase.name -replace '[\\\/\:\.]',' ' # remove characters that can cause problems
					# Database directory full path
					$DatabaseDirPath =  "\\myserver\myshare\SQLobjectbackup\DatabaseScripts\" + $($sqlDatabaseName)					
					 #for a single database or testing	
					 IF ($sqlDatabase.name -eq "mydatabase")
					 {	
						 	# Set object filters
							$procs = $sqlDatabase.StoredProcedures | Where-object {-not $_.IsSystemObject -and -not $_.IsEncrypted }
							$views = $sqlDatabase.views | Where-object {-not $_.IsSystemObject -and -not $_.IsEncrypted }
							$tables = $sqlDatabase.tables  | Where-object {-not $_.IsSystemObject  }	
							$udfs = $sqlDatabase.UserDefinedFunctions | Where-object {-not $_.IsSystemObject -and -not $_.IsEncrypted -and $_.name -notlike "dm_*" }							
							#print database name"*****Scripting.. $sqlDatabaseName"		
								 #TABLES"""********Scripting... TABLES...."							 
							 if($tables -ne $null)
							 {
										# Set Type of ObjectName
										$TypeObjectName = $tables.GetType().Name
										# Complete save directory path
										#$CompleteFileSavePathName = $DatabaseDirPath + "\" + $DateFolder + "\" + $TypeObjectName + "\" 
										$CompleteFileSavePathName = $DatabaseDirPath + "\" + $TypeObjectName + "\" 	
										# Create directory
										new-item "$CompleteFileSavePathName" -type directory -force | out-null	
										# Set full path for procedure to variable
   								        $scriptfile = $CompleteFileSavePathName + "\" + "AllTables.sql"
										# SetScriptOptions	
   										$ScriptCreate.Options.FileName = $scriptfile
										$ScriptCreate.Options.DRIAll = $true # All Constraints 
										$ScriptCreate.Options.ClusteredIndexes = $True 
										$ScriptCreate.Options.Indexes = $true 
										$ScriptCreate.Options.Triggers = $true 
										$ScriptCreate.Options.IncludeIfNotExists = $true
										$ScriptCreate.Options.SchemaQualify = $true 
										$ScriptCreate.Options.AllowSystemObjects = $false
										$ScriptCreate.Options.AppendToFile = $true
										$ScriptCreate.Options.ExtendedProperties = $true 
										$ScriptCreate.Options.WithDependencies = $True
									#Need a collection of tables to script
									$smoObjects = New-Object Microsoft.SqlServer.Management.Smo.UrnCollection
										foreach($table in $tables) 
											{																							   
												 If ($table.IsSystemObject -eq $FALSE) 												
												   { 
														# Set table name to variable
														$ObjectName = $table.name 	
														#print which table we are scripting										"***********Scripting... table: $ObjectName ..."													
														$smoObjects.Add($table.Urn)	
													}
											}
									$ScriptCreate.Script($smoObjects) | out-File -Force $scriptfile 	

						}
					} 
					
				}
 
				

thanks

gv


Viewing all articles
Browse latest Browse all 12963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>