0
votes

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.

1
FYI, I have set execution policy for both host and remote computer to unrestricted.Sheiky
All i need to do is get this script working from Jenkins.Sheiky
Do you have WinRM enabled on the remote computer?cory-fowler
Yes its enabled, as i mentioned i am able to run the script from powershell window.Sheiky

1 Answers

1
votes

You should set the execution policy to unrestricted to test.