2015-09-22 27 views
0

我对发布/更新新的DLL的运行窗口服务电源外壳脚本:电源外壳Windows服务更新的DLL进程

Import-Module WebAdministration 

function Main(
[string] $siteName = $(Throw "Value cannot be null: siteName"), 
[string] $sitePath = $(Throw "Value cannot be null: sitePath"), 
[string] $servicePath = $(Throw "Value cannot be null: sitePath"), 
[string] $serviceName = $(Throw "Value cannot be null: sitePath"), 
[string] $buildConfiguration = $(Throw "Value cannot be null: sitePath")) 
{ 
... 

    $serviceBinPath = Join-Path $serviceBinPath $buildConfiguration 
    Write-Host "Directory of Windows Service : $($serviceBinPath)`r`n" 

    StopWindowsService $serviceName 
    RemoveFiles $servicePath 
    CopyFiles $serviceBinPath $servicePath 
    StartWindowsService $serviceName 
} 

function RemoveFiles(
    [string] $path = $(Throw "Value cannot be null: sitePath")) 
{ 
    If (Test-Path $path) 
    { 
     Write-Host "Removing folder ($path)...`r`n" 
     Remove-Item -Recurse -Force "$($path)*" 
     Write-Host "Successfully removed website folder ($path)...`r`n"   
    } 
} 

function CopyFiles(
    [string] $sourcePath = $(Throw "Value cannot be null: sitePath"), 
    [string] $destinationPath = $(Throw "Value cannot be null: sitePath")) 
{ 
    If ((Test-Path $sourcePath) -and (Test-Path $destinationPath)) 
    { 
     Write-Host "Copy files from ($sourcePath) to folder ($destinationPath)...`r`n" 
     Copy-Item "$($sourcePath)\*" $destinationPath -Recurse -Force 
     Write-Host "Successfully copied files from ($sourcePath).`r`n" 
    } 
} 
function StopWindowsService(
    [string] $serviceName = $(Throw "Value cannot be null: siteName")) 
{ 
    $serviceBefore = Get-Service $serviceName 
    Write-Host "($serviceName) is now ($serviceBefore.status)...`r`n" 

    Write-Host "Stopping Windows Service ($serviceName)...`r`n" 
    Stop-Service $serviceName 
    Write-Host "Successfully stopped Windows Service ($serviceName)...`r`n" 

    $serviceAfter = Get-Service $serviceName 
    Write-Host "($serviceName) is now ($($serviceAfter.status))...`r`n" 
} 

function StartWindowsService(
    [string] $serviceName = $(Throw "Value cannot be null: siteName")) 
{ 
    $serviceBefore = Get-Service $serviceName 
    Write-Host "($serviceName) is now ($serviceBefore.status)...`r`n" 

    Write-Host "Starting Windows Service ($serviceName)...`r`n" 
    Start-Service $serviceName 
    Write-Host "Successfully started Windows Service ($serviceName)...`r`n" 

    $serviceAfter = Get-Service $serviceName 
    Write-Host "($serviceName) is now ($($serviceAfter.status))...`r`n" 
} 

所有罚款与启动/停止/复制新的Windows服务的DLL。 但当我尝试停止服务所有的人都被锁定后,删除旧文件,我得到错误:

Remove-Item : Cannot remove item ...\WindowsService\bin\Autofac.dll: Access to the path '...WindowsService\bin\Autofac.dll' is denied. 

对于所有的dll文件。

可能需要卸载/安装服务isnstead停止/运行?有任何想法吗?

回答

0

我找到了解决方案。 第一件事:我运行这个脚本安装windows服务处于停止状态,并且dll已经在文件夹中。第一次运行没有产生任何错误。第二次运行后,我得到了错误。 当我停止服务后,加入等待和删除之前旧的DLL的一切都很好:

StopWindowsService $serviceName 

Start-Sleep -s 5 

RemoveFiles $servicePath 
CopyFiles $serviceBinPath $servicePath 
StartWindowsService $serviceName 

我认为,这是因为这些文件必须停止服务后没有立即解锁。

非常感谢。对不起,因为英文不好

0

停止服务应该足够了。 检查以下内容:

  1. 您正在使用直接pathes您的文件
  2. 运行脚本是根据正常priveledged用户运行的应用程序或控制台。
  3. 检查dll文件夹的安全性。检查您的应用程序运行在priveldged删除指定文件夹中的文件