通过powershell创建审计失败," SetParent失败"

时间:2014-06-17 11:36:39

标签: powershell sql-server-2008-r2 audit

我尝试使用以下PowerShell脚本在WinServer 2008 R2上创建新的服务器审核。

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null 
$server = "SM1111" #change to desired instance
$instance = "S111"
$auditName = "$instance"+"TestAudit"
$auditDir = 'F:\Microsoft SQL Server\'+$instance+'AuditTestLogsNew\'
$srv = new-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist $instance

$newAudit = New-Object Microsoft.SqlServer.Management.Smo.Audit($srv, "$auditName")
$newAudit.DestinationType = [Microsoft.SqlServer.Management.Smo.AuditDestinationType]::File
$newAudit.FilePath = $auditDir
$newAudit.MaximumRolloverFiles = 10
$newAudit.MaximumFileSize = 100
$newAudit.QueueDelay = 1000
$newAudit.Create()
$newAudit.Enable()

但是以下行始终失败:

$newAudit = New-Object Microsoft.SqlServer.Management.Smo.Audit($srv, "$auditName")

我收到以下错误消息:

New-Object : Exception calling ".ctor" with "2" argument(s): "SetParent failed for Audit 'S111TestAudit'. "
At MYFOLDER\Documents\Auditing_Test\CreateAudit.ps1:9 char:13
+ $newAudit = New-Object Microsoft.SqlServer.Management.Smo.Audit($srv, "$auditNam ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

我一直在谷歌搜索,但仍然找不到任何可能解决问题的方法,因为我不太明白是什么引发了错误。

我拥有完全的管理员权限。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您缺少实例的服务器名称。您的变量$srv未指向实际的服务器实例。

$server = "SM1111" #change to desired instance     <- This isn't doing anything
$instance = "S111"
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server -argumentlist "$server\$instance"
相关问题