Advertisement
Sp00ny

PS scripts Spiceworks

Mar 22nd, 2017
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Dir of Ticket uploads
  2. $dir1 = "C:\Program Files (x86)\Spiceworks\data\uploads\Ticket"
  3. # Dir of unwanted files from signatures
  4. $dir2 = "C:\Program Files (x86)\Spiceworks\data\uploads\Hash"
  5.  
  6. # Get-FileHash out of Ticket uploads folder, exclude pdf files
  7. $var1 = get-childitem -path $dir1 -Recurse | where name -NotLike "*.PDF" | Get-FileHash -Algorithm MD5
  8. # Get-FileHash out of unwanted files from signatures
  9. $var2 = get-childitem -path $dir2 | Get-FileHash -Algorithm MD5
  10.  
  11. # compare both hashes if Ticket uploads matches unwanted files from signatures mark for deletion
  12. $marked4Deletion = foreach ($attachment in $var1)
  13. {
  14. if ($var2.Hash -contains $attachment.Hash)
  15. {$attachment.Path}
  16. }
  17. # if there is nothing to delete, stop the script from executing further steps
  18. if ($marked4Deletion -eq $null)
  19. {return "nothing to delete"}
  20.  
  21. # delete unwanted files from $dir2
  22. $marked4Deletion | Remove-Item -Force
  23.  
  24. # Get filenames only
  25. $marked4Deletion = foreach ($q in $marked4Deletion) {Split-Path $q -leaf}
  26. #split on comment_id
  27. $marked4Deletion = foreach ($q in $marked4Deletion) {$q.Split([string[]]"-",[StringSplitOptions]"None")[0]}
  28. # Prepare where clause
  29. $whereclause = $marked4Deletion -join ','
  30. # create sql statment for set deletion
  31. $sqlstatement = "delete from comments where id in ($whereclause)"
  32.  
  33.  
  34. # connect to database
  35. $conn = new-object System.Data.Odbc.OdbcConnection
  36. $conn.connectionstring = "DSN=Spiceworks"
  37. $conn.open()
  38. # delete the comments
  39. $cmd = New-object System.Data.Odbc.OdbcCommand($sqlstatement,$conn)
  40. $dataset = New-Object System.Data.DataSet
  41. (New-Object System.Data.Odbc.OdbcDataAdapter($cmd)).Fill($dataSet)
  42. $conn.Close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement