Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Using WMI Permanent requires VBScript? (Automating NTFS permissions)

$
0
0

Hello,

I'm relatively new to using WMI. It certainly seems powerful, but dizzyingly complex. I've got a script using FileSystemWatcher that monitors a folder and its children for files added by an application, and it resets the NTFS inheritance on those items (they're copied from a temp folder with different perms, so they don't get the correct permissions when moved to the target).

The FileSystemWatcher script is reasonably effective, but it seems to randomly stop working (despite the eventsubscriber still being loaded according to PowerShell). Plus, this is intended to run on a fileserver, so we don't want to leave a session logged in on the machine just to run this PS script. So, I figure a permanent WMI object is the way to go here.

I've got the script converted to use Register-WmiEvent, and this works, but as I understand it, this still isn't saving it as a permanent WMI object, so when I close PowerShell, this will still get unloaded.

$wmiNewFileAddedQuery = @"
SELECT * FROM __InstanceCreationEvent within 1
WHERE TargetInstance ISA 'Cim_DirectoryContainsFile'
AND TargetInstance.GroupComponent = 'Win32_Directory.Name="d:\\\\filedrop"'"@

Register-WmiEvent -Query $wmiNewFileAddedQuery -MessageData "New file added to local cloud." -SourceIdentifier "New File Added To Local Cloud" -Action {
    $PartComponent = $Event.SourceEventArgs.NewEvent.TargetInstance.GetPropertyValue("PartComponent")
    $startPos = $PartComponent.IndexOf("CIM_DataFile.Name=") + 19
    $filepath = $PartComponent.Substring($startPos, ($PartComponent.Length - $startPos - 1))
    $acl = Get-ACL $filepath
    $acl.SetAccessRuleProtection($false, $false)
    $acl | Set-ACL"Event fired" | Out-File D:\zzzzzzzzzzzzzzzzzzzzzzzzz.txt
}

The last line is just so that I know the event triggered, since I was having trouble getting it to work at first.

Looking at Ed Wilson's "Use PowerShell to Create a Permanent WMI Event to Launch a VBSscript" article, it looks like the Permanent WMI registrations will only run VBscript, but if that's the case, then I lose my event context and this becomes little different from just running the permission reset on a timer. Can you use Permanent WMI events to run inline PowerShell actions instead?

What is my best option for making this code survive a reboot?


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>