簡體   English   中英

用於運行 2 exe 的 Powsershell 腳本 - 具有管理員權限和當前登錄用戶

[英]Powsershell script to run 2 exe - with Administrator privilege and Current logged user

我想為以下內容編寫一個 Powershell 腳本:

  • 1 以管理員權限運行exe安裝軟件
  • 2 使用當前登錄的用戶權限運行另一個 exe,沒有憑據提示。

這是我的 Powershell 腳本:

# This Powershell script is used to deploy an application on user desktop who don't have Administrative rights
# This script is run as Administrator


# Get current user logged
$user = Get-WmiObject -Class win32_computersystem | % Username
write-host "User: " $user


#Start the process with the Administrative privilege
(Start-Process -FilePath "setup.exe").WaitForExit()

#Start the process agent as current user logged
Start-Process -FilePath "agent.exe" -Credential $User

感謝您的幫助。

我通過這樣做解決了我的問題:

    #Start the setup with the Administrative privilege
    Write-Host "Start setup"
    Start-Process -FilePath $LocalSetupFilePath -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART -Encryption=Auto" -NoNewWindow -Wait
    Write-Host "Installation completed"

    #Create scheduled task for start agent
    $agentTaskName = "RunAgent"
    $taskPath = "\"
    $OSArch = (Get-WmiObject -class Win32_OperatingSystem).OSArchitecture

    Write-Host "OSArchitecture= $OSArch"
    If ($OSArch -like '64*') {
        $ProgFilePath=$env:ProgramW6432
    } else
    { 
        $ProgFilePath=$env:ProgramFiles
    }
    $agentFilePath = [IO.Path]::Combine($ProgFilePath, "agent.exe")
    Write-Host "Run $agentFilePath"

    $time = (Get-Date).AddHours(1).ToShortTimestring();

    # Create & Run scheduled task to start agent
    schtasks /create /s $ComputerName /tn $agentTaskName /sc once /tr "cmd /c start '' '$agentFilePath'" /st $time /ru $UserName
    schtasks /run /tn $agentTaskName

    Start-Sleep -Seconds 5

    Write-Host "Stop & delete $agentTaskName"
    #Stop scheduled task
    schtasks /end /tn $agentTaskName
    #Remove scheduled task
    schtasks /delete /tn $agentTaskName /f

    Write-Host "Success"

暫無
暫無

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

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