简体   繁体   中英

IIS Administration from TFS Release automation - PowerShell script fails Get-ChildItem -Path IIS:\Sites

I'm having difficulty in using the Powershell WebAdministration Module with Powershell 5.1.

This is on a Server 2008R2 machine, running IIS 7.5

There seems to be an issue with this module in that occasionally the module requires a few ms to complete initialization after loading. The recommendation is to do a simple 'write-output' after loading to allow the server to complete the init tasks. I dont see it on all the servers I'm managing, but this particular server is consistent in its need.

There's also an issue that I found people had with Get-Sites failing that could be dealt with by wrapping in a try/catch.

However, the problem I'm seeing is that even with the identified workarounds, I'm not getting consistent results between an interactive run, and a run that is performed from 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()
}

When run via TFS Release automation (agent version 2.117.2, PowerShell on the target machine version 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'

(no sitelist is returned)

When run as an interactive process (with same user)

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>

When WebAdministration doesn't import properly it seems there is more going wrong than just a slow initialization. The IIS: provider is not functional.. it throws no error. It does return something though - the value set at the top of the script is being overwritten... I just dont get the sites list.

As can be seen by the interactive run, there are sites present, so an empty return value does not make any sense.

Updated:

All the suggested workarounds have been applied yet the Get-ChildItem in the catch fails to produce the sites list. How do I get a consistent result where I can obtain the full list of sites in IIS both interactively and through TFS Release's PowerShell on the target machine task??

I can run the script via TFS Release automation without any error and get the consistent result with the interactive process. (Agent version 2.117.1 , PowerShell on the target machine version 5.1.14393.1944 ).

You need to set the TFS build agent service account as an local administrator account in the target machine (Just add the build agent service account to the local administrators group).

So, please have a try with that. If that still not work, just try to upgrade the PowerShell version on your target machine (may be caused by the PS version). Refer to Install PowerShell 5 in Windows Server 2008 R2 for details.

在此处输入图片说明


UPDATE:

Generally, the output of the scripts will not back to build process when run the PS scripts with task PowerShell on the target machines .

You need to update the script to get back the outputs (use Write-Verbose instead of Write-Output ), just try below scripts, it works for me:

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
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM