【问题标题】:Power Shell Windows Service Update dll's processPowershell Windows 服务更新 dll 进程
【发布时间】:2015-09-22 18:08:25
【问题描述】:

我有用于发布/更新新 dll 以运行 Windows 服务的 Power Shell 脚本:

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 文件。

可能需要卸载/安装服务而不是停止/运行?有什么想法吗?

【问题讨论】:

    标签: c# shell dll


    【解决方案1】:

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

    1. 您正在使用文件的直接路径
    2. 运行您的脚本的应用程序或控制台在适当的特权用户下运行。
    3. 检查 dlls 文件夹的安全性。检查运行您的应用程序的用户是否有权删除指定文件夹中的文件

    【讨论】:

      【解决方案2】:

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

      StopWindowsService $serviceName
      
      Start-Sleep -s 5
      
      RemoveFiles $servicePath 
      CopyFiles $serviceBinPath $servicePath
      StartWindowsService $serviceName
      

      我认为是因为停止服务后文件还没有立即解锁。

      非常感谢。抱歉英语不好

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-17
        • 2013-11-07
        • 2021-11-05
        • 1970-01-01
        • 2020-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多