詹金斯的高级PS脚本

时间:2016-12-08 15:49:52

标签: powershell jenkins

我一直在尝试从Windows Jenkins(从属)服务器运行脚本。该脚本是用PowerShell编写的,需要提升权限(例如,如果在PS上右键单击并选择以管理员身份运行)。

Jenkins以下列方式启动其脚本:

powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\JOHAN.DER\AppData\Local\Temp\2\hudson9084956499652818911.ps1'" 

我的脚本失败,因为它需要提升权限。如何生成一个新的提升特权的PS进程(不需要点击,因为Jenkins不能这样做)可以运行我的脚本?

干杯!

3 个答案:

答案 0 :(得分:0)

试试这个:

powershell -Command "Start-Process powershell \"-ExecutionPolicy Bypass -NoExit -Command `\"cd \`\"%scriptFolderPath%`\"; & \`\".\%powershellScriptFileName%\`\"`\"\" -Verb RunAs"

答案 1 :(得分:0)

下面的代码片段会检查当前进程是否已升级,如果没有,则会生成一个新的特权进程。获取子PowerShell进程的输出有点棘手,因此我使用transcript命令来捕获它。您可以在下面找到我的管道定义步骤:

            powershell """
                cd "${env.WORKSPACE}"
                If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
                { 
                    echo "* Respawning PowerShell child process with elevated privileges"
                    \$pinfo = New-Object System.Diagnostics.ProcessStartInfo
                    \$pinfo.FileName = "powershell"
                    \$pinfo.Arguments = "& '" + \$myinvocation.mycommand.definition + "'"
                    \$pinfo.Verb = "RunAs"
                    \$pinfo.RedirectStandardError = \$false
                    \$pinfo.RedirectStandardOutput = \$false
                    \$pinfo.UseShellExecute = \$true
                    \$p = New-Object System.Diagnostics.Process
                    \$p.StartInfo = \$pinfo
                    \$p.Start() | Out-Null
                    \$p.WaitForExit()
                    echo "* Child process finished"
                    type "C:/jenkins/transcript-${env.JOB_NAME}-${env.BUILD_NUMBER}.txt"
                    Remove-Item "C:/jenkins/transcript-${env.JOB_NAME}-${env.BUILD_NUMBER}.txt"
                    Exit \$p.ExitCode
                } Else {
                    echo "Child process starting with admin privileges"
                    Start-Transcript -Path "C:/jenkins/transcript-${env.JOB_NAME}-${env.BUILD_NUMBER}.txt"
                }

                # put rest of your script here, it will get executed
                # with elevated privileges.
            """

答案 2 :(得分:0)

即使这是一个旧线程,由于我有同样的问题,我仍然在这里提供我的方法,希望能帮助任何找到答案的人。

首先,此问题与Jenkins无关,这是Windows的问题,您必须启用内置管理员以获得提升的权限,这是reference

管理员用户
这是一个未提升的管理员帐户 Windows安装期间默认创建的文件。如果 管理员用户尝试执行需要提升权限的操作 (例如:以管理员身份运行),Windows会为您显示UAC提示 管理员用户才能批准操作。

内置“管理员”
隐藏的内置高架 “管理员帐户” 是具有完整帐户的本地帐户 对PC的无限制访问权限。默认情况下,此“管理员” UAC不会提示该帐户。

启用内置管理员后,您有两种方法来提升由Jenkins触发的PS脚本:

1。使用内置管理员登录Windows:
这是实现目标的最简单方法,只需使用内置Administrator进行登录,一切都会提高,包括由Jenkins触发的PS脚本。 (我正在使用这种方法。)

2。通过凭据并以管理员身份运行:
在PS脚本中添加一些代码

$user = "Administrator"
$passwd = "password"
$securePasswd = ConvertTo-SecureString $passwd -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $user, $securePasswd

#Use Credential to prevent from being prompted for password
#Use argument -Verb RunAs to get script elevated
Start-Process powershell.exe -Credential $credential -ArgumentList "Start-Process powershell.exe -Verb RunAs -ArgumentList '-File  hudson.ps1' -Wait" 
相关问题