我正在尝试使用 PowerShell 在批处理文件中创建问候语,但出现错误
代码:-
powershell -Command $Hour = (Get-Date).Hour; Write-host "Hi"; If ($Hour -lt 12) {"Good Morning $($Env:UserName)" }; ElseIf ($Hour -gt 16) {"Good Eventing $($Env:UserName)"}; Else {"Good Afternoon $($Env:UserName)"}
pause
我收到的错误:-
Good : The term 'Good' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:1 char:60
+ ... our = (Get-Date).Hour; Write-host Hi; If ($Hour -lt 12) {Good Morning ...
+ ~~~~
+ CategoryInfo : ObjectNotFound: (Good:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
ElseIf : The term 'ElseIf' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:93
+ ... i; If ($Hour -lt 12) {Good Morning $($Env:UserName) }; ElseIf ($Hour ...
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (ElseIf:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Else : The term 'Else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:1 char:149
+ ... ElseIf ($Hour -gt 16) {Good Eventing $($Env:UserName)}; Else {Good A ...
+ ~~~~
+ CategoryInfo : ObjectNotFound: (Else:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
答案1
将命令放在引号内。删除 和 之间/之前的分号elseif
。else
您的命令应如下所示
powershell -Command "$Hour = (Get-Date).Hour; Write-host 'Hi'; If ($Hour -lt 12) {'Good Morning $($Env:UserName)' } ElseIf ($Hour -gt 16) {'Good Eventing $($Env:UserName)'} Else {'Good Afternoon $($Env:UserName)'}"
pause