powershell - 在 Powershell 中停止服务时是否有绕过依赖服务的功能?
问题描述
我的 Powershell 代码有一些问题
我需要使用这个脚本停止和禁用一些服务但是有一些问题,这里是:
Get-Content -path $PWD\servicestop1.txt | ForEach-Object
{
$service = $_
(Set-Service -Name $service -Status Stopped -StartupType Disabled -PassThru )
}
1 - 当我想停止一些服务时遇到一些依赖问题
2 - 我无法在同一个脚本中禁用和停止它们
你有什么主意吗?非常感谢!
PS:我尝试使用参数“-force”但没有用
解决方案
在不知道servicestop1.txt
实际持有有效服务名称的情况下,您可以尝试以下操作:
Get-Content -Path "$PWD\servicestop1.txt" | ForEach-Object {
$service = Get-Service -Name $_ -ErrorAction SilentlyContinue
if ($service) {
if ($service.Status -eq 'Running') {
# stop the service and wait for it
$service | Stop-Service -Force
}
if ($service.StartType -ne 'Disabled') {
$service | Set-Service -StartupType Disabled
}
}
else {
Write-Warning "Could not find service with name '$_'"
}
}
推荐阅读
- java - @Autowired Java @Configuration 文件中的类成员
- hibernate - 使用 CiriteriaBuilder JPA 创建联接查询
- python-3.x - 比较列行列表并在熊猫中使用过滤器
- c# - 获取 CIM_Tachometer 的当前读数以获取风扇当前速度
- react-native - 获取 API 调用在反应原生 android 中失败以获取自签名证书
- php - 使用已经打开的 PHP 套接字
- java - Java Spring Boot Hibernate 原生查询
- python - 如何在同一图表上绘制包含日期和整数的列表(相关性)?
- php - wordpress 插件激活/停用功能未触发
- spring - 如何让 JNDI 在 Spring 中查找可选