Skip to content

Commit e4b2f5f

Browse files
committed
Set Strictmode fixes
Object Properties Missing and Scripter Method ignoring schema
1 parent 52a9785 commit e4b2f5f

7 files changed

Lines changed: 53 additions & 23 deletions

Scripts/01_Server_Logins.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,15 @@ function CopyObjectsToFiles($objects, $outDir) {
166166

167167
$schemaPrefix = ""
168168

169+
try
170+
{
169171
if ($o.Schema -ne $null -and $o.Schema -ne "") {
170172
$schemaPrefix = $o.Schema + "."
171173
}
174+
}
175+
catch
176+
{
177+
}
172178

173179
$fixedOName = $o.name.replace('\','_')
174180
$scripter.Options.FileName = $outDir + $schemaPrefix + $fixedOName + ".sql"
@@ -236,7 +242,7 @@ $logins = $srv.Logins
236242

237243
foreach ($Login in $Logins)
238244
{
239-
245+
240246
# Skip non-Domain logins that look like Domain Logins (contain "\")
241247
if ($Login.Name -like "NT SERVICE\*") {continue}
242248
if ($Login.Name -like "NT AUTHORITY\*") {continue}
@@ -294,7 +300,7 @@ foreach ($Login in $Logins)
294300
}
295301

296302

297-
# Windows Domain Users
303+
# Windows Users
298304
if ($Login.LoginType -eq "WindowsUser")
299305
{
300306
Write-Output ("Scripting out: {0}" -f $Login.Name)

Scripts/01_Server_Resource_Governor.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,15 @@ function CopyObjectsToFiles($objects, $outDir) {
105105

106106
$schemaPrefix = ""
107107

108+
try
109+
{
108110
if ($o.Schema -ne $null -and $o.Schema -ne "") {
109111
$schemaPrefix = $o.Schema + "."
110112
}
113+
}
114+
catch
115+
{
116+
}
111117

112118
$fixedOName = $o.name.replace('\','_')
113119
$scripter.Options.FileName = $outDir + $schemaPrefix + $fixedOName + ".sql"
@@ -198,7 +204,7 @@ if(!(test-path -path $output_path))
198204

199205
#pools
200206
$pools = $srv.ResourceGovernor.ResourcePools | where-object -FilterScript {$_.Name -notin "internal","default"}
201-
if ($pools.Count -gt 0)
207+
if ($pools -ne $null)
202208
{
203209
CopyObjectsToFiles $pools $output_path
204210
}

Scripts/03_NET_Assemblies.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ foreach($sqlDatabase in $srv.databases)
167167
}
168168

169169
# Any results?
170-
if ($results.count -gt 0)
170+
if ($results -ne $null)
171171
{
172172
Write-Output "Scripting out .NET Assemblies for: "$fixedDBName
173173
}

Scripts/04_Agent_Operators.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,15 @@ Foreach ($row in $results)
197197
Add-Content -Value "`r`n" -Path "$fullfolderPath\Agent_Operators.sql" -Encoding Ascii
198198
}
199199

200-
if ($results.Count -gt 0)
200+
try
201+
{
202+
if ($results -ne $null)
203+
{
204+
Write-Output ("{0} Operators Exported" -f $results.count)
205+
}
206+
}
207+
catch
201208
{
202-
Write-Output ("{0} Operators Exported" -f $results.count)
203209
}
204210

205211

Scripts/05_DBMail_Accounts.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,17 @@ Foreach ($row in $results)
182182
Add-Content -Value "`r`n" -Path "$fullfolderPath\DBMail_Accounts.sql" -Encoding Ascii
183183
}
184184

185-
Write-Output ("{0} DBMail Accounts Exported" -f $results.count)
185+
186+
try
187+
{
188+
if ($results.count -ne $null)
189+
{
190+
Write-Output ("{0} DBMail Accounts Exported" -f $results.count)
191+
}
192+
}
193+
catch
194+
{
195+
}
186196

187197

188198
set-location $BaseFolder

Scripts/14_Service_Broker.ps1

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ foreach($sqlDatabase in $srv.databases)
223223
foreach($MsgType2 in $db.ServiceBroker.ServiceContracts)
224224
{
225225
# Script out objects for each DB
226-
$strmyBrokerMsgTypeName = $MsgType2.Name
227-
$strmyBrokerMsgType = $fixedDBName+"_Broker_Contract_"+$strmyBrokerMsgTypeName+".sql"
226+
$strmyBrokerContractName = $MsgType2.Name
227+
$strmyBrokerContract = $fixedDBName+"_Broker_Contract_"+$strmyBrokerContractName+".sql"
228228
$strmyBObj = $MsgType2.script()
229-
$output_path = $DB_Broker_output_path+"\"+$strmyBrokerMsgType
229+
$output_path = $DB_Broker_output_path+"\"+$strmyBrokerContract
230230

231231
# Not system Objects
232232
if (!$MsgType2.IsSystemObject)
@@ -246,13 +246,13 @@ foreach($sqlDatabase in $srv.databases)
246246
foreach($MsgType3 in $db.ServiceBroker.Queues)
247247
{
248248
# Script out objects for each DB
249-
$strmyBrokerMsgTypeName = $MsgType3.Name
250-
$strmyBrokerMsgType = $fixedDBName+"_Broker_Queue_"+$strmyBrokerMsgTypeName+".sql"
249+
$strmyBrokerQueueName = $MsgType3.Name
250+
$strmyBrokerQueue = $fixedDBName+"_Broker_Queue_"+$strmyBrokerQueueName+".sql"
251251
try
252252
{
253253
$strmyBObj = $MsgType3.script()
254254

255-
$output_path = $DB_Broker_output_path+"\"+$strmyBrokerMsgType
255+
$output_path = $DB_Broker_output_path+"\"+$strmyBrokerQueue
256256

257257
# Not system Objects
258258
if (!$MsgType3.IsSystemObject)
@@ -268,7 +268,7 @@ foreach($sqlDatabase in $srv.databases)
268268
}
269269
catch
270270
{
271-
#Write-Output "Skipping system queue $strmyBrokerMsgTypeName"
271+
#Write-Output "Skipping system queue $strmyBrokerQueueName"
272272
}
273273
}
274274

@@ -277,10 +277,11 @@ foreach($sqlDatabase in $srv.databases)
277277
foreach($MsgType4 in $db.ServiceBroker.Services)
278278
{
279279
# Script out objects for each DB
280-
$strmyBrokerMsgTypeName = $MsgType4.Name
281-
$strmyBrokerMsgType = $fixedDBName+"_Broker_Service_"+$strmyBrokerMsgTypeName+".sql"
280+
$strmyBrokerServiceName = $MsgType4.Name
281+
$strmyBrokerService = $fixedDBName+"_Broker_Service_"+$strmyBrokerServiceName+".sql"
282282
$strmyBObj = $MsgType4.script()
283-
$output_path = $DB_Broker_output_path+"\"+$strmyBrokerMsgType
283+
$output_path = $DB_Broker_output_path+"\"+$strmyBrokerService
284+
284285

285286
# Not system Objects
286287
if (!$MsgType4.IsSystemObject)
@@ -300,13 +301,14 @@ foreach($sqlDatabase in $srv.databases)
300301
foreach($MsgType5 in $db.ServiceBroker.Routes)
301302
{
302303
# Script out objects for each DB
303-
$strmyBrokerMsgTypeName = $MsgType5.Name
304-
$strmyBrokerMsgType = $fixedDBName+"_Broker_Route_"+$strmyBrokerMsgTypeName+".sql"
304+
$strmyBrokerRouteName = $MsgType5.Name
305+
$strmyBrokerRoute = $fixedDBName+"_Broker_Route_"+$strmyBrokerRouteName+".sql"
305306
$strmyBObj = $MsgType5.script()
306-
$output_path = $DB_Broker_output_path+"\"+$strmyBrokerMsgType
307+
$output_path = $DB_Broker_output_path+"\"+$strmyBrokerRoute
307308

308-
# Not system Objects
309-
if (!$MsgType5.IsSystemObject -and $MsgType5.Name -ne "AutoCreatedLocal")
309+
310+
# Skip system Objects
311+
if ($MsgType5.Name -ne "AutoCreatedLocal")
310312
{
311313
# Only create path if something to write
312314
if(!(test-path -path $DB_Broker_output_path))
@@ -316,6 +318,7 @@ foreach($sqlDatabase in $srv.databases)
316318
$strmyBObj | out-file $output_path -Force -encoding ascii
317319
$anyfound = $true
318320
}
321+
319322
}
320323

321324
if ($anyfound-eq $true)

Scripts/17_Managed_Backups.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ Param(
3434
[string]$mypass
3535
)
3636

37-
Set-StrictMode -Version latest;
3837

3938
[string]$BaseFolder = (Get-Item -Path ".\" -Verbose).FullName
4039

0 commit comments

Comments
 (0)