1

Here's the problem:

When I run my PowerShell script manually, everything works fine. But it doesn't work via task scheduling.

The reason is probably my stored credentials for Active Directory and Mobile Device Management.I stored them with "Export-Clixml" but the xml files cant be read when i open the script with task scheduler. Without it works perfect.

The task scheduler is executed with the same user who saved the credentials in the XML before.

I hope you understand what I mean.

credential XML file


Edit 1:

I've tried the Solution from "Bender the Greatest". Unfortunately it still doesn't work. I exported my credentials with "Export-Clixml" and import them with "Import-Clixml".

It doesn't matter if I generate them manually via my admin account or via the system account (with psexec). If I then execute it via the corresponding account with which the credentials were generated, it still doesn't work.

But it seems to find the credentials (I saw that in the log). But The import doesn't seem to work.

If I manually include the credentials in the script it works fine, but I would have liked to store them encrypted.

[Lizenzauswertung] Überprüfe Zugangsdaten
[Lizenzauswertung] MDM Zugangsdaten gefunden - Credentials found
[Lizenzauswertung] MDM AccessToken gefunden - Credentials found
[Lizenzauswertung] AD Zugangsdaten gefunden - Credentials found

Here im trying to decrypt the Password from the XML File:

It is not possible to call a method for an expression that has NULL.(Translated)
In C:\Scripts\ADAuswertung\AD_Auswertung_GKZ.ps1:171 Zeichen:1
+ $PSCPW  = $CredsMDM.GetNetworkCredential().Password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
It is not possible to call a method for an expression that has NULL.(Translated)
In C:\Scripts\ADAuswertung\AD_Auswertung_GKZ.ps1:171 Zeichen:1
+ $PSCPW  = $CredsMDM.GetNetworkCredential().Password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

This is a replacement for the username to have the right spelling for a webrequest:

It is not possible to call a method for an expression that has NULL.(Translated)
In C:\Scripts\ADAuswertung\AD_Auswertung_GKZ.ps1:172 Zeichen:1
+ $RPSCUser = $PSCUser.Replace("INTRA","intra.lan")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

It is not possible to call a method for an expression that has NULL.(Translated)
In C:\Scripts\ADAuswertung\AD_Auswertung_GKZ.ps1:172 Zeichen:1
+ $RPSCUser = $PSCUser.Replace("INTRA","intra.lan")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Here you can see the errors that occur when importing the MDM credentials. The reason for the errors is described above. I edit the credentials after importing them before they are passed to the script.

The MDM Access Token is not encrypted and can be read.

The AD credentials are not processed beforehand, but passed directly to the AD commands. Therefore, no error is displayed here. Since it does not finish the AD report, which I saw in the log, these are not imported either.

Here is a little Codesnippet of the Import/Export. Just got the code here for the MDM credentials. The other queries are the same.

If (Test-Path $CredentialsMDM){
        $CredsMDM = Import-Clixml -Path $CredentialsMDM
        Write-Host "[Lizenzauswertung] MDM Zugangsdaten gefunden" -ForegroundColor Green
    } else {
        Write-Host "[Lizenzauswertung] MDM Zugangsdaten nicht gefunden" -ForegroundColor Yellow
        Get-Credential -Message "Zugangsdaten für MDM / Airwatch" | export-clixml -path $CredentialsMDM
        $CredsMDM = Import-Clixml -Path $CredentialsMDM
}

Write-Host "[Lizenzauswertung] Zugangsdaten überprüft" -ForegroundColor Green

$PSCUser = $CredsMDM.UserName
$PSCPW  = $CredsMDM.GetNetworkCredential().Password
$RPSCUser = $PSCUser.Replace("INTRA","intra.lan")

$AccessToken = $CredsMDMAT.accesstoken
$Auth = $CredsAD

(I am sorry for the spaghetti code.)

2 Answers2

0

You need to provide the error you're getting but I'm going to take a shot in the dark here. I'm guessing you're either exporting secretAD.xml as one user and running your script as another user from Task Scheduler, or exporting secretAD.xml on another computer/server. By default, credentials are able to be decrypted only by the user that built it on that specific server. This is how Windows' Data Protection API (DPAPI) works.

The easy way to fix this is to generate secretAD.xml as the user you intend to run the script as. You can specify your own key for decryption, which can be used by any user on any box, but you will need to figure out a method of secure delivery of the decryption key to the runtime. If you want/need to run the script as NT Authority\SYSTEM, I recommend using psexec to open an interactive SYSTEM Powershell session, generate the credential, and then use Export-CliXml to serialize the credential to secretAD.xml.


Note that the default DPAPI key gets changed when an account password is rotated, so you'll need to remember to rebuild the credential when that happens.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • Exactly you understood my problem. However, I created the XML file on the server with the user that I use to perform the task in task scheduling and it still doesn't work. - No matter I try it as NT Authority\SYSTEM and create the XML file with the psexec. When I'm back at work. If this also doesn't work I will contact you again. Otherwise I will mark this as a solution. - Unfortunately this is a bit difficult with the error I get. For some reason the log is not created. When I execute the task. So I think it is due to the credentials. I can't say that exactly. – Michael Clemens Dec 03 '19 at 17:20
  • If this doesn't work for you, update the question with how you are generating secretAD.xml – codewario Dec 03 '19 at 18:26
  • Added Logs, Code Snippets and more Information. Hopefully we'll find a solution. – Michael Clemens Dec 05 '19 at 07:56
0

I've found the solution to my problem.

The reason was that the path to the credentials was ".\secretMDM.xml". The task scheduler executes the script at a different location. Therefore I had to specify the whole path of the file.

Thanks for the help anyway.

Edit: Out of interest I created the "Credential" - XML files with the Task Scheduler. Have a look where it stores the files ;)

image description

Community
  • 1
  • 1