gpt4 book ai didi

powershell - 在服务名称带有破折号的地方使用Get-Service

转载 作者:行者123 更新时间:2023-12-03 01:28:02 28 4
gpt4 key购买 nike

我想使用PS脚本启动和停止特定服务。当我使用以下命令时:

Get-Service "SAS [SASConfig-Lev1] SASMeta - Metadata Server" | Where {$_.status –eq 'Stopped'} | Start-Service

我得到错误:
Get-Service : The specified wildcard character pattern is not valid: SAS [SASConfig-Lev1] SASMeta - Metadata Server

At line:1 char:1

+ Get-Service "SAS [SASConfig-Lev1] SASMeta - Metadata Server" | Where ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-Service], WildcardPatternException
+ FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.GetServiceCommand

问题不在Where子句中,因为如果删除它,则会出现相同的错误。
Get-Service "SAS [SASConfig-Lev1] SASMeta - Metadata Server" | Start-Service

我已经试过了
Get-Service 'SAS [SASConfig-Lev1] SASMeta - Metadata Server' | Start-Service

还有这个
Get-Service "SAS [SASConfig`-Lev1] SASMeta - Metadata Server" | Start-Service

但得到同样的错误。

关于如何在服务名称中包含破折号并使 Get-Service运行无误的任何建议?

最佳答案

您需要进行一些转义操作才能将方括号放入服务名称值中。开头括号前的两个前导坟墓标记(``)没错。转义命令使Get-Service command解析为,而不是,将第一个参数视为包含通配符。

Get-Service "SAS ``[SASConfig-Lev1] SASMeta - Metadata Server"

我的Powershell控制台示例:
> Get-Service "SAS ``[BIserver-Lev1] Web Infrastructure Platform Data Server" | Format-List


Name : SAS [BIserver-Lev1] Web Infrastructure Platform Data Server
DisplayName : SAS [BIserver-Lev1] Web Infrastructure Platform Data Server
Status : Running
DependentServices : {}
ServicesDependedOn : {RPCSS}
CanPauseAndContinue : True
CanShutdown : True
CanStop : True
ServiceType : Win32OwnProcess

Wildcards
PowerShell supports the following wildcard characters:

Wildcard Description
* Match zero or more characters
? Match one character in that position
[ ] Match a range of characters i.e. [a-j]
[ ] Match specific characters i.e. [adrstz]

关于powershell - 在服务名称带有破折号的地方使用Get-Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60626185/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com