powershell 如何获得IISreset状态-如果已启动则为true,如果已停止则为false

hwamh0ep  于 2023-04-30  发布在  Shell
关注(0)|答案(2)|浏览(106)

无法获取为IISStatus返回的$true$false

$status = iisreset /start

我想检查IIS是否已启动。

kwvwclae

kwvwclae1#

检查自动变量-或者
$LASTEXITCODE- * 包含运行的最后一个基于Windows的程序的退出代码。*

$?- * 包含最后一个命令的执行状态。如果最后一个命令成功,则包含True,如果失败,则包含False。*
考虑到以下关系(同一来源):
对于本机命令(可执行文件),当$LASTEXITCODE0时,$?设置为True,当$LASTEXITCODE为任何其他值时,$?设置为False

示例1

$status = iisreset /start
"$? $LASTEXITCODE"
$status
False 5

Access denied, you must be an administrator of the remote computer to use this 
command. Either have your account added to the administrator local group of 
the remote computer or to the domain administrator global group.

示例2(提升的PowerShell):

$status = iisreset /start
"$? $LASTEXITCODE"
$status
True 0

Attempting start...
Internet services successfully started
xxb16uws

xxb16uws2#

根据我的评论。

Get-Service -Name IISADMIN
# Results
<#
Status   Name               DisplayName                           
------   ----               -----------                           
Running  IISADMIN           IIS Admin Service
#>

iisreset /stop

# Results
<#
Attempting stop...

Internet services successfully stopped
#>


Get-Service -Name IISADMIN
# Results
<#
Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  IISADMIN           IIS Admin Service 
#>

iisreset /start

# Results
<#
Attempting start...

Internet services successfully started
#>

Get-Service -Name IISADMIN
# Results
<#
Status   Name               DisplayName                           
------   ----               -----------                           
Running  IISADMIN           IIS Admin Service
#>


Stop-Service -Name IISADMIN
# Results
<#
WARNING: Waiting for service 'IIS Admin Service (IISADMIN)' to stop...
WARNING: Waiting for service 'IIS Admin Service (IISADMIN)' to stop...
#>

Get-Service -Name IISADMIN
# Results
<#
Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  IISADMIN           IIS Admin Service 
#>

Start-Service -Name IISADMIN
Get-Service -Name IISADMIN
# Results
<#
Status   Name               DisplayName                           
------   ----               -----------                           
Running  IISADMIN           IIS Admin Service 
#>

(Get-Service -Name IISADMIN).Status
# Results
<#
Running
#>

仅供参考,有许多IIS cmdlet可供使用,而不必还原为可执行文件。

Get-Command -Name 'IIS*'

它们是“IIS管理”模块的一部分。

Find-Module -Name 'IIS*'

Version              Name                                Repository           Description                                                                                       
-------              ----                                ----------           -----------                                                                                       
1.1.0.0              IISAdministration                   PSGallery            IIS Configuration management module

相关问题