I found a Powershell script to script out all of my database objects. I found that certain characters such as * or \ aren’t liked. We use * to mark a table for deprecation and if you create a table without specifying schema it gets created with your network username domain\userid.
Anyway, either SMO or Powershell don’t like it. How can I filter objects with these characters so it doesn’t bomb our when running from SQL Agent?
It bombs on the outfile chunk of code, last code line.
$objs.Script($so)+”GO” | out-File $OutFile
#-power shell script Set-Location d: $path = “D:\ETLDeployment\D01_VISN05\ScriptObjects\” $ServerName = “vhacdwdwhsql33” [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') $serverInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName $DateStr = Get-Date -format "yyyy-MM-dd" $IncludeTypes = @(“tables”,”StoredProcedures”,”Views”,”UserDefinedFunctions”) $ExcludeSchemas = @(“sys”,”Information_Schema”) $so = new-object ('Microsoft.SqlServer.Management.Smo.ScriptingOptions') $so.IncludeIfNotExists = 0 $so.SchemaQualify = 1 $so.AllowSystemObjects = 0 $so.ScriptDrops = 0 #Script Drop Objects $dbs=$serverInstance.Databases foreach ($db in $dbs) { $dbname = “$db”.replace(“[“,””).replace(“]”,””) $dbpath = “$path”+”$dbname” + “_” + “$DateStr” + “\” if ($dbname -like 'D01_VISN05*') { #if ( !(Test-Path $dbpath)) # {$null=new-item -type directory -name “$dbname”-path “$path”} foreach ($Type in $IncludeTypes) { $objpath = “$dbpath” + “$Type” + “\” if ( !(Test-Path $objpath)) {$null=new-item -type directory -name “$Type”-path “$dbpath”} foreach ($objs in $db.$Type) { If ($ExcludeSchemas -notcontains $objs.Schema ) { $ObjName = “$objs”.replace(“[“,””).replace(“]”,””) $OutFile = “$objpath” + “$ObjName” + “.sql” $objs.Script($so)+”GO” | out-File $OutFile } } } } }