$currentDir = $PSScriptRoot
$wshell = New-Object -ComObject Wscript.Shell
$popupTitle = "Importowanie certyfikatu do weryfikacji plików podpisanych cyfrowo"

if (![Environment]::Is64BitProcess)
{
  $wshell.Popup("Skrypt został uruchomiony w 32-bitowej wersji PowerShell. Wymagana jest wersja 64-bitowa", 0, $popupTitle, 16)

  Exit 1
}

$myWindowsPrincipal = new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())

if (!$myWindowsPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
{
  Exit (Start-Process PowerShell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb runAs -WindowStyle Hidden -Wait -PassThru).ExitCode
}

try
{
  $certName = "TelWin SCADA"
  $certFileName = "TelWin_SCADA"
  $certPath = $currentDir + "\" + $certFileName + ".p7b"
  $pathExists = Test-Path $certPath

  if ($pathExists -eq $False)
  {
    $wshell.Popup("Błąd`: Brak pliku z certyfikatem " + $certPath, 0, $popupTitle, 16)

    Exit 10
  }

  $certNamePattern = "*CN=" + $certName + "*"
  $certStoreLocation = "Cert:\LocalMachine"
  $certList = Get-ChildItem $certStoreLocation -Recurse | Where-Object Subject -like $certNamePattern

  foreach ($cert in $certList)
  {
    if ($cert.Thumbprint -ne $certPfx.Thumbprint)
    {
      $cert | Remove-Item
    }
  }


  $certNamePattern = "*CN=" + "TEL-STER Root CA v3" + "*"
  $certList = Get-ChildItem $certStoreLocation -Recurse | Where-Object Subject -like $certNamePattern

  foreach ($cert in $certList)
  {
    if ($cert.Thumbprint -ne $certPfx.Thumbprint)
    {
      $cert | Remove-Item
    }
  }
  
  $certNamePattern = "*CN=" + "TEL-STER CodeSigning CA v1" + "*"
  $certList = Get-ChildItem $certStoreLocation -Recurse | Where-Object Subject -like $certNamePattern

  foreach ($cert in $certList)
  {
    if ($cert.Thumbprint -ne $certPfx.Thumbprint)
    {
      $cert | Remove-Item
    }
  }


  $certStoreLocation = "Cert:\LocalMachine\TrustedPublisher"

  Import-Certificate -FilePath "$certPath" -CertStoreLocation $certStoreLocation

  $certStoreLocation = "Cert:\LocalMachine\Root"

  Import-Certificate -FilePath "$certPath" -CertStoreLocation $certStoreLocation

  $wshell.Popup("Operacja zakończona poprawnie.", 0, $popupTitle, 4160)
}
catch
{
  $wshell.Popup("Błąd`: $($PSItem.ToString())", 0, $popupTitle, 16)

  Exit 100
}

Exit 0

# SIG # Begin signature block
# MIIP5QYJKoZIhvcNAQcCoIIP1jCCD9ICAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCBGnblU+YPQ/KJO
# 8v1AmFRzGX56jMZrObF8tTqowKVUkqCCDQYwggWzMIIDm6ADAgECAgEBMA0GCSqG
# SIb3DQEBCwUAMH0xCzAJBgNVBAYTAlBMMREwDwYDVQQKEwhURUwtU1RFUjEUMBIG
# A1UECxMLVEVMLVNURVIgQ0ExIzAhBgNVBAMTGlRFTC1TVEVSIENvZGVTaWduaW5n
# IENBIHYxMSAwHgYJKoZIhvcNAQkBFhFiaXVyb0B0ZWwtc3Rlci5wbDAeFw0yMTAy
# MTAyMjExMzVaFw0zMTAyMDgyMjExMzVaMGYxCzAJBgNVBAYTAlBMMREwDwYDVQQK
# EwhURUwtU1RFUjELMAkGA1UECxMCSVQxFTATBgNVBAMTDFRlbFdpbiBTQ0FEQTEg
# MB4GCSqGSIb3DQEJARYRYml1cm9AdGVsLXN0ZXIucGwwggEiMA0GCSqGSIb3DQEB
# AQUAA4IBDwAwggEKAoIBAQCk8p1f0HoDXhMp5Q5+vX2bhdaXmwJcnKhFRZjYkA4k
# 7nEmlAmFWAnPwffi8TTCiW5waJOSH6nsBVBfB9OP29jnZWnsJ3a6mlQyRkC/GH8y
# 7kkbQ41WkjtCQMxvIvyH6ZIt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