簡體   English   中英

Powershell Set-ADUser 不在計划任務中運行

[英]Powershell Set-ADUser doesn't run in scheduled task

我終於完成了我正在編寫的腳本,其中包括獲取 AD 用戶列表並導出到 CSV,將特定信息塊通過電子郵件發送給多人,然后修改用戶帳戶上的注釋。 當我在 ISE 中運行該腳本時,該腳本運行良好,完全沒有問題。

當我嘗試將它作為計划任務運行時,除 set-aduser 命令外,一切正常。 生成了 CSV 導出,在我的日志記錄中捕獲了任何其他錯誤,發送了電子郵件,但 set-aduser 似乎沒有運行。 我一直無法捕獲任何錯誤或輸出,只是當我查看 AD 帳戶時,注釋沒有更新。 同樣,當我在 ISE 中運行它時,它運行良好。 我逐步完成了代碼的每一部分,它點擊了 set-aduser 並在 ISE 中設置了注釋。 我認為這不是我的腳本的問題,但我在下面復制了一個通用塊。 我的 catch 塊永遠不會捕獲任何東西,即使我添加了 -erroraction stop。

$errorlog = 'c:\Error.txt'
$file = Import-csv 'c:\Test.csv'
$errordate = get-date
foreach ($name in $file)
    {
    $newnotes = "$($name.notes) `r`n NewNotesHere" 
    $NotesUserName = $name.sAMAccountName
        try
        {
        ##Set the notes for the user
        set-aduser -Identity $NotesUserName -Replace @{info="$newnotes"} 
        } 
        catch
        {
        ##Non terminating catch, used only for error logging.
        $ErrorName = $Error[0].exception.GetType().fullname 
        $ErrorDescription = $Error[0].exception.Message 
        $ErrorLineNumber = $Error[0].InvocationInfo.ScriptLineNumber
        $ErrorOffsetLineNumber = $Error[0].InvocationInfo.OffsetInLine
        Write-host "Something went wrong setting the notes for $NotesUserName... `r`n $ErrorName `r`n $ErrorDescription  `r`n $ErrorLineNumber -- $ErrorOffsetLineNumber `r`n`r`n" -foregroundcolor Yellow
        "$errordate `r`n Something went wrong setting the notes for: `r`n $($name.firstname) $($name.lastname) -- Username: $($name.username)... `r`n $ErrorName `r`n $ErrorDescription `r`n `r`n" | Out-file -filepath $ErrorLog -append
        }
    }

計划任務在 Windows Server 2012 R2 機器上運行,具有域管理員帳戶(我用來在 ISE 中成功測試它的帳戶)並且格式如下:

在此處輸入圖片說明 在此處輸入圖片說明

在 ISE 中運行它和作為計划任務運行它之間的唯一區別是它是一個計划任務。

我認為我最大的問題是:對計划任務是否有某種控制可以阻止 PowerShell 修改 Active Directory?

第二個問題:是否存在阻塞,是否有人對如何糾正這種行為提出建議?

在你的情況下,我可以看到你沒有提到執行政策 這可能是問題之一。 嘗試繞過如下所述。

嘗試直接從 cmd 提示符運行腳本並查看結果。 這些是選項。 剛剛打了powershell /?

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { - | <script-block> [-args <arg-array>]
                  | <string> [<CommandParameters>] } ] 

我面臨的幾個主要觀察結果:

1)不要只提供 powershell.exe ,而是嘗試提供完整的 PS 路徑 C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe

2) 許可是另一個問題。 您通過其運​​行任務的用戶可能沒有運行該任務的權限。

3) 執行策略——確保你使用 -executionpolicy bypass 繞過了執行策略

4) 確保您以最高權限運行任務

5)最后,通過日志分析

希望能幫助到你。

事實證明,這根本不是 PowerShell 問題,我們遇到了域控制器問題。 所以它正在更新,但沒有復制任何內容,因此在提取用戶帳戶時,它看起來沒有更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM