События.NET для Процесса исполняемый запуск

Вы можете вывести значение объекта, используя точечные обозначения.

$var.Access.IdentityReference

или это ..

$($var.Access).IdentityReference

Что касается этого ...

получить значение каждого атрибута отдельным способом ...

что это значит?

Если вы хотите отделить эти два ценностных элемента друг от друга ...

attribute1: value1
attribute2: value22
attribute3: value33
attribute4: value44

attribute1: value2
attribute2: value222
attribute3: value333
attribute4: value444

...

Вы можете разделить на ':' и выбрать нужную позицию, которую вы хотите.

$DBValues = @'
    attribute1: value1
    attribute2: value22
    attribute3: value33
    attribute4: value44

    attribute1: value2
    attribute2: value222
    attribute3: value333
    attribute4: value444
'@

$DBValues -split ': '

Или выберите значение, используя RegEx ... Пример:

# Grab the string value based on the match
[regex]::Matches($DBValues,'\svalue.*').Value

# Grab the string value after the match
[regex]::Matches($DBValues,'(?<=attribute.*[:]\s).*?(?=\s)').Value

Что касается этого ...

присваивает их переменной Powershell

... вы не можете передавать данные, пока не настроите их в своем скрипте в

param
(
  [Parameter(mandatory=$true, ValueFromPipeline=$true)]$InputObject,
  [Parameter(mandatory=$true)]$FileName
)
...
19
задан Adam Haile 11 May 2009 в 15:23
поделиться

2 ответа

You could use the following:

    private ManagementEventWatcher WatchForProcessStart(string processName)
    {
        string queryString =
            "SELECT TargetInstance" +
            "  FROM __InstanceCreationEvent " +
            "WITHIN  10 " +
            " WHERE TargetInstance ISA 'Win32_Process' " +
            "   AND TargetInstance.Name = '" + processName + "'";

        // The dot in the scope means use the current machine
        string scope = @"\\.\root\CIMV2";

        // Create a watcher and listen for events
        ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
        watcher.EventArrived += ProcessStarted;
        watcher.Start();
        return watcher;
    }

    private ManagementEventWatcher WatchForProcessEnd(string processName)
    {
        string queryString =
            "SELECT TargetInstance" +
            "  FROM __InstanceDeletionEvent " +
            "WITHIN  10 " +
            " WHERE TargetInstance ISA 'Win32_Process' " +
            "   AND TargetInstance.Name = '" + processName + "'";

        // The dot in the scope means use the current machine
        string scope = @"\\.\root\CIMV2";

        // Create a watcher and listen for events
        ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
        watcher.EventArrived += ProcessEnded;
        watcher.Start();
        return watcher;
    }

    private void ProcessEnded(object sender, EventArrivedEventArgs e)
    {
        ManagementBaseObject targetInstance = (ManagementBaseObject) e.NewEvent.Properties["TargetInstance"].Value;
        string processName = targetInstance.Properties["Name"].Value.ToString();
        Console.WriteLine(String.Format("{0} process ended", processName));
    }

    private void ProcessStarted(object sender, EventArrivedEventArgs e)
    {
        ManagementBaseObject targetInstance = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
        string processName = targetInstance.Properties["Name"].Value.ToString();
        Console.WriteLine(String.Format("{0} process started", processName));
    }

You would then call either WatchForProcessStart and/or WatchForProcessEnd passing in your process name (eg "notepad.exe").

The ManagementEventWatcher object is returned from the two Watch* methods as it implements IDisposable and so you should call Dispose on these objects when you have finished with them to prevent issues.

You could also change the polling value in the queries if you need the event to be raised more quickly after the process has started. To do this change the line "WITHIN 10" to be WITHIN something less than 10.

30
ответ дан 30 November 2019 в 03:53
поделиться

WMI может создавать события при создании процессов. Затем вы можете отфильтровать эти события.

2
ответ дан 30 November 2019 в 03:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: