Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

# Path and filter settings
$path = "C:\sign"
$filter = "*.*"


# Ensure the path exists
if (!(Test-Path $path)) { 
    Write-Host "Path '$path' does not exist!" 
    return 
}


# The script block called when files are created
$action = { 
    $signtoolPath = "C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\signtool.exe"
    $filePath = $Event.SourceEventArgs.FullPath
    $arguments = "sign /debug /sha1 f90be6d6ba25c388a384189ba5cd7975a3a04389 /v /td fd SHA256 $filePath"
    Write-Host "Signing file '$filePath'"
    #Start-Process -FilePath $signtoolPath -ArgumentList $arguments -NoNewWindow
    Invoke-Expression "& '$signtoolPath' $arguments"
}


$sourceIdentifier = "FileCreated"


# Unregister the event if it is already registered
try {
    $existingEvent = Get-EventSubscriber -SourceIdentifier $sourceIdentifier -ErrorAction Stop
    if ($null -ne $existingEvent) {
        Unregister-Event -SourceIdentifier $sourceIdentifier
    }
}
catch {
    Write-Host "Event not found. Registering the event."
}


# Create the FileSystemWatcher
$fsw = New-Object IO.FileSystemWatcher $path, $filter
$fsw.EnableRaisingEvents = $true


$job = Register-ObjectEvent $fsw Created -SourceIdentifier $sourceIdentifier -Action $action


# Validate if the event is actually registered
if (Get-EventSubscriber | Where-Object { $_.SourceIdentifier -eq $sourceIdentifier })
{
    Write-Host "Event has been registered successfully."
}
else
{
    Write-Host "Failed to register event."
}


Write-Host "Script is now monitoring $path."



# Copy test.exe to the sign folder
$testExePath = "C:\test.exe"
if (Test-Path $testExePath) {
    Copy-Item $testExePath -Destination $path
    Write-Host "test.exe copied to $path."
} else {
    Write-Host "Could not find $testExePath."
}



# Prevent the console from closing immediately
do {
    Start-Sleep -Seconds 1
} while ($true)


...