0

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.

4
  • FYI, I have set execution policy for both host and remote computer to unrestricted. Commented Aug 6, 2015 at 6:11
  • All i need to do is get this script working from Jenkins. Commented Aug 6, 2015 at 6:27
  • Do you have WinRM enabled on the remote computer? Commented Aug 6, 2015 at 9:23
  • Yes its enabled, as i mentioned i am able to run the script from powershell window. Commented Aug 6, 2015 at 10:17

1 Answer 1

1

You should set the execution policy to unrestricted to test.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.