Reputation: 1879
I have below script which will
Stop WebService
Delete WebService
Copy items from Jenkins workspace to server path
Create WebService
Start WebService
I run this PS script in Jenkins job and after execution of this,I get a email as Build is failed.
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
My PS script
# 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"
Is there anything i am missing here
Upvotes: 2
Views: 2136
Reputation: 371
Try running your Jenkins with the administrator account, the reason why you are getting this error is because your jenkins user is unable to interact with the services.
Running it with the administrator account will give it sufficient privileges to interact with other services.
Upvotes: 0
Reputation: 3528
Your code needed a little tidying up, as you had a couple of repeating sections, that were performed whether the service existed or not. Also flipped the if logic.
Added some steps at the start of your code to cause PowerShell to be more strict and drop out on any errors with a non-zero code.
This has not been tested, so may need a little fine-tuning. However, it should be 95% there, hopefully!
# 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"
Upvotes: 4