I have a SQL Agent job that is launched on a WMI file creation event. However, when the job runs, it is unable to retrieve a WMI property of the target instance (the created file).
Here is the SQL Agent WMI alert:
EXEC msdb.dbo.sp_add_alert @name=N'DocumentImportNewFileAlert',
@message_id=0,
@severity=0,
@enabled=1,
@delay_between_responses=0,
@include_event_description_in=0,
@category_name=N'[Uncategorized]',
@wmi_namespace=N'\\.\root\CIMV2',
@wmi_query=N'SELECT *
FROM __InstanceCreationEvent
WITHIN 10
WHERE TargetInstance ISA ''CIM_DataFile''
AND TargetInstance.Drive= ''c:''
AND TargetInstance.Path= ''\\DocumentImport\\''',
@job_id=N'bd09c2a5-325e-47b3-96bc-d3ab59f0a7d6'
Here is the T-SQL code in the job step:
DECLARE @Msg varchar(1000)
SET @Msg = 'File is ' + '$(ESCAPE_SQUOTE(WMI(Name)))'
PRINT @Msg
The error message from the job log is:
MessageUnable to start execution of step 1 (reason: Error retriving WMI variable WMI(Name): 0x80041002). The step failed.
I had the same notification query running in WBEMTEST at the time, and it fired also, providing detail about the target instance:
instance of CIM_DataFile{
AccessMask = 2032127;
Archive = TRUE;
Caption = "c:\\documentimport\\test1.txt";
Compressed = FALSE;
CreationClassName = "CIM_LogicalFile";
CreationDate = "20130614192328.974491-240";
CSCreationClassName = "Win32_ComputerSystem";
CSName = "MyServer1";
Description = "c:\\documentimport\\test1.txt";
Drive = "c:";
EightDotThreeFileName = "c:\\documentimport\\test1.txt";
Encrypted = FALSE;
Extension = "txt";
FileName = "test1";
FileSize = "0";
FileType = "Text Document";
FSCreationClassName = "Win32_FileSystem";
FSName = "NTFS";
Hidden = FALSE;
InstallDate = "20130614192328.974491-240";
LastAccessed = "20130616225922.488482-240";
LastModified = "20130616225922.488482-240";
Name = "c:\\documentimport\\test1.txt";
Path = "\\documentimport\\";
Readable = TRUE;
Status = "OK";
System = FALSE;
Writeable = TRUE;
};
As you can see, "Name" is a property of the target instance which fired the event.
I have enabled "Replace tokens for all job responses to alerts" for SQL Agent, and the service has been restarted.
Using WMI(TIME_CREATED) works. This is a top-level property of the WMI __InstanceCreationEvent. So is TargetInstance, but it is an object type rather than an value type:
instance of __InstanceCreationEvent{
TargetInstance = instance of CIM_DataFile
TIME_CREATED = "130159750745781304";
};
I have tried using WMI(TargetInstance.Name), but that syntax is not recognized.
Does anyone know of a way to access the properties of an object with the WMI() macro token in a SQL Agent job step? Or is this beyond the capabilities of WMI()?