对于dotnetcore 3.1 Windows服务,如何使用powershell脚本5.1停止、卸载、安装和启动服务 首先,我为PS5.1编写了以下脚本(我知道PS6.0有删除服务选项,但没有5.1)。 我有所有必需的.exe、dll和config以及这个脚本,该脚本打包为工件,并将部署在目标的Headnode目录中。 不确定以下脚本是否将卸载并安装该服务。但我可以看到,当我运行它时,它正在删除并启动新服务** Powershell core 6.0/7.0版本似乎有get-WMIOObject,WMI已被弃用,那么我们必须使用CIM使用get-CIMInstance

对于dotnetcore 3.1 Windows服务,如何使用powershell脚本5.1停止、卸载、安装和启动服务 首先,我为PS5.1编写了以下脚本(我知道PS6.0有删除服务选项,但没有5.1)。 我有所有必需的.exe、dll和config以及这个脚本,该脚本打包为工件,并将部署在目标的Headnode目录中。 不确定以下脚本是否将卸载并安装该服务。但我可以看到,当我运行它时,它正在删除并启动新服务** Powershell core 6.0/7.0版本似乎有get-WMIOObject,WMI已被弃用,那么我们必须使用CIM使用get-CIMInstance,powershell,.net-core,azure-devops,windows-services,.net-core-3.1,Powershell,.net Core,Azure Devops,Windows Services,.net Core 3.1,休息似乎都很好。删除的代码: $acl = Get-Acl "C:\Program Files\Matt\Wservice" $aclRuleArgs = "XYZ", "Read,Write,ReadAndExecute", "ContainerInherit,ObjectInherit", "None", "Allow" $accessRule = New-Object S

休息似乎都很好。删除的代码:

$acl = Get-Acl "C:\Program Files\Matt\Wservice"
$aclRuleArgs = "XYZ", "Read,Write,ReadAndExecute", "ContainerInherit,ObjectInherit", "None", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($aclRuleArgs)
$acl.SetAccessRule($accessRule)
if (Get-Service "Wservice" -ErrorAction 'SilentlyContinue')
{
     Stop-Service -Name Wservice -ErrorAction SilentlyContinue -Force
     (Get-CIMInstance -Class Win32_Service -filter "Name='Wservice'").delete()
     Write-Host "Please wait until removing the : Wservice  " 
     Start-Sleep -s 30 
    }
$Username = 'xyz'
$Password = '123'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$pass
New-Service -Name  Wservice -BinaryPathName "C:\Program Files\Matt\Wservice\Wservice.exe" -Credential $MySecureCreds -DisplayName "WserviceService"  -StartupType Automatic
Start-Service -Name "Wservice"

感谢@wasif Hasan的检查和更正。但我依赖于使用PS5.1,而不是6.0或7.0。此外,我还看到了一些带有以下.netframework运行时选项的代码,不确定是否有类似于.netcore3.1运行时中使用的代码
$RunTimeDirectory=$([System.runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())$InstallUtilPath=(“{0}{1}”-f$RunTimeDirectory,“InstallUtil.exe”)
另外,如果我正确地完成了停止,请卸载,然后干净地安装并启动,而不会出现编写脚本的问题。请注意:关于此。。。[但我依赖于使用PS 5.1],这并不重要,因为CIM也在v5x中,最好养成使用CIM vs WMI的习惯,除非CIM没有提供WMI没有提供的某些信息。此外,避免在评论中添加代码响应,更新您的原始帖子,以便更恰当地跟踪和响应。但根据您的提问,您似乎在问,[如何在PSv5x中复制PSCore Remove Service cmdlet?]。还是我弄错了?请忽略我上面的评论,因为我的代码起作用,我不需要进一步的帮助。感谢您查看@postanote
$acl = Get-Acl "C:\Program Files\Matt\Wservice"
$aclRuleArgs = "XYZ", "Read,Write,ReadAndExecute", "ContainerInherit,ObjectInherit", "None", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($aclRuleArgs)
$acl.SetAccessRule($accessRule)
if (Get-Service "Wservice" -ErrorAction 'SilentlyContinue')
{
     Stop-Service -Name Wservice -ErrorAction SilentlyContinue -Force
     (Get-CIMInstance -Class Win32_Service -filter "Name='Wservice'").delete()
     Write-Host "Please wait until removing the : Wservice  " 
     Start-Sleep -s 30 
    }
$Username = 'xyz'
$Password = '123'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$pass
New-Service -Name  Wservice -BinaryPathName "C:\Program Files\Matt\Wservice\Wservice.exe" -Credential $MySecureCreds -DisplayName "WserviceService"  -StartupType Automatic
Start-Service -Name "Wservice"