Powershell为啥有些指令需要加()


PowerShell交流中心分类: QuestionsPowershell为啥有些指令需要加()
0
Eso asked 7 年 ago

Powershell为啥有些指令需要加()

2 Answers
1
Best Answer
Mooser Lee 管理员 answered 7 年 ago

任何编程语言或者脚本语言中的小括号,主要作用都是用来改变默认操作符的运算顺序的,PowerShell也不例外。
比如:

PS> 1-3/3
0
PS> (1-3)/3
-0.666666666666667
0
Second answered 7 年 ago

产品设计吧,调用对象的方法时会这么用
如(get-data).Addday(4)

Mooser Lee 管理员 replied 7 年 ago

正解,如果去掉第一对小括号,PowerShell语法解析器会认为Get-Date.AddDays为一个命令,然后执行时就会报错:
Get-Date.AddDays : The term ‘Get-Date.AddDays’ is not recognized as the name of a cmdlet, function, script file, or ope
rable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again
.