我在SSIS中尝试做的是有一个WMI事件监视器任务,它监视一个文件夹以创建一个文件,然后用它做一些事情。主要部分是“观看文件创建文件夹”。
我有一个网络文件夹(完整路径):\\srvblah10\main\child\target\
我去过的所有网站都以此为例:
SELECT * FROM __InstanceCreationEvent WITHIN 10
WHERE TargetInstance ISA "CIM_DirectoryContainsFile"
AND TargetInstance.GroupComponent = "Win32_Directory.Name=\"d:\\\\NewFiles\""
由于文件夹是网络文件夹,我无法提供物理磁盘信件。那么有没有办法使用类似的WQL查询但是对于网络文件夹路径而不是物理文件夹路径?
答案 0 :(得分:0)
您必须使用dos命令映射驱动器: net use s:\ srvblah10 \ main \ child \ target \ / user dotnetN00b pa $$ word
然后你可以WMI Event Watcher Task观看它。
答案 1 :(得分:0)
我试图这样做一段时间,最后放弃尝试使用SSIS WMI事件观察器任务,并在Script任务中编写了等效内容。挑战的问题是让WMI事件监视器使用我想从配置部分获得的特定用户凭证进行远程连接(而不是硬件代码到包中)。
第二个不使用脚本困难的问题只是将网络共享转换为服务器上的本地路径名,即事件监视器所需的名称。您将从下面的脚本中看到,一切都是通过最小的努力完成的。
只需要额外的抬头,确保包含脚本在ReadOnlyVariables中使用的DTS.Variables(正常情况下)。下面的代码需要三个DTS变量,例如,如果您尝试在以下位置 \ copernicus \ dropoff \ SAP \ Import 中查看要删除的文件,那么您将设置变量,如下所示:
public void Main()
{
string localPath = "";
try
{
ConnectionOptions connection = new ConnectionOptions();
connection.Username = "<valid username here>";
connection.Password = "<password here>";
connection.Authority = "ntlmdomain:<your domain name here>";
ManagementScope scope = new ManagementScope(@"\\" + Dts.Variables["User::FileServerName"].Value.ToString() + @"\root\CIMV2", connection);
scope.Connect();
/// Retrieve the local path of the network share from the file server
///
string queryStr = string.Format("SELECT Path FROM Win32_Share WHERE Name='{0}'", Dts.Variables["User::ShareName"].Value.ToString());
ManagementObjectSearcher mosLocalPath = new ManagementObjectSearcher(scope, new ObjectQuery(queryStr));
foreach (ManagementObject elements in mosLocalPath.Get())
{
localPath = elements["Path"].ToString();
}
queryStr = string.Format(
"SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent=\"Win32_Directory.Name='{0}{1}'\"",
localPath.Replace(@"\", @"\\"),
Dts.Variables["User::ImportPath"].Value.ToString().Replace(@"\", @"\\")); // query requires each seperator to be a double back slash
ManagementEventWatcher watcher = new ManagementEventWatcher(scope, new WqlEventQuery(queryStr));
ManagementBaseObject eventObj = watcher.WaitForNextEvent();
// Cancel the event subscription
watcher.Stop();
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (ManagementException err)
{
Dts.Events.FireError((int)err.ErrorCode, "WMI File Watcher", "An error occurred while trying to receive an event: " + err.Message, String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
catch (System.UnauthorizedAccessException unauthorizedErr)
{
Dts.Events.FireError((int)ManagementStatus.AccessDenied, "WMI File Watcher", "Connection error (user name or password might be incorrect): " + unauthorizedErr.Message, String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}