ipzipzap
Goto Top

Auf Powershell SecretStore zugreifen per Script in Aufgabenplanung

Hallo,

ich bin an einem System exemplarisch als "Administrator" angemeldet, habe hier ein gespeichertes Passwort im lokalen SecretStore und folgenden Powershell-Schnipsel:

# Start Logging to file
$dt = get-date -Format "yyyy-MM-dd_HH.mm.ss"  
$logname = ".\logs\sync_$dt.log"  
Start-Transcript $logname
# ################################################

Write-Host "---------------------------------------------------------------------"  
$securePasswordPath = ".\passwd.xml"  
Write-Host "securePasswordPath = $securePasswordPath"  

$password = Import-CliXml -Path $securePasswordPath
Write-Host "Password: $password"  
Write-Host "System.Security.SecureString: $System.Security.SecureString"  
Write-Host "---------------------------------------------------------------------"  

Unlock-SecretStore -Password $password

# ################################################
# Close Log
Stop-Transcript

Starte ich dieses auf der Console, funktioniert alles. Das Logfile dazu:

Transcript started, output file is .\logs\sync_2024-06-18_11.28.59.log
---------------------------------------------------------------------
securePasswordPath = .\passwd.xml
Password: System.Security.SecureString
System.Security.SecureString: .Security.SecureString
---------------------------------------------------------------------
**********************
PowerShell transcript end

Es steht auch das richtigte Passwort in der Variable, der weitere Code nach dem Schnipsel funktioniert problemlos.

Nun möchte ich das Script per Aufgabenplanung jede Nacht ausführen und habe dort eine Aufgabe erstellt.

Tab Allgemein:
Beim Ausführen folgendes Konto verwenden: Administrator
[x] Unabhängig von der Benutzeranmeldung ausführen
[x] Kennwort nicht speichern

Tab Aktionen:
Aktion: Programm starten
Programm: "C:\Program Files\PowerShell\7\pwsh.exe"
Argumente: -File "C:\sync\sync.ps1"
Starten in: C:\sync

Starte ich die Aufgabe nun, funktioniert sie. Das Log sieht exakt aus, wie oben und zeigt keine Fehler an. Auch die nächsten Tage funktioniert die Aufgabe. Melde ich mich jetzt von der Konsole bzw. der RDP-Session ab, also nicht "Trennen", sondern wirklich "abmelden" bzw. "Sign out", dann funktioniert das Script nicht mehr. Das Log sagt hier dann:

**********************
Transcript started, output file is .\logs\sync_2024-06-18_11.28.51.log
---------------------------------------------------------------------
securePasswordPath = .\passwd.xml
PS>TerminatingError(Import-Clixml): "Error occurred during a cryptographic operation."  
Import-Clixml: C:\sync\sync.ps1:12
Line |
  12 |  $password = Import-CliXml -Path $securePasswordPath
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Error occurred during a cryptographic operation.
Import-Clixml: C:\sync\sync.ps1:12
Line |
  12 |  $password = Import-CliXml -Path $securePasswordPath
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Error occurred during a cryptographic operation.

Password:
System.Security.SecureString: .Security.SecureString
---------------------------------------------------------------------
PS>TerminatingError(Unlock-SecretStore): "Cannot validate argument on parameter 'Password'. The argument is null. Provide a valid value for the argument, and then try running the command again."  
Unlock-SecretStore: C:\sync\sync.ps1:17
Line |
  17 |  Unlock-SecretStore -Password $password
     |                               ~~~~~~~~~
     | Cannot validate argument on parameter 'Password'. The argument is null. Provide a valid value for the argument, and then try running the command again.  
Unlock-SecretStore: C:\sync\sync.ps1:17
Line |
  17 |  Unlock-SecretStore -Password $password
     |                               ~~~~~~~~~
     | Cannot validate argument on parameter 'Password'. The argument is null. Provide a valid value for the argument,  
     | and then try running the command again.

**********************
PowerShell transcript end

Man sieht, das die Variable $password leer ist und Import-CliXml anscheinend nicht richtig funktioniert hat.

Melde ich mich nun wieder als User "Administrator" am System an und führe die Aufgabe aus, funktioniert sie immer noch nicht.

Führe ich das Script aber einmal manuell in der Konsole aus, funktioniert es und ab da funktioniert es auch wieder in der Aufgabenplanung. So lange, bis ich den User wieder abmelde.

Hat irgendjemand eine Idee, warum das so ist und evtl. eine Lösung zu dem Problem?

Danke im Voraus!

ipzipzap

Content-Key: 93569705549

Url: https://administrator.de/contentid/93569705549

Printed on: June 26, 2024 at 18:06 o'clock

Mitglied: 13034433319
Solution 13034433319 Jun 18, 2024 updated at 10:40:12 (UTC)
Goto Top
Das ist dein Problem:
[x] Kennwort nicht speichern
Der kryptographische Masterkey mit der das Passwort für den Store in der XML verschlüsselt wurde liegt im Benutzerprofil des Users und auf diesen kann der Task nur zugreifen wenn er das Environment des Users laden kann, ansonsten hat er keinen Zugriff auf den Master Key des User-Profils und kann folglich den Securestring aus der XML nicht entschlüsseln, da dieser dafür beim Anlegen der XML benutzt wird .

Deswegen kommt auch der Fehler
"Error occurred during a cryptographic operation"

Das es temp. funktioniert wenn du dich einmal mit dem Konto anmeldest und das Skript ausführst liegt daran das der Store dann schon für eine Zeit lang geöffnet wurde bis eben der Timeout wieder greift.

Gruß
Member: ipzipzap
ipzipzap Jun 18, 2024 at 11:23:29 (UTC)
Goto Top
Arghh... Wie war das mit dem Wald und den Bäumen? face-big-smile

Das war's natürlich. Kleiner Haken, große Wirkung!


1000 Dank!