代码之家  ›  专栏  ›  技术社区  ›  Scott Brown

TFS Release automation的IIS管理-PowerShell脚本无法获取子项-路径IIS:\Sites

  •  0
  • Scott Brown  · 技术社区  · 7 年前

    在Powershell 5.1中使用Powershell WebAdministration模块时遇到困难。

    这是在服务器2008R2计算机上,运行IIS 7.5

    本模块似乎存在以下问题: 偶尔地 加载后,模块需要几毫秒才能完成初始化。建议在加载后执行简单的“写入输出”,以允许服务器完成初始化任务。我并没有在我管理的所有服务器上都看到它,但这台服务器的需求是一致的。

    还有一个问题,我发现人们在Get站点失败时遇到的,可以通过包装try/catch来解决。

    然而,我看到的问题是,即使使用已确定的解决方法,我也无法在交互式运行和从TFS Automated release执行的运行之间获得一致的结果。

    Import-Module WebAdministration
    $sites="none"
    Write-Output "suggested as a work around for the task dying for no apparent reason"
    try {
        $sites = Get-ChildItem -Path IIS:\Sites
        Write-Output "part of try"
    } catch {
        $sites = Get-ChildItem -Path IIS:\Sites
        Write-Output "part of catch"
    } finally {
        Write-Output  $sites
        Write-Output  $sites.GetType()
    }
    

    通过TFS Release automation(代理版本2.117.2,目标计算机上的PowerShell版本1.0.47)运行时:

     2018-01-25T13:18:29.5474995Z Importing alias 'End-WebCommitDelay'.
     2018-01-25T13:18:29.5474995Z 
     2018-01-25T13:18:29.5474995Z suggested as a work around for the task dying for no apparent reason
     2018-01-25T13:18:29.5474995Z part of catch
     2018-01-25T13:18:29.5474995Z 
     2018-01-25T13:18:29.5474995Z 
     2018-01-25T13:18:29.5631000Z Deployment status for machine 'DESTSERV:5985' : 'Passed'
    

    (未返回网站列表)

    作为交互式进程运行时(与同一用户)

    PS C:\Users\Install> C:\Installers\Modules\test-iis.ps1
    suggested as a work around for the task dying for no apparent reason
    part of try
    
    Name             ID   State      Physical Path                  Bindings
    ----             --   -----      -------------                  --------
    AppTest          2    Started    E:\WebApps\AppTest             http *:80:
                                                                    https *:443:
    Test             1    Stopped    C:\inetpub\wwwroot\Test        http *:80:
                                                                    https 
    
    
    
    Module                     : CommonLanguageRuntimeLibrary
    Assembly                   : mscorlib, Version=4.0.0.0, Culture=neutral, 
    PublicKeyToken=b77a5c561934e089
    TypeHandle                 : System.RuntimeTypeHandle
    DeclaringMethod            :
    BaseType                   : System.Array
    UnderlyingSystemType       : System.Object[]
    FullName                   : System.Object[]
    AssemblyQualifiedName      : System.Object[], mscorlib, Version=4.0.0.0, 
    Culture=neutral,
                                 PublicKeyToken=b77a5c561934e089
    Namespace                  : System
    GUID                       : 00000000-0000-0000-0000-000000000000
    IsEnum                     : False
    GenericParameterAttributes :
    IsSecurityCritical         : False
    IsSecuritySafeCritical     : False
    IsSecurityTransparent      : True
    IsGenericTypeDefinition    : False
    IsGenericParameter         : False
    GenericParameterPosition   :
    IsGenericType              : False
    IsConstructedGenericType   : False
    ContainsGenericParameters  : False
    StructLayoutAttribute      :
    Name                       : Object[]
    MemberType                 : TypeInfo
    DeclaringType              :
    ReflectedType              :
    MetadataToken              : 33554432
    GenericTypeParameters      : {}
    DeclaredConstructors       : {Void .ctor(Int32)}
    DeclaredEvents             : {}
    DeclaredFields             : {}
    DeclaredMembers            : {Void Set(Int32, System.Object), System.Object& 
    Address(Int32), System.Object Get(Int32),
                                 Void .ctor(Int32)}
    DeclaredMethods            : {Void Set(Int32, System.Object), System.Object& 
    Address(Int32), System.Object Get(Int32)}
    DeclaredNestedTypes        : {}
    DeclaredProperties         : {}
    ImplementedInterfaces      : {System.ICloneable, System.Collections.IList, 
    System.Collections.ICollection,
                                 System.Collections.IEnumerable...}
    TypeInitializer            :
    IsNested                   : False
    Attributes                 : AutoLayout, AnsiClass, Class, Public, Sealed, 
    Serializable
    IsVisible                  : True
    IsNotPublic                : False
    IsPublic                   : True
    IsNestedPublic             : False
    IsNestedPrivate            : False
    IsNestedFamily             : False
    IsNestedAssembly           : False
    IsNestedFamANDAssem        : False
    IsNestedFamORAssem         : False
    IsAutoLayout               : True
    IsLayoutSequential         : False
    IsExplicitLayout           : False
    IsClass                    : True
    IsInterface                : False
    IsValueType                : False
    IsAbstract                 : False
    IsSealed                   : True
    IsSpecialName              : False
    IsImport                   : False
    IsSerializable             : True
    IsAnsiClass                : True
    IsUnicodeClass             : False
    IsAutoClass                : False
    IsArray                    : True
    IsByRef                    : False
    IsPointer                  : False
    IsPrimitive                : False
    IsCOMObject                : False
    HasElementType             : True
    IsContextful               : False
    IsMarshalByRef             : False
    GenericTypeArguments       : {}
    CustomAttributes           : {[System.SerializableAttribute()]}
    
    
    PS C:\Users\Install>
    

    当WebAdministration无法正确导入时,似乎出现了比缓慢初始化更严重的问题。IIS:提供程序不起作用。。它不会出错。但它确实返回了一些信息-脚本顶部设置的值正在被覆盖。。。我只是没有得到网站列表。

    如交互式运行所示,存在站点,因此空返回值没有任何意义。

    更新日期:

    所有建议的解决方法都已应用,但catch中的Get ChildItem无法生成站点列表。如何获得一致的结果,以便在目标机器任务上以交互方式或通过TFS Release的PowerShell获取IIS中站点的完整列表??

    1 回复  |  直到 7 年前
        1
  •  0
  •   Andy Li-MSFT    7 年前

    我可以通过TFS Release automation无任何错误地运行脚本,并获得与交互过程一致的结果。(代理版本 2.117.1 ,目标计算机版本上的PowerShell 5.1.14393.1944 ).

    您需要设置TFS build agent service account 作为目标计算机中的本地管理员帐户(只需添加 生成代理服务帐户 到本地 administrators 组)。

    所以,请尝试一下。如果仍然不起作用,请尝试在目标计算机上升级PowerShell版本(可能是由PS版本引起的)。提到 Install PowerShell 5 in Windows Server 2008 R2 有关详细信息。

    enter image description here


    更新:

    通常,在使用task运行PS脚本时,脚本的输出不会返回到构建过程 PowerShell on the target machines .

    您需要更新脚本以获取输出(使用 Write-Verbose 而不是 Write-Output ),请尝试以下脚本,它对我很有用:

    Import-Module WebAdministration
    $sites="none"
    Write-Verbose "suggested as a work around for the task dying for no apparent reason"
    try {
        $sites = Get-ChildItem -Path IIS:\Sites | Out-String
        Write-Verbose "part of try"
    } catch {
        $sites = Get-ChildItem -Path IIS:\Sites | Out-String
        Write-Verbose "part of catch"
    } finally {
        Write-Verbose  $sites -verbose
        Write-Verbose  $sites.GetType() -verbose
    }
    

    enter image description here