我在Powershell(以管理员身份)PSVersion 5.1.17763.1007中创建了一项新服务。新服务存在,但无法在Powershell或服务中运行。
在Powershell中:
PS C:\ Windows \ system32>
Get-Service ztest|Start-Service
Start-Service:由于以下原因,无法启动服务'ztest(zTest)' 出现以下错误:无法在计算机“。”上启动服务zTest。在 行:1字符:19 +获得服务ztest |开始服务 \ ~~~~~~~~~~~~~ + + CategoryInfo:OpenError:(System.ServiceProcess.ServiceController:ServiceController) [启动服务],ServiceCommandException + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
在服务中: 我收到1053错误服务未及时响应启动或控制请求。
已完成的操作: 我使用了Powershell脚本,该脚本已通过Win-PS2Exe转换为.exe文件。当我运行.exe文件时,程序将按预期工作。
创建新服务:
New-Service -Name zTest1 -BinaryPathName $filepath -DisplayName zTest1 -Description $description -StartupType Automatic -Credential $cred
我更改了Regedit中的超时设置,将文件和文件夹都添加到了受信任的位置,授予了所有权并完全访问.exe文件和文件夹的服务帐户,我编写了PS脚本并将其转换为.exe( -PS2Exe)powershell.exe -executionpolicy bypass -file 'C:\users\...\desktop\RunTESTNoCommentsReview.exe'
关于.exe文件: .exe文件没有结束。在无限循环中,每次暂停1分钟。
我读过很多人说潜在的问题,其名称与Windows Service Manager中的名称不符,但除了他们的评论外,我找不到任何其他文档。
我的机器: 我当前正在运行最新的Windows Server 2019 Standard。
我不确定是否应该在某个地方注册该服务,或者我可能还会错过什么。
答案 0 :(得分:0)
继续我的评论。
您没有显示您所说的用作服务的任何代码。
有many examples of how to do this all over the web。
对于Example:
$serviceName = "MyService"
if (Get-Service $serviceName -ErrorAction SilentlyContinue)
{
$serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name='$serviceName'"
$serviceToRemove.delete()
"service removed"
}
else
{
"service does not exists"
}
"installing service"
$secpasswd = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential (".\MYUser", $secpasswd)
$binaryPath = "c:\servicebinaries\MyService.exe"
New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic -credential $mycreds
"installation completed"
因此,如果没有上述的MRE之类的东西,您就迫使我们假设。