簡體   English   中英

具有存儲憑據的Start-Process PowerShell無法正常工作

[英]Start-Process PowerShell with stored credentials is not working

我有一個必須作為提升用戶運行的腳本。 我正在使用問題“ 以管理員身份運行PowerShell腳本而不輸入密碼 ”中的代碼,但我的第二個腳本未被調用。 第一個腳本由系統進程觸發(我的票務系統收到一封電子郵件,然后它以主題行作為參數調用我的提升腳本),並且只是使用計划任務不是一個選項。

調用腳本:

param(
    [Parameter(Mandatory=$true)]
    [String]$MyParam
)

$LogFile = "c:\temp\log.txt"

$encpwd = Get-Content c:\temp\password.bin
$passwd = ConvertTo-SecureString $encpwd
$cred = new-object System.Management.Automation.PSCredential 'domain\LocalAdminAccount',$passwd
Add-Content $LogFile "I am running as $env:userdomain\$env:username"
Add-Content $LogFile "Trying to call the script with the parameter: $MyParam"
try {
    Add-Content $LogFile "Calling script"
    Start-Process PowerShell -WorkingDirectory 'C:\Windows\System32' -Cred $cred -ArgumentList '-File', "c:\temp\TargetScript.ps1 $MyParam"
    Add-Content $LogFile "Script called"
} catch {
    $msg = $Error[0].Exception.Message
    Add-Content $LogFile "Error caught: $msg"
}
Add-Content $LogFile "Error caught: $msg"

被調用的腳本:

param(
    [Parameter(Mandatory=$true)]
    [String]$PassedParam
)
$LogFile = "c:\temp\log.txt"

Add-Content $LogFile "I am running as $env:userdomain\$env:username"
if ($PassedParam) {
    try {
        #stuff
        if ($?) {
            Add-Content $LogFile "$PassedParam worked"
        } else {
            Add-Content $LogFile "Failed"
        }
    } catch {
        $msg = $Error[0].Exception.Message
        Add-Content $LogFile "Error caught: $msg"
    }
}
Add-Content $LogFile "Error caught: $msg"

這是放在日志文件中的內容:

I am running as DOMAIN\COMPUTER$
Trying to call the script with the parameter: Tim
Calling script

它似乎永遠不會真正啟動第二個PowerShell進程,或者至少如果它實際上,第二個PowerShell進程沒有寫入日志文件。 我特別授予LocalAdminAccount對日志文件和password.bin文件的完全權限,LocalAdminAccount在計算機上的administrators組中。

如果重要,我的powershell版本是:

PS C:\> $PSVersionTable.PSVersion
Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1

更新:如果我以自己的身份登錄計算機並運行腳本,則以下是日志文件顯示的內容:

I am running as DOMAIN\TIM
Trying to call the script with the parameter: Tim
Calling script
Script called
I am running as DOMAIN\LocalAdminAccount
Tim worked
Error caught: 

更新:我確實找到了這篇文章: https//www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-2/ ,這表明我需要提供密碼的解密密鑰,因為我在我的帳戶下創建了密碼文件,但NT SYSTEM正在對其進行解密。 但這並沒有解決我的問題。

我只是試着打電話給記事本做了更多的測試。 如果我嘗試以不同的用戶身份打開它,它會失敗,但如果我只是嘗試打開它,我可以在SYSTEM用戶名下運行的任務管理器中看到它。

我的問題似乎是SYSTEM(DOMAIN \\ COMPUTER $)沒有能力以不同的用戶身份運行進程?

從交互式PowerShell窗口以系統身份運行主腳本命令(請參閱:將PowerShell作為系統運行)

這將揭示錯誤:

由於錯誤,無法運行此命令:拒絕訪問。

此問題已在以下位置進行了描述: Powershell Start-Process:由於錯誤,此命令無法執行:訪問被拒絕並由本地策略定義:

Security Settings
    Local Policies
        User Rights Assignment
            Impersonate a client after authentication

要在另一個進程中運行腳本,請記住PowerShell始終在啟動它的用戶的上下文中運行,因此您需要將腳本設置為自動提升/最小化使用RunAs動詞。

Start-Process PowerShell -verb RunAs -Cred $cred -ArgumentList '-noexit','-File','path-to-script' 

或者在記事本或其他腳本用例中說...

Start-Process powershell -Credential mydomain\mydomainAdmin -ArgumentList '-noprofile -command &{Start-Process notepad -verb runas}'

...作為你正在使用的指針顯示。 您沒有在發布的代碼中的任何位置使用它。 您必須單獨啟動另一個進程以作為另一個用戶運行。

作為一種解決方法,我創建了一個作為我的serviceadmin運行的計划任務。 現在,當票務系統觸發調用計划任務的腳本時,計划任務運行需要提升的第二個腳本。 這並不理想,但確實可以正常工作。

計划任務(命名為步驟2)

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2019-08-01T09:12:24.7058452</Date>
    <Author>Tim</Author>
    <Description></Description>
  </RegistrationInfo>
  <Triggers />
  <Principals>
    <Principal id="Author">
      <UserId>Domain\ServiceAccount</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    all defaults. Removed for brevity.
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe</Command>
      <Arguments>-file "C:\Temp\Step2.ps1"</Arguments>
    </Exec>
  </Actions>
</Task>

Step1.ps1(調用腳本)

param(
    [Parameter(Mandatory=$true)]
    [String]$MyParam
)

$LogFile = "c:\Temp\Actions.log"
$ParamFile = "c:\Temp\Params.txt"
Remove-Item $ParamFile
$MyParam | Out-File -FilePath $ParamFile
Add-Content $LogFile "$MyParam written to file."
Add-Content $LogFile "Calling Step 2 task."
Start-ScheduledTask -TaskName "Step 2"

Step2.ps1(被調用的腳本)

$LogFile = "c:\temp\log.txt"
$ParamFile = "c:\Temp\Params.txt"
$PassedParam = Get-Content $ParamFile
Add-Content $LogFile "I am running as $env:userdomain\$env:username"
if ($PassedParam) {
    try {
        #stuff
        if ($?) {
            Add-Content $LogFile "$PassedParam worked"
        } else {
            Add-Content $LogFile "Failed"
        }
    } catch {
        $msg = $Error[0].Exception.Message
        Add-Content $LogFile "Error caught: $msg"
    }
}
Add-Content $LogFile "Step 2 complete"

打算將此標記為答案,因為它解決了我使用解決方法的問題,但它沒有回答我的問題。

我的建議是授予計算機帳戶訪問相關目錄對象的權限(最明顯的方法是將計算機的帳戶添加到目錄中具有必要權限的組),並使用SYSTEM帳戶在該計算機上運行腳本。 在我看來,這對於您的場景來說是最簡單,最簡單的解決方案。

暫無
暫無

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

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