powershell - 用于运行 2 个 exe 的 Powsershell 脚本 - 具有管理员权限和当前登录用户
问题描述
我想为以下内容编写一个 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"
推荐阅读
- javascript - 从日期获取日期名称,格式为 dd-mm-yyyy?
- pandas - 避免使用 pandas 到 sqlalchemy 转换的重复项
- reactjs - 使用 Typescript 的 Redux reducer 初始状态
- java - signjar 的 Ant 并行任务
- sas - SAS Hash Tables:有没有办法在不同的键上查找/加入或有可选的键
- java - getAdapterPosition 有时会返回错误的值
- arrays - 如何使用 Laravel 在数据库中保存多个表行
- c# - 如何获得空组,懒惰
- python - 输入浮点数列表并通过迭代行计算每列的平均值 - python
- linux - 如何使用 .txt 中的日期列表作为 shell 脚本的输入?