2010-05-12 2 views
1

데이터베이스에서 스크립트를 생성하려고하는데 이러한 개체를 약간 수정하면됩니다. 예를 들어 저장 프로 시저에 대한 스크립트를 생성하려고하지만 스크립트에서 다른 스키마로 스크립트를 생성하려고합니다.PowerShell에서 SMO를 사용하여 약간 변경된 개체를 스크립팅하는 방법은 무엇입니까?

나는 아래에서 현재 시도하고있는 예를 가지고있다. 완전히 잘못된 방향으로 가고 있다면 올바른 방향으로 나를 가리켜주세요. 나는 잠재적 인 위험을 많이 가지고 있기 때문에 어떤 종류의 문자열 검색을 수행하고 바꾸고 싶지 않습니다.

내가 시도한 것은 데이터베이스의 기존 함수에서 함수 개체를 만든 다음 새 개체를 만들어 방금 만든 개체의 복사본으로 만들려고합니다. 이것은 메모리에 있어야하고 데이터베이스에 연결되어 있지 않아야하므로 스키마를 변경할 수 있어야합니다. 그런 다음 스키마를 변경하고 스크립트를 작성하려고합니다. 불행히도 PowerShell에서 오류가 발생하지 않는 .Script 구문을 찾을 수 없었습니다.

$instance = $args[0] # Gets the instance name from the command line parameters 
$database = $args[1] # Gets the database name from the command line parameters 

# Import the SMO assembly 
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null 

# Create an instance for the SQL Server 
$serverInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance 

# Create an instance for the Database 
$databaseInstance = $serverInstance.Databases[$database] 

# Loop through all of the user defined functions (that are not system objects) in the database 
foreach ($function in $databaseInstance.UserDefinedFunctions | Where-Object {$_.IsSystemObject -eq $False}) 
{ 
    $newFunction = New-Object ('Microsoft.SqlServer.Management.Smo.UserDefinedFunction') ($databaseInstance, $function.Name, "tnf_dbo_func") 

    $newFunction.TextHeader = $function.TextHeader 
    $newFunction.TextBody = $function.TextBody 

    $newFunction.TextMode = $False 
    $newFunction.ChangeSchema("tnf_dbo_func") 
    $newFunction.TextMode = $True 

    Write-Host $newFunction.Schema 
    Write-Host $newFunction.TextHeader 
    Write-Host $newFunction.TextBody 
    $newFunction.Script() 
} 

답변

3

나는 내 질문에 타이핑 했으므로 .TextMode 설정은 내가 시도한 다른 해결책에서 제자리에 있다는 것을 깨달았다. 그것들을 제거하면 문제가 해결되었습니다. 질문에 이미 입력 한 이후로 나는 다른 사람을 도울 경우를 대비하여 어쨌든 그것을 게시 할 것이라고 생각했습니다.

수정 된 코드 :

$instance = $args[0] # Gets the instance name from the command line parameters 
$database = $args[1] # Gets the database name from the command line parameters 

# Import the SMO assembly 
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null 

# Create an instance for the SQL Server 
$serverInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance 

# Create an instance for the Database 
$databaseInstance = $serverInstance.Databases[$database] 

# Loop through all of the user defined functions (that are not system objects) in the database 
foreach ($function in $databaseInstance.UserDefinedFunctions | Where-Object {$_.IsSystemObject -eq $False}) 
{ 
    $newFunction = New-Object ('Microsoft.SqlServer.Management.Smo.UserDefinedFunction') ($databaseInstance, $function.Name, "tnf_dbo_func") 

    $newFunction.TextHeader = $function.TextHeader 
    $newFunction.TextBody = $function.TextBody 

    $newFunction.ChangeSchema("tnf_dbo_func") 

    Write-Host $newFunction.Schema 
    Write-Host $newFunction.TextHeader 
    Write-Host $newFunction.TextBody 
    $newFunction.Script() 
} 
관련 문제