【问题标题】:Remove windows service with Powershell called by Octopus Deploy使用 Octopus Deploy 调用的 Powershell 删除 Windows 服务
【发布时间】:2013-12-06 16:04:29
【问题描述】:

我一直在尝试使用 powershell 停止和删除远程服务器上的 Windows 服务,特别是通过 Octopus Deploy 自动调用的 PostDeploy.ps1。我已尝试按照here:

的描述使用以下内容
$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service.delete()

当脚本运行时,我得到这个:

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     : 
__DYNASTY        : __PARAMETERS
__RELPATH        : 
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
ReturnValue      : 0
PSComputerName   : 

但服务仍然存在。我也尝试过使用

net stop "my service"

紧随其后

sc.exe delete "my service"

但会收到拒绝访问的消息。当我使用以管理员身份运行在盒子上手动使用这些命令时,我可以删除和删除服务。

我也尝试按照章鱼部署页面here.上的示例描述进行操作

有关信息,我可以通过 powershell 安装和启动服务。

我可以在我的 ps1 文件中添加一些内容以强制命令以管理员身份运行,还是我需要做其他事情?

【问题讨论】:

  • 这可能是该机器上的八达通代理没有适当权限的问题。您能否验证该代理以谁的身份运行以及是否是管理员?
  • 八达通代理作为本地系统运行,这大概意味着它拥有停止和删除服务的权限?

标签: windows powershell service wmi octopus-deploy


【解决方案1】:

部署目标机器上的 Octopus 代理执行 Powershell 脚本。因此,您需要确保运行 Octopus 代理的用户已设置适当的权限来管理其他服务:

查看这些文章:
1.http://octopusdeploy.com/documentation/configuration/custom-user
2.http://help.octopusdeploy.com/discussions/problems/64-installing-tentacle-under-workgroup-user-or-domain-accoun
3.http://octopusdeploy.com/documentation/security/octopus-tentacle

【讨论】:

    【解决方案2】:

    如果某些命令需要以管理员身份运行,您可以使用计划任务来安排脚本并使其以系统身份运行。

    您也可以以管理员身份启动 PowerShell,即使在使用 PowerShell 时使用:

    Start-Process -verb runas PowerShell
    

    你可以试试这个:

    Start-Process -verb runas PowerShell PostDeploy.ps1
    

    【讨论】:

      【解决方案3】:

      如果您有 .ps1 脚本在本地运行,则只需使用 PsExec 工具在远程计算机上运行脚本,如下所示:

      - Create a share on your local machine and put both the psexec.exe and the .ps1 script in it
      - Run the following command - change to direct in cmd where the psexec file is:
      
      PsExec.exe \\REMOTECOMPUTER -u -p \\mypc\shared\PostDeploy.ps1
      

      -u 开关(指定域用户名用于登录具有管理权限或域管理员权限的远程计算机) -p 开关(为上面指定的管理/域帐户指定密码。如果省略此选项,系统将提示您输入隐藏密码)

      【讨论】:

        猜你喜欢
        • 2015-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多