|
29 | 29 | Database1Name = 'ScriptDatabase1' |
30 | 30 | Database2Name = 'ScriptDatabase2' |
31 | 31 | Database3Name = '$(DatabaseName)' |
| 32 | + Database4Name = 'ScriptDatabase4' |
32 | 33 |
|
33 | 34 | GetSqlScriptPath = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName()) |
34 | 35 | SetSqlScriptPath = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName()) |
35 | 36 | TestSqlScriptPath = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName()) |
36 | 37 |
|
| 38 | + GetSqlScriptPath2 = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName()) |
| 39 | + SetSqlScriptPath2 = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName()) |
| 40 | + TestSqlScriptPath2 = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName()) |
| 41 | + |
37 | 42 | GetSqlScript = @' |
38 | 43 | SELECT Name FROM sys.databases WHERE Name = '$(DatabaseName)' FOR JSON AUTO |
39 | 44 | '@ |
@@ -294,6 +299,148 @@ Configuration DSC_SqlScript_RunSqlScriptWithVariablesDisabled_Config |
294 | 299 | } |
295 | 300 | } |
296 | 301 |
|
| 302 | +<# |
| 303 | + .SYNOPSIS |
| 304 | + Creates the script files and runs the SQL script as a Windows User |
| 305 | + in the same configuration using DependsOn. |
| 306 | +#> |
| 307 | +Configuration DSC_SqlScript_RunSqlScriptAsWindowsUserWithDependencies_Config |
| 308 | +{ |
| 309 | + Import-DscResource -ModuleName 'xPSDesiredStateConfiguration' -ModuleVersion '9.1.0' |
| 310 | + Import-DscResource -ModuleName 'SqlServerDsc' |
| 311 | + |
| 312 | + node $AllNodes.NodeName |
| 313 | + { |
| 314 | + xScript 'CreateFile_GetSqlScript' |
| 315 | + { |
| 316 | + SetScript = { |
| 317 | + $Using:Node.GetSqlScript | Out-File -FilePath $Using:Node.GetSqlScriptPath2 -Encoding ascii -NoClobber -Force |
| 318 | + } |
| 319 | + |
| 320 | + TestScript = { |
| 321 | + $getScriptResult = & ([ScriptBlock]::Create($GetScript)) |
| 322 | + |
| 323 | + return $getScriptResult.Result -eq $Using:Node.GetSqlScript |
| 324 | + } |
| 325 | + |
| 326 | + GetScript = { |
| 327 | + $fileContent = $null |
| 328 | + |
| 329 | + if (Test-Path -Path $Using:Node.GetSqlScriptPath2) |
| 330 | + { |
| 331 | + $fileContent = Get-Content -Path $Using:Node.GetSqlScriptPath2 -Raw |
| 332 | + } |
| 333 | + |
| 334 | + return @{ |
| 335 | + Result = $fileContent |
| 336 | + } |
| 337 | + } |
| 338 | + } |
| 339 | + |
| 340 | + xScript 'CreateFile_TestSqlScript' |
| 341 | + { |
| 342 | + SetScript = { |
| 343 | + $Using:Node.TestSqlScript | Out-File -FilePath $Using:Node.TestSqlScriptPath2 -Encoding ascii -NoClobber -Force |
| 344 | + } |
| 345 | + |
| 346 | + TestScript = { |
| 347 | + $getScriptResult = & ([ScriptBlock]::Create($GetScript)) |
| 348 | + |
| 349 | + return $getScriptResult.Result -eq $Using:Node.TestSqlScript |
| 350 | + } |
| 351 | + |
| 352 | + GetScript = { |
| 353 | + $fileContent = $null |
| 354 | + |
| 355 | + if (Test-Path -Path $Using:Node.TestSqlScriptPath2) |
| 356 | + { |
| 357 | + $fileContent = Get-Content -Path $Using:Node.TestSqlScriptPath2 -Raw |
| 358 | + } |
| 359 | + |
| 360 | + return @{ |
| 361 | + Result = $fileContent |
| 362 | + } |
| 363 | + } |
| 364 | + } |
| 365 | + |
| 366 | + xScript 'CreateFile_SetSqlScript' |
| 367 | + { |
| 368 | + SetScript = { |
| 369 | + $Using:Node.SetSqlScript | Out-File -FilePath $Using:Node.SetSqlScriptPath2 -Encoding ascii -NoClobber -Force |
| 370 | + } |
| 371 | + |
| 372 | + TestScript = { |
| 373 | + $getScriptResult = & ([ScriptBlock]::Create($GetScript)) |
| 374 | + |
| 375 | + return $getScriptResult.Result -eq $Using:Node.SetSqlScript |
| 376 | + } |
| 377 | + |
| 378 | + GetScript = { |
| 379 | + $fileContent = $null |
| 380 | + |
| 381 | + if (Test-Path -Path $Using:Node.SetSqlScriptPath2) |
| 382 | + { |
| 383 | + $fileContent = Get-Content -Path $Using:Node.SetSqlScriptPath2 -Raw |
| 384 | + } |
| 385 | + |
| 386 | + return @{ |
| 387 | + Result = $fileContent |
| 388 | + } |
| 389 | + } |
| 390 | + } |
| 391 | + |
| 392 | + SqlScript 'Integration_Test' |
| 393 | + { |
| 394 | + Id = 'Integration_Test' |
| 395 | + ServerName = $Node.ServerName |
| 396 | + InstanceName = $Node.InstanceName |
| 397 | + |
| 398 | + GetFilePath = $Node.GetSqlScriptPath2 |
| 399 | + TestFilePath = $Node.TestSqlScriptPath2 |
| 400 | + SetFilePath = $Node.SetSqlScriptPath2 |
| 401 | + Variable = @( |
| 402 | + ('DatabaseName={0}' -f $Node.Database4Name) |
| 403 | + ) |
| 404 | + QueryTimeout = 30 |
| 405 | + Encrypt = 'Optional' |
| 406 | + |
| 407 | + PsDscRunAsCredential = New-Object ` |
| 408 | + -TypeName System.Management.Automation.PSCredential ` |
| 409 | + -ArgumentList @($Node.Admin_UserName, (ConvertTo-SecureString -String $Node.Admin_Password -AsPlainText -Force)) |
| 410 | + |
| 411 | + DependsOn = @( |
| 412 | + '[xScript]CreateFile_GetSqlScript' |
| 413 | + '[xScript]CreateFile_TestSqlScript' |
| 414 | + '[xScript]CreateFile_SetSqlScript' |
| 415 | + ) |
| 416 | + } |
| 417 | + } |
| 418 | +} |
| 419 | + |
| 420 | +<# |
| 421 | + .SYNOPSIS |
| 422 | + Remove the database created from the combined configuration test. |
| 423 | +#> |
| 424 | +Configuration DSC_SqlScript_RemoveDatabase4_Config |
| 425 | +{ |
| 426 | + Import-DscResource -ModuleName 'SqlServerDsc' |
| 427 | + |
| 428 | + node $AllNodes.NodeName |
| 429 | + { |
| 430 | + SqlDatabase 'RemoveDatabase4' |
| 431 | + { |
| 432 | + Ensure = 'Absent' |
| 433 | + ServerName = $Node.ServerName |
| 434 | + InstanceName = $Node.InstanceName |
| 435 | + Name = $Node.Database4Name |
| 436 | + |
| 437 | + PsDscRunAsCredential = New-Object ` |
| 438 | + -TypeName System.Management.Automation.PSCredential ` |
| 439 | + -ArgumentList @($Node.Admin_UserName, (ConvertTo-SecureString -String $Node.Admin_Password -AsPlainText -Force)) |
| 440 | + } |
| 441 | + } |
| 442 | +} |
| 443 | + |
297 | 444 | <# |
298 | 445 | .SYNOPSIS |
299 | 446 | Remove the database created from the DisabledVariables test. |
|
0 commit comments