Unable to execute Powershell script via Jenkins

1

I wrote this powershell script to impersonate another user with DB access, execute a select query against, export the results into a CSV and also purges historic results from a specified folder. It works great when run manually. But I want to set it up as a Jenkins job so I can grant relevant teams with access to run ad hoc. But Since Jenkins suppresses popup console window for PS process, the job fails. It throws below error in system event log:

Application popup: powershell.exe - Application Error : The application was unable to start correctly (0xc0000142). Click OK to close the application.

Below is the powershell script:

$uid='dummy\user'
$pass='password' | ConvertTo-SecureString -Force -AsPlainText
$creds=New-object System.Management.Automation.PSCredential ($uid, $pass)
$job = Start-Job -ScriptBlock {
$query = "
USE [ReportServer]
 SELECT
 iname,
 path,
 domain,
 sid,
 [Parameters],
 note1,
 start,
 end,
 count,
 [RowCount],
 AdditionalInfo
 FROM log
 where start <  GetDate() and end  > dateadd(minute, -180, GetDate())
 ORDER BY start"
function purger (){
$limit = (Get-Date).AddDays(-1)
Get-ChildItem -Path "C:\temp\NAS\" -Recurse -Force | Where-Object { (!$_.PSIsContainer -and $_.LastWriteTime -lt $limit ) -and $_.Name -Match '.csv' } | Remove-Item -Force
}
$database = "dummyDB"
$SQLInstance = "machine\database"
$connectionString ="server=$SQLInstance;trusted_connection=true;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open() 
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
purger
$table.Load($result)
$table | Export-Csv -path "C:\temp\NAS-$(get-date -format yyyy-MM-dd-hh-mm).csv"
Write-Host "done"
$connection.Close()
} -Credential $creds
Wait-Job $job
Receive-Job $job

Jenkins fails with the following output:

Started by timer [workspace] $ powershell.exe "& 'C:\Users\dummyuser\AppData\Local\Temp\hudson3554646184930.ps1'" Receive-Job : [localhost] The background process reported an error with the fol lowing message: . At C:\temp\launcher.ps1:51 char:12 + Receive-Job <<<< $execute + CategoryInfo : OpenError: (:) [Receive-Job], PSRemotingTranspor tException + FullyQualifiedErrorId : PSSessionStateBroken Finished: SUCCESS

Any help on this would be awesome. Thanks heaps in advance for your time.

powershell
jenkins
automation
asked on Stack Overflow Sep 17, 2015 by Karthik

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0