无法通过Jenkins执行Powershell脚本

无法通过Jenkins执行Powershell脚本,powershell,jenkins,automation,Powershell,Jenkins,Automation,我编写此powershell脚本是为了模拟另一个具有DB访问权限的用户,对其执行select查询,将结果导出到CSV,并从指定文件夹中清除历史结果。手动运行时效果非常好。但我想把它设置为Jenkins的工作,这样我就可以授予相关团队运行临时任务的权限。但由于Jenkins抑制了PS进程的弹出控制台窗口,所以作业失败。它在系统事件日志中抛出以下错误: 应用程序弹出窗口:powershell.exe-应用程序错误:应用程序无法正确启动(0xc0000142)。单击“确定”关闭应用程序 下面是powe

我编写此powershell脚本是为了模拟另一个具有DB访问权限的用户,对其执行select查询,将结果导出到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
$uid='dummy\user'
$pass='password'|转换为SecureString-Force-AsPlainText
$creds=新对象System.Management.Automation.PSCredential($uid,$pass)
$job=开始作业-脚本块{
$query=”
使用[ReportServer]
挑选
在南,
路径
领域,
希德,
[参数],
注1:,
开始
完,,
计数
[行计数],
附加信息
从日志
其中startdateadd(分钟,-180,GetDate())
“按开始顺序订购”
函数purger(){
$limit=(获取日期).AddDays(-1)
获取ChildItem-Path“C:\temp\NAS\”-Recurse-Force |其中对象{(!$.psicContainer-和$\.LastWriteTime-lt$limit)-和$\.Name-Match.csv'}删除项-Force
}
$database=“dummyDB”
$SQLInstance=“机器\数据库”
$connectionString=“服务器=$SQLInstance;可信连接=true;”
$connection=新对象System.Data.SqlClient.SqlConnection
$connection.ConnectionString=$ConnectionString
$connection.Open()
$command=$connection.CreateCommand()
$command.CommandText=$query
$result=$command.ExecuteReader()
$table=新对象“System.Data.DataTable”
呼噜声
$table.Load($result)
$table |导出Csv-路径“C:\temp\NAS-$(获取日期-格式yyyy MM dd hh MM).Csv”
写主机“完成”
$connection.Close()
}-凭证$creds
等待作业$Job
接收Job$Job
Jenkins失败,输出如下:

由计时器[工作区]$powershell.exe启动”& “C:\Users\dummyuser\AppData\Local\Temp\hudson355464184930.ps1” 接收作业:[localhost]后台进程报告错误 带有以下消息:。在C:\temp\launcher.ps1:51字符:12
+接收作业如何在Jenkins中设置构建步骤?我刚刚尝试了你的脚本-当作为Windows批处理命令(“powershell-File script.ps1”)或通过Jenkins powershell plugin.Hi@AlexanderDrogin执行时,对我来说效果很好-这很奇怪,因为我尝试了你提到的两种方法,每次都会出现“PSSessionStateBruke”和“Application popup:”错误。我已安装Powershell 2.0。你认为防火墙会在这里发挥作用吗?如果它对你有效,那么我一点也不知道我的设置有什么问题。我希望这是个剧本问题(我尝试了Windows批处理命令和PS插件,但没有成功。有什么建议吗?如何在Jenkins中设置构建步骤?我刚刚尝试了您的脚本-当作为Windows批处理命令(“powershell-File script.ps1”)执行时,对我来说效果很好)或者通过Jenkins Powershell plugin.Hi@AlexanderDrogin-这很奇怪,因为我尝试了你提到的两种方法,得到了“PSSessionStateBruke”和“应用程序弹出窗口”:每次都出现错误。我安装了Powershell 2.0。你认为这里可能有防火墙吗?如果它对你有效,那么我不知道我的设置有什么问题。我希望这是脚本问题。:(我尝试了Windows批处理命令和PS插件,但没有成功。有什么建议吗?