小编Jon*_*nyG的帖子

Powershell无法返回正确的退出代码

当使用-File命令行开关执行Powershell脚本(在2.0中)并在Param中显式定义输入参数时,退出代码始终为"0"(永不失败),而不是正确返回已定义或预期的错误代码.
使用显式参数定义和-Command开关时不会发生这种情况,但是出于无关的目的,我需要在脚本中保留-File开关.

任何有关解决方法的帮助(不涉及删除显式参数定义)都将非常有用.

Powershell"无法返回正确的退出代码":

exit1.ps1:调用显式定义参数的脚本.Errorlevel始终为0,即使脚本的某些部分无意中失败也是如此.

param(
    [Parameter(mandatory=$true)][string]$arg1,
    [Parameter(mandatory=$true)][string]$arg2,
    [Parameter(mandatory=$true)][string]$arg3
);
exit 1;
Run Code Online (Sandbox Code Playgroud)

输出:

C:\temp\testnant>powershell -noprofile -nologo -noninteractive -executionpolicy Bypass -file .\exit1.ps1 "one" "two" "three"

C:\temp\testnant>echo %errorlevel%
0
Run Code Online (Sandbox Code Playgroud)




现在,让我们在将param函数修改为不那么明确时尝试相同的事情:

Exit1LooseParam.ps1:

param(
    $arg1,
    $arg2,
    $arg3
);
exit 1;
Run Code Online (Sandbox Code Playgroud)

输出(带三个参数):

C:\temp\testnant>powershell -noprofile -nologo -noninteractive -executionpolicy Bypass -file .\Exit1looseParam.ps1 "one" "two" "three"

C:\temp\testnant>echo %errorlevel%
1
Run Code Online (Sandbox Code Playgroud)




看来,当你明确定义输入参数时,Powershell由于某种原因似乎"失去了它的思维"并且无法返回正确的退出代码.

有没有人有解决方法或能够解释为什么会这样?

parameters powershell exit-code parameter-passing errorlevel

12
推荐指数
1
解决办法
5881
查看次数

Powershell替代SetSPN

如何让SetSPN使用DC的特定DC或IP地址?从不属于Lab域的跳转框设置Lab DC内的Lab帐户的服务主体名称.

通过添加"-Server"参数完成大多数Powershell AD cmdlet.

使用SetSPN似乎不可能.

powershell active-directory powershell-3.0

3
推荐指数
1
解决办法
7129
查看次数

使用Powershell操作IIsWebVirtualDir上的IP限制

在使用Powershell操作IIsWebVirtualDir(虚拟目录)上的IP限制时遇到问题.

但是,我有在VBS中执行此操作的代码,所以希望这将是一个简单的事情来获得帮助:)

VBS中的代码:

 Sub Add2IPRList(WebsiteADSI, strIP2Add, strIP2AddSubnet)
    Set WebRootObj = GetObject(WebsiteADSI) '"IIS://localhost/W3SVC/2/ROOT/TestVDIR"
    set IPSecObj = WebRootObj.IPSecurity
    If(IPSecObj.GrantByDefault)then
        IPList = IPSecObj.IPDeny
    Else
        IPList = IPSecObj.IPGrant
    End If

    ReDim Preserve IPList (Ubound(IPList)+1)     'resize local copy of IPList array to CurrentSize+1
    IPList(Ubound(IPList))=strIP2Add&","&strIP2AddSubnet     'add the entry to the end of the array


    If(IPSecObj.GrantByDefault)then
        IPSecObj.IPDeny = IPList
    Else
        IPSecObj.IPGrant = IPList
    End If

    WebRootObj.IPSecurity = IPSecObj
    WebRootObj.SetInfo        'apply the setttings on the server.
    set IPSecObj = Nothing
    set WebRootObj = Nothing    
End Sub
Run Code Online (Sandbox Code Playgroud)

在Powershell中尝试1:对象返回,但是类型很奇怪.

PS …
Run Code Online (Sandbox Code Playgroud)

powershell iis-6 powershell-2.0

2
推荐指数
1
解决办法
3004
查看次数