【问题标题】:IIS Administration from TFS Release automation - PowerShell script fails Get-ChildItem -Path IIS:\Sites来自 TFS 发布自动化的 IIS 管理 - PowerShell 脚本失败 Get-ChildItem -Path IIS:\Sites
【发布时间】:2018-07-06 05:25:27
【问题描述】:

我在将 Powershell WebAdministration Module 与 Powershell 5.1 一起使用时遇到了困难。

这是在运行 IIS 7.5 的 Server 2008R2 机器上

此模块似乎存在问题,偶尔该模块在加载后需要几毫秒才能完成初始化。建议在加载后做一个简单的“写输出”,以允许服务器完成初始化任务。我在我管理的所有服务器上都没有看到它,但是这个特定的服务器在它的需要上是一致​​的。

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

但是,我看到的问题是,即使使用已确定的变通方法,我在交互式运行和从 TFS 自动版本执行的运行之间也无法获得一致的结果。

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 发布自动化运行时(代理版本 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:提供程序不起作用..它不会引发错误。虽然它确实返回了一些东西 - 脚本顶部设置的值被覆盖了......我只是没有得到网站列表。

从交互式运行中可以看出,存在站点,因此空返回值没有任何意义。

更新:

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

【问题讨论】:

  • 别忘了问你的问题。

标签: powershell iis tfs azure-pipelines-release-pipeline


【解决方案1】:

我可以通过 TFS Release 自动化运行脚本而不会出现任何错误,并通过交互过程获得一致的结果。 (代理版本2.117.1,目标机器上的PowerShell版本5.1.14393.1944)。

您需要将TFS build agent service account 设置为目标机器中的本地管理员帐户(只需将build agent service account 添加到本地administrators 组)。

所以,请尝试一下。如果还是不行,尝试升级目标机器上的 PowerShell 版本(可能是 PS 版本引起的)。详情请参考Install PowerShell 5 in Windows Server 2008 R2


更新:

一般来说,当运行带有任务PowerShell on the target machines的PS脚本时,脚本的输出不会返回到构建过程。

您需要更新脚本以取回输出(使用 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
}

【讨论】:

  • TFS 发布服务器(运行代理的位置)和终端 Web 服务器都运行 PowerShell 5.1.14409.1012。不幸的是,构建服务帐户不能与 vsts 代理和 Web 服务器之间的帐户相同,因为构建服务器位于域内,而 Web 服务器位于工作组内。我能做的最好的事情是在 Web 服务器上创建一个与域用户具有相同用户名/密码的本地用户,并将该用户添加到本地管理员。在该配置中,我仍然有相同的结果 - TFS 运行执行捕获路径,并且不返回任何内容。这是在代理 2.117.2 上运行的
  • @ScottBrown 代理应该没问题,我测试了 2.117.12.122.1,都可以正常工作 (no 2.117.2 on my side for testing)。根据您的说明,我认为您可以尝试使用任务 PowerShell on Target Machines 而不是当前的 PowerShell 任务,脚本应该在网络服务器上运行。
  • 正在使用 PowerShell On Target Machines 任务 - 版本 1.0.47,并且正在 Web 服务器上调用该任务(在问题中编辑为 DESTSERV)
  • @ScottBrown 误会了,之前还以为是PS版。有关详细信息,请参阅我的更新答案。
  • 谢谢安迪-这可行,但我不确定为什么需要详细。我在其他系统上不需要它(我已经部署到大约 60 个 Web 和应用程序服务器)只有这台机器似乎需要这种类型的 TLC,而且我的 SysOps 人员都看不到这台服务器与任何其他 Web 的不同之处或我们部署到的应用系统。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多