![](/img/trans.png)
[英]PowerShell: command “Get-ChildItem IIS:\Sites” causes an error
[英]IIS Administration from TFS Release automation - PowerShell script fails Get-ChildItem -Path IIS:\Sites
我在將Powershell WebAdministration模塊與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 Release Automation(代理程序版本2.117.2,在目標計算機版本1.0.47上運行PowerShell)運行時:
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無法生成站點列表。 我如何獲得一致的結果,以便可以在目標機器任務上以交互方式以及通過TFS發行版的PowerShell獲得IIS中站點的完整列表?
我可以通過TFS發行版自動化運行腳本,而不會出現任何錯誤,並且可以通過交互式過程獲得一致的結果。 (代理程序版本2.117.1
,目標計算機版本5.1.14393.1944
上的PowerShell)。
您需要將TFS build agent service account
為目標計算機中的本地管理員帳戶(只需將build agent service account
添加到本地administrators
組)。
因此,請嘗試一下。 如果仍然無法解決問題,請嘗試在目標計算機上升級PowerShell版本(可能是PS版本引起的)。 有關詳細信息,請參考在Windows Server 2008 R2中安裝PowerShell 5 。
更新:
通常, PowerShell on the target machines
使用任務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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.