2017-02-15 54 views
0

我有以下脚本,将詹金斯作业失败,即使成功地执行PS脚本

停止WebService的

删除的WebService

复制从詹金斯工作区项目的服务器路径

创建的WebService

启动WebService

我在詹金斯的工作中运行这个PS脚本,执行后,我收到一封电子邮件,因为编译失败。

val STOPPED 
[SC] DeleteService SUCCESS 
val DELETED 
Remove-Item : Cannot remove item \\Location.dll: Access to the 
path '\\Location.dll' is denied. 
At C:\Users\Administrator\AppData\Local\Temp\hudson7587011077362516847.ps1:33 char:5 
+  Remove-Item "$val\*" -Force -Recurse 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : PermissionDenied: (\\10.0.1.190\d$...h\Grpc.Core.dll:FileInfo) [Remove-Item], Unauthoriz 
    edAccessException 
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand 

我的PS脚本

# force strict - so any variable used before being assigned causes an error 
Set-PsDebug -Strict 

# force PowerShell to exit with a non-zero code on teh first error 
$ErrorActionPreference = 'Stop' 

# set $LASTEXITCODE to zero, so the script doesn't fail if no exit code returned 
$LASTEXITCODE = 0 

# set directories here once, so we can reuse 
$MystiflySearchDir = "\\Location" 
$ReleaseDir = "C:\Location" 

# get directory contents (are you expecting these to return to Jenkins?) 
Get-ChildItem "$ReleaseDir\*" 

# create the search directory if it doesn't exist 
if (-not (Test-Path -Path $val -PathType Container)) { New-Item -Path $val-type directory -Force } 

# get the service, but fail gracefully if it doesn't exist 
$service = Get-Service -Name val -Computername $env:SERVER -ErrorAction SilentlyContinue 

# if we have a service, stop and delete it 
if($service.Status) 
{ 
    sc.exe \\$env:SERVER stop val 
    if ($LASTEXITCODE -ne 0) { throw "error stopping the service: $LASTEXITCODE" } 
    Write-Host "val STOPPED" 
    Start-Sleep -s 10 
    sc.exe \\$env:SERVER delete val 
    if ($LASTEXITCODE -ne 0) { throw "error deleting the service: $LASTEXITCODE" } 
    Write-Host "val DELETED" 
    Start-Sleep -s 25 
    Remove-Item "$val\*" -Force -Recurse 
} 

# copy release to search 
xcopy "$ReleaseDir\*" $val/k/e/d/Y 

# (re)create the service 
sc.exe \\$env:SERVER create val start=auto DisplayName= "value" binPath= D:\Location.exe 
if ($LASTEXITCODE -ne 0) { throw "error creating the service: $LASTEXITCODE" } 
sc.exe \\$env:SERVER description val "val" 
if ($LASTEXITCODE -ne 0) { throw "error adding description to service: $LASTEXITCODE" } 
sc.exe \\$env:SERVER start val 
if ($LASTEXITCODE -ne 0) { throw "error starting the service: $LASTEXITCODE" } 
Write-Host "val STARTED" 

有什么我很想念这里

+0

好吧,如果你完全确定它不是'退出1',其他一些步骤是失败的构建作业 – 4c74356b41

回答

1

您的代码需要稍微完善一些,因为你有一对夫妇重复的部分,是否已执行服务是否存在。还翻转了if逻辑。

在代码的开始处添加了一些步骤,以使PowerShell更加严格,并使用非零代码删除任何错误。

这没有经过测试,所以可能需要稍微微调。但是,它应该是95%,希望!

# force strict - so any variable used before being assigned causes an error 
Set-PsDebug -Strict 
# force PowerShell to exit with a non-zero code on the first error 
$ErrorActionPreference = 'Stop' 

# set $LASTEXITCODE to zero, so the script doesn't fail if no exit code returned 
$LASTEXITCODE = 0 

# se directories her once, so we can reuse 
$AirSearchDir = "\\Location" 
$ReleaseDir = "C:\Location" 

# get directory contents (are you expecting these to return to Jenkins?) 
Get-ChildItem "$ReleaseDir\*" 

# create the search directory if it doesn't exist 
if (-not (Test-Path -Path $AirSearchDir -PathType Container)) { New-Item -Path $AirSearchDir -type directory -Force } 

# get the service, but fail gracefully if it doesn't exist 
$service = Get-Service -Name val -Computername $env:SERVER -ErrorAction SilentlyContinue 
# if we have a service, stop and delete it 
if($service.Status) 
{ 
    sc.exe \\$env:SERVER stop val 
    if ($LASTEXITCODE -ne 0) { throw "error stopping the service: $LASTEXITCODE" } 
    Write-Host "val STOPPED" 
    sc.exe \\$env:SERVER delete val 
    if ($LASTEXITCODE -ne 0) { throw "error deleting the service: $LASTEXITCODE" } 
    Write-Host "val DELETED" 
} 

# copy release to search 
Copy-Item "$ReleaseDir\*" $AirSearchDir -Force -Recurse 

# (re)create the service 
sc.exe \\$env:SERVER create val start=auto DisplayName="val" binPath= D:\Location.exe 
if ($LASTEXITCODE -ne 0) { throw "error creating the service: $LASTEXITCODE" } 
sc.exe \\$env:SERVER description val "val" 
if ($LASTEXITCODE -ne 0) { throw "error adding description to service: $LASTEXITCODE" } 
sc.exe \\$env:SERVER start val 
if ($LASTEXITCODE -ne 0) { throw "error starting the service: $LASTEXITCODE" } 
Write-Host "val STARTED" 
+0

我看到下面的错误。 无法检索变量'$ LastExitCode',因为它尚未设置。 在C:\ Users \ Administrator \ AppData \ Local \ Temp \ hudson242794619230845253.ps1:32 char:6 + exit $ LastExitCode + ~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation :(LastExitCode:String)[],ParentContainsErrorRecordException + FullyQualifiedErrorId:VariableIsUndefined – Kally

+0

当我执行此错误时,我总是收到此错误。我是否错过了更多。看到编辑过的代码和错误 – Kally

+0

对不起,还没有用过詹金斯。您需要在PowerShell_ISE的Jenkins之外运行脚本。在第一个命令处添加一个breakpoiont,然后在F10中遍历代码以确定哪条线路导致错误。有关调试,请参阅此MSDN文章:https://msdn.microsoft.com/en-us/powershell/scripting/core-powershell/ise/how-to-debug-scripts-in-windows-powershell-ise – TechSpud

0

尝试使用管理员帐户运行您的Jenkins,之所以出现此错误是因为您的jenkins用户无法与服务交互。

使用管理员帐户运行它将为其提供足够的权限与其他服务交互。