使用SMO修改数据和日志文件属性

时间:2016-02-29 17:00:45

标签: powershell

我一直在努力为一堆服务器自动化一些SQL Server的安装后配置设置。其中一项任务是将数据库文件的默认增长和growthtype从百分比更改为KB / MB。

对于测试,我将只使用单个实例和测试数据库。这就是我到目前为止所提出的。

$sqlServer = "SQLSVRNM\INSTNM"

#Load SqlServer SMO assembly
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")

#Connect to the instance using SMO
$smosvr = New-Object Microsoft.SqlServer.Management.Smo.Server $sqlServer

#Filter database, assign db properties to variable
$database = $smosvr.databases | where name -eq "testdb"

#Check database size
$dbsize = $database.size

#Check database name
$name = $database.name

#Get logfile properties
$logfile = $database.logfiles

#Get logfile name
$logfileName = $logfile.filename

#Get file growth val
$logfileGrowth = $logfile.growth

#Get growthtype
$logfileGrowthtype = $logfile.growthtype

#Get logfile size 
$logfileSize = $logfile.size

当我使用

设置新的增长大小时
$logfile.growth = [double](2.0 * 32.0)

我收到以下错误。

The property 'growth' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1
+ $logfile.growth = [double](2.0 * 32.0)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException

但是,如果我在$ logfile上执行get-member,则属性存在。

$logfile | Get-Member -Name Growth*


TypeName: Microsoft.SqlServer.Management.Smo.LogFile

Name       MemberType Definition
----       ---------- ----------
Growth     Property   double Growth {get;set;}
GrowthType Property   Microsoft.SqlServer.Management.Smo.FileGrowthType GrowthType {get;set;}

那么,有人可以帮助我理解我在这里可能缺少的东西吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

代码中的$ logfile是一个集合。即使只有一个你必须打破它来设置属性

相关问题