使用Jenkins的PowerShell脚本重置远程IIS

时间:2015-08-06 06:10:18

标签: windows powershell iis ant jenkins

我有一个PowerShell脚本连接到远程服务器并重置IIS,如果我从PowerShell窗口执行它,这个脚本工作正常,我的问题是让它与Jenkins一起工作,我尝试了几个选项,如下所示。

在我的构建脚本中使用Exec Ant任务:

<target name ="reset.IIS" description="Resets the IIs of specified URL">
    <exec executable="powershell" failonerror="true" outputproperty="myscript.out" errorproperty="myscript.error" output="iisout.log">
        <arg line="-ExecutionPolicy RemoteSigned" />
        <arg line="-File ${script.path}" />
    </exec>
<if>
    <not>
        <equals arg1="${myscript.error}" arg2="" trim="true" />
    </not>
    <then>
        <fail message="${myscript.error}" />
    </then>
</if>
</target>

这是我的PowerShell脚本:

Import-Module WebAdministration
Set-ExecutionPolicy Remotesigned
$User = "MYComputer\User"
$File = "E:\Temp\Password.txt"
$targetServer  = "AA140294"
$MyCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)

Invoke-Command -ComputerName $targetServer -ScriptBlock{ D:\Trunk\build\sprint17qcReset.ps1 } -Credential $MyCredential

当我从詹金斯那里跑来时,没有任何事情发生;它只会在控制台窗口中显示执行脚本。

我也尝试将其从构建脚本中移出并将其放入Jenkins&#34; Windows powershell&#34;中的构建步骤中,但是我收到以下错误:

[JenkinsCI] $ powershell.exe "&       'C:\Windows\TEMP\hudson7326095447547431161.ps1'"
File C:\Windows\TEMP\hudson7326095447547431161.ps1 cannot be loaded because the
execution of scripts is disabled on this system. Please see "get-help
about_signing" for more details.

这是远程计算机中的ps1脚本:

Import-Module WebAdministration
$name = "build.test.com"
$path = "IIS:\Sites\"
$fullpath = $path + $name
Stop-Webitem $fullpath
Start-Webitem $fullpath
Get-Website -Name $name
Exit-PSSession

我从PowerShell窗口检查了执行策略。它不受限制。

感谢任何帮助,提前致谢。

1 个答案:

答案 0 :(得分:1)

您应该将执行策略设置为unrestricted to test。