Powershell的退出码


0
DJ asked 6年 ago

如果我想写一个脚本,当我退出时可以向batch那样使用%ERRORLEVEL%查看退出码,不知道powershell中有什么命令能这样用的,exit吗,在powershell中是用什么命令查看退出码的?

×用微信扫描并分享
0 Answers
1
Mooser Lee 管理员 answered 6年 ago

$?

PS> ping dfadsfdfdsfdsf.eee.com
Ping request could not find host dfadsfdfdsfdsf.eee.com. Please check the name and try aga
PS> $?
False
PS> ping baidu.com

Pinging baidu.com [123.125.114.144] with 32 bytes of data:
Reply from 123.125.114.144: bytes=32 time=185ms TTL=41
Reply from 123.125.114.144: bytes=32 time=185ms TTL=41
Reply from 123.125.114.144: bytes=32 time=185ms TTL=41
Reply from 123.125.114.144: bytes=32 time=184ms TTL=41

Ping statistics for 123.125.114.144:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 184ms, Maximum = 185ms, Average = 184ms
PS> $?
PS> True

 

DJ replied 6年 ago

我想问一下,我在脚本里面能设置退出码吗,比如我想在执行成功后,如果用$?来查看他的退出码是1,那我需要怎么设置呢,exit 1?