我在 SQL Server 2008R2 机器的 Powershell 脚本下运行。该脚本的目的是一次性在所有提到的 SQL Server 实例上远程创建 SQL 作业。服务器名称、实例名称和端口号已通过记事本提及。它能够在 2008R2 实例上执行此操作,但不能在 SQL Server 2012 上执行此操作。我一直怀疑 powershell 脚本是否取决于 SQL Server 版本?
如果有人可以帮助我消除疑虑并解决问题,那就太好了。
错误信息:
以下错误消息不断重复我们为作业设置的所有参数,例如 OwnerLoginName、Subsystem、Command 等
新对象:使用“2”参数调用“.ctor”的异常:“SetParent failed for Job 'SQL Services Restarted Aler” At E:\Sachin\create-SQLJOB_AllInstances_V1.ps1:18 char:16 + $j = new-object <<<< ('Microsoft.SqlServer.Management.Smo.Agent.Job') ($s.JobServer,'SQL Services Restarted Aler + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
在此对象上找不到属性“描述”;确保它存在并且可设置。在 E:\Sachin\create-SQLJOB_AllInstances_V1.ps1:19 char:4 + $j。<<<< Description = 'Alert to identify the SQL Services Restart' + CategoryInfo : InvalidOperation: (Description:String) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
您不能对空值表达式调用方法。在 E:\Sachin\create-SQLJOB_AllInstances_V1.ps1:21 char:10 + $j.Create <<<< () + CategoryInfo : InvalidOperation: (Create:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
提前致谢。`
function CreateSQLJob{
param (
[string]$svr,
[string]$inst,
[string]$port
)
foreach ($instancename in $inst)
{
$ConnectionString = "data source = $inst,$port; initial catalog = master; trusted_connection = true;"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server')
$s.ConnectionContext.ConnectionString = $ConnectionString
$j = new-object ('Microsoft.SqlServer.Management.Smo.Agent.Job') ($s.JobServer,'SQL Services Restarted Alert')
$j.Description = 'Alert to identify the SQL Services Restart'
$j.OwnerLoginName = 'sa'
$j.Create()
$js = new-object ('Microsoft.SqlServer.Management.Smo.Agent.JobStep') ($j,'Step 01')
$js.SubSystem = 'TransactSql'
$js.Command = "IF(SELECT DATEDIFF(MI,Crdate,GETDATE()) FROM master.dbo.sysdatabases WHERE NAME='TEMPDB')<=2
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name='XXX',
@recipients=N'[email protected]',
@subject=N'SQL Services Restarted on on Server - $inst',
@body=N'This is an informational message only: SQL services possibly restarted on this server. Please restart any dependent application services after verifying status with DBA Team first.'
END"
$js.OnSuccessAction = 'QuitWithSuccess'
$js.OnFailAction = 'GoToStep'
$js.OnFailStep=2
$js.Create()
$SQLJobStep2 = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Agent.JobStep -argumentlist $j,"Failure Notification"
$SQLJobStep2.Command = "EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'XXX',
@recipients='[email protected]',
@subject = 'SQL Services Restarted Alert job - FAILED on $inst',
@body = 'Hi Team,<br><br>
SQL Services Restarted Alert job failed . <br><br>
Thank You.',
@body_format = 'HTML' ;"
$SQLJobStep2.DatabaseName = "master"
$SQLJobStep2.OnSuccessAction = "QuitWithFailure"
$SQLJobStep2.OnFailAction = "QuitWithFailure"
$SQLJobStep2.Create()
$jsid = $js.ID
$j.ApplyToTargetServer($inst)
$j.StartStepID = $jsid
$j.Alter()
$jsch = new-object ('Microsoft.SqlServer.Management.Smo.Agent.JobSchedule') ($j,'Sched 01')
$jsch.FrequencyTypes = 'AutoStart'
$jsch.ActiveStartDate = get-date
$jsch.Create()
write-host "SQL Services Restarted Alert Job created on server $inst"
}
}
$servers = Get-Content 'E:\Sachin\SQL_Servers1.txt'
foreach ($sv in $servers) {
$srvr = $sv.Split(",")
$server = $srvr[0]
$instance = $srvr[1]
$port = $srvr[2]
CreateSQLJob $server $instance $port
}`
我首先在装有 SQL Server 2008R2 的同一台机器上安装了 SSMS 2014。
然后我为 SQL Server 2014 安装了 Powershell Provider。
之后,我也可以从更高版本的 SQL Server(如 2012)中获取详细信息。
无需更新 Powershell。