无法通过Jenkins执行Powershell脚本

时间:2015-09-17 02:52:27

标签: powershell jenkins automation

我编写了这个powershell脚本来冒充另一个具有数据库访问权限的用户,执行选择查询,将结果导出为CSV并清除指定文件夹中的历史结果。手动运行时效果很好。但我希望将其设置为Jenkins工作,以便我可以授予相关团队临时运行权限。但由于Jenkins禁止PS进程的弹出控制台窗口,因此作业失败。它在系统事件日志中抛出以下错误:

  

应用程序弹出窗口:powershell.exe - 应用程序错误:应用程序无法正确启动(0xc0000142)。单击“确定”关闭应用程序。

以下是powershell脚本:

$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失败并输出以下内容:

  

由计时器[工作区]启动$ powershell.exe“&amp;   'C:\用户\ dummyuser \应用程序数据\本地\ TEMP \ hudson3554646184930.ps1'”   Receive-Job:[localhost]后台进程报告错误   随着消息:。在C:\ temp \ launcher.ps1:51 char:12   + Receive-Job&lt;&lt;&lt;&lt; $执行       + CategoryInfo:OpenError:(:) [Receive-Job],PSRemotingTranspor tException       + FullyQualifiedErrorId:PSSessionStateBroken完成:SUCCESS

对此的任何帮助都会很棒。非常感谢你的时间。

0 个答案:

没有答案