2017-02-15 7 views
0

私はジェンキンス仕事をしてもPSスクリプトが正常に実行されて失敗している

停止WebServiceがサーバパスへジェンキンスワークスペースから

削除WebServiceの

コピー項目

WebServiceの

を作成するスクリプトの下に持っています

WebServiceを開始

私はJenkinsのジョブでこのPSスクリプトを実行し、この実行後にBuildが失敗したので電子メールを受け取ります。

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

もし、あなたが完全にそれをしていないなら、 'exit 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で:32文字::6 +終了$ LastExitCode + ~~~~~~~~~~~~~ + CategoryInfo:はInvalidOperation:(\ユーザーは、管理者\のAppData \ローカル\一時\のhudson242794619230845253.ps1を\しますLastExitCode:String)[]、ParentContainsErrorRecordException + FullyQualifiedErrorId:VariableIsUndefined – Kally

+0

このエラーをいつでも実行します。私はもっ​​と甘いものを紛失していますか?編集したコードとエラー – Kally

+0

を参照してください申し訳ありませんが、Jenkinsは使用していません。あなたはJenkinsの外でPowerShell_ISEの中でスクリプトを実行する必要があります。最初のコマンドで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ユーザーがサービスと対話できないためです。

管理者アカウントで実行すると、他のサービスとやり取りするのに十分な特権が与えられます。

関連する問題