Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- This function is called when the service is started. Once this function returns,
- your service will be set to a Running state.
- #>
- function OnStart() {
- Start-Transcript C:\Temp\Main.log
- $SNID = (Get-Ciminstance -class win32_bios).SerialNumber
- $MeterFolder = "$Env:ProgramData\PSEM\Metering"
- $QueueFolder = "$Env:ProgramData\PSEM\Metering\Queue"
- try {
- Resolve-Path $QueueFolder -ErrorAction Stop
- }
- catch {
- New-Item -Path $QueueFolder -ItemType Directory -Force
- }
- function Enable-ProcessTrace {
- [CmdLetBinding()]
- param(
- $Interval = 3.
- )
- Start-Transcript C:\Temp\ProcessTrace.log
- $Query = "Select * From __InstanceCreationEvent within $Interval Where TargetInstance ISA 'Win32_Process'"
- $Identifier = "StartProcess"
- $ActionBlock = {
- $e = $event.SourceEventArgs.NewEvent.TargetInstance
- If ($MatchProcess -contains $e.name) {
- $eventObj = [PSCustomObject]@{
- Name = $E.Name
- Path = $E.ExecutablePath
- Username = (Get-Process -Id $E.ProcessID -IncludeUserName).Username
- CommandLine = $E.CommandLine
- snID = $SNID
- Timestamp = Get-Date
- }
- $Data = $EventObj | ConvertTo-Json
- If (([ADSI]"LDAP://RootDSE").dnsHostName -ne $Null) {
- Try {
- Resolve-Path $QueueFolder\Queue.json -ErrorAction Stop
- $MeterObj = Get-Content $QueueFolder\Queue.json -raw -ErrorAction Stop
- }
- Catch {
- Remove-Variable MeterObj
- }
- If ($MeterObj) {
- try {
- Invoke-RestMethod https://PowershellUniversalServer.com/endmgmt/meter/Post -Method POST -Body $MeterObj -ErrorAction Stop
- Remove-Item $QueueFolder\Queue.json -Force
- }
- catch {
- }
- }
- Try {
- Invoke-RestMethod https://PowershellUniversalServer.com/endmgmt/meter/Post -Method POST -Body $Data -ErrorAction Stop
- }
- catch {
- If ($MeterObj) {
- $NewObj = New-Object System.Collections.Arraylist
- $MeterObj = $MeterObj | ConvertFrom-Json
- $MeterObj | Foreach {
- $NewObj.Add($_)
- }
- $NewObj.Add($EventObj)
- $NewObj | ConvertTo-Json | Out-File $QueueFolder\Queue.json
- }
- Else {
- $Data | Out-File $QueueFolder\Queue.json
- }
- }
- }
- Else {
- Try {
- Resolve-Path $QueueFolder\Queue.json -ErrorAction Stop
- $MeterObj = Get-Content $QueueFolder\Queue.json -raw -ErrorAction Stop
- }
- Catch {
- }
- If ($MeterObj) {
- $NewObj = New-Object System.Collections.Arraylist
- $MeterObj = $MeterObj | ConvertFrom-Json
- $MeterObj | Foreach {
- $NewObj.Add($_)
- }
- $NewObj.Add($EventObj)
- $NewObj | ConvertTo-Json | Out-File $QueueFolder\Queue.json
- }
- Else {
- $Data | Out-File $QueueFolder\Queue.json
- }
- }
- }
- }
- Register-WMIEvent -Query $Query -SourceIdentifier $Identifier -Action $ActionBlock
- }
- Function Register-Watcher {
- param ($folder, $file)
- $watcher = New-Object IO.FileSystemWatcher $folder, $file -Property @{
- IncludeSubdirectories = $false
- EnableRaisingEvents = $true
- }
- Start-Transcript C:\Temp\FileWatcher.log
- $changeAction = [scriptblock]::Create('
- # This is the code which will be executed every time a file change is detected
- $eventObj = [PSCustomObject]@{
- Name = $Event.SourceEventArgs.Name
- Path = $Event.SourceEventArgs.FullPath
- Username = $Event.SourceEventArgs.FullPath.Replace("C:\Users\", "").Replace("\AppData\Local\TechSmith\SnagIt\DataStore\SnagIt900.sdf", "").Replace("\", ""
- CommandLine = ""
- snID = $SNID
- Timestamp = $Event.TimeGenerated
- }
- $Data = $EventObj | ConvertTo-Json
- If (([ADSI]"LDAP://RootDSE").dnsHostName -ne $Null) {
- Try {
- Resolve-Path $QueueFolder\Queue.json -ErrorAction Stop
- $MeterObj = Get-Content $QueueFolder\Queue.json -raw -ErrorAction Stop
- }
- Catch {
- Remove-Variable MeterObj
- }
- try {
- Invoke-RestMethod https://PowershellUniversalServer.com/endmgmt/meter/Post -Method POST -Body $MeterObj -ErrorAction Stop
- Remove-Item $QueueFolder\Queue.json -Force
- }
- catch {
- }
- Try {
- Invoke-RestMethod https://PowershellUniversalServer.com/endmgmt/meter/Post -Method POST -Body $Data -ErrorAction Stop
- }
- catch {
- If ($MeterObj) {
- $NewObj = New-Object System.Collections.Arraylist
- $MeterObj = $MeterObj | ConvertFrom-Json
- $MeterObj | Foreach {
- $NewObj.Add($_)
- }
- $NewObj.Add($EventObj)
- $NewObj | ConvertTo-Json | Out-File $QueueFolder\Queue.json
- }
- Else {
- $Data | Out-File $QueueFolder\Queue.json
- }
- }
- }
- Else {
- Try {
- Resolve-Path $QueueFolder\Queue.json -ErrorAction Stop
- $MeterObj = Get-Content $QueueFolder\Queue.json -raw -ErrorAction Stop
- }
- Catch {
- }
- If ($MeterObj) {
- $NewObj = New-Object System.Collections.Arraylist
- $MeterObj = $MeterObj | ConvertFrom-Json
- $MeterObj | Foreach {
- $NewObj.Add($_)
- }
- $NewObj.Add($EventObj)
- $NewObj | ConvertTo-Json | Out-File $QueueFolder\Queue.json
- }
- Else {
- $Data | Out-File $QueueFolder\Queue.json
- }
- }
- ')
- Register-ObjectEvent $Watcher -EventName "Changed" -Action $changeAction
- }
- try {
- $Metering = Invoke-RestMethod https://PowershellUniversalServer.com/endmgmt/meter -ErrorAction Stop
- $Metering | ConvertTo-Json | Out-File $MeterFolder\SFTMetering.json
- }
- catch {
- $Metering = Get-Content $MeterFolder\SFTMetering.json -raw | ConvertFrom-Json
- }
- $MatchProcess = $metering.Process.exe
- Enable-ProcessTrace
- foreach ($File in $Metering.File) {
- If ($File.path -match "\*") {
- Get-ChildItem -Path $File.path.Substring(0, $File.path.indexof('*')) | foreach {
- $Path = "$($_.FullName)\$($File.path.Substring($File.path.indexof('*')+1))"
- If (Test-Path $Path) {
- Start-Job
- Register-Watcher -folder $Path -file $File.File
- }
- }
- }
- }
- }
- <#
- This function is called when the service is started. Once this function returns,
- your service will be set to a Stopped state and the process will terminate.
- #>
- function OnStop() {
- }
- # Specifies whether this service can be stopped once started
- $CanStop = $true
- <# Below is the JSON returned from Powershell Universal to the $Metering Variable
- {
- "Process": [
- {
- "Name": "Bluebeam Revu",
- "EXE": "Revu.exe",
- "Path": "C:\\Program Files\\Bluebeam Software\\Bluebeam Revu\\20\\Revu"
- },
- {
- "Name": "Adobe Acrobat DC",
- "EXE": "Acrobat.exe",
- "Path": "C:\\Program Files (x86)\\Adobe\\Acrobat DC\\Acrobat"
- },
- {
- "Name": "Visio",
- "EXE": "Visio.exe",
- "Path": "C:\\Program Files\\Microsoft Office\\root\\Office16"
- },
- {
- "Name": "Project",
- "EXE": "WINPROJ.exe",
- "Path": "C:\\Program Files\\Microsoft Office\\root\\Office16"
- },
- {
- "Name": "AutoCAD LT 2022",
- "EXE": "acadlt.exe",
- "Path": "C:\\Program Files\\Autodesk\\AutoCAD LT 2022"
- },
- {
- "Name": "AutoCAD 2022",
- "EXE": "acad.exe",
- "Path": "C:\\Program Files\\Autodesk\\AutoCAD 2022"
- }
- ],
- "File": [
- {
- "Name": "Snagit",
- "File": "SnagIt900.sdf",
- "Path": "C:\\Users\\*\\AppData\\Local\\TechSmith\\SnagIt\\DataStore"
- }
- ]
- }
- #>
Advertisement
RAW Paste Data
Copied
Advertisement