带有Clixml和Taskscheduler的PowerShell脚本

问题描述 投票:1回答:1

这里是问题:

当我手动运行PowerShell脚本时,一切正常。但是它不能通过任务计划来工作。

原因可能是我为Active Directory和移动设备管理存储的凭据。我使用“ Export-Clixml”存储了它们,但是当我使用任务计划程序打开脚本时无法读取xml文件。没有它,效果很好。

任务调度程序是与以前在XML中保存凭据的同一用户执行的。

希望您能理解我的意思。

credential XML file


编辑1:

我已经尝试过“最大的本德尔”的解决方案。不幸的是,它仍然无法正常工作。我使用“ Export-Clixml”导出了凭据,并使用“ Import-Clixml”导入了凭据。

无论是通过管理员帐户还是通过系统帐户(使用psexec)手动生成它们,都没有关系。然后,如果我通过生成凭据的相应帐户执行它,则它仍然无法正常工作。

但是似乎找到了凭据(我在日志中看到了)。但是导入似乎无效。

如果我在脚本中手动包含凭据,则可以正常工作,但是我希望将其加密存储。

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

这里我正试图从XML文件解密密码:

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

这是用户名的替代,以使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

在这里您可以看到导入MDM凭据时发生的错误。错误的原因如上所述。在将凭据导入到脚本之前,我会对其进行编辑。

MDM访问令牌未加密,可以读取。

AD凭据不会被预先处理,而是直接传递给AD命令。因此,这里没有显示错误。由于它没有完成我在日志中看到的AD报告,因此也不会导入这些报告。

这里是导入/导出的一些代码片段。刚刚在这里获得了MDM凭证的代码。其他查询相同。

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
powershell taskscheduler
1个回答
0
投票

您需要提供您所得到的错误,但我将在此处进行暗中拍摄。我猜想您是作为一个用户导出secretAD.xml并从Task Scheduler中以另一个用户身份运行脚本,还是在另一台计算机/服务器上导出secretAD.xml。默认情况下,凭据可以仅由在该特定服务器上构建凭据的用户解密。 This is how Windows' Data Protection API (DPAPI) works.

解决此问题的简单方法是,以您打算作为脚本运行用户的身份生成secretAD.xml。您可以指定自己的解密密钥,任何用户都可以在任何盒子上使用它,但是您将需要找出一种将解密密钥安全交付给运行时的方法。如果要/需要以NT Authority\SYSTEM身份运行脚本,建议使用psexec打开交互式SYSTEM Powershell会话,生成凭据,然后使用Export-CliXml将凭据序列化为secretAD.xml。 >


请注意,当旋转帐户密码时,默认的DPAPI密钥会更改,因此,您需要记住在发生这种情况时重建凭证。

© www.soinside.com 2019 - 2024. All rights reserved.