简体   繁体   中英

Reset Remote IIS with PowerShell script from Jenkins

I have a PowerShell script which connects to remote server and resets the IIS, this script works fine if i execute it from PowerShell window, my problem is getting it work with Jenkins, I have tried few options as below.

Using Exec Ant task in my build script:

<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>

This is my PowerShell script:

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

When I run this from Jenkins nothing happens; it will just show executing script in console window.

I also tried moving it out from build script and putting it inside a build step in Jenkins "Windows powershell", but I get the following error:

[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.

This is my ps1 script in remote computer:

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

I checked Execution policy from PowerShell window. It's Unrestricted.

Any help is appreciated, Thanks in advance.

您应该将执行策略设置为不受限制地进行测试。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM