2011-03-24 96 views
12

我試過這段代碼,它看起來工作正常。但是,我注意到,如果您將用戶名和密碼分配給不存在的帳戶,代碼將繼續執行而不會出現問題。另外,如果你分配了一個無效的賬戶並且調用stop()然後start(),IIS池確實停止並且開始了!此外,當我去InetMgr並開始,停止或recylce游泳池它也停下來,並開始沒有抱怨!通過Powershell將用戶分配給IIS AppPool

我希望添加一個無效的帳戶會有效地引發錯誤,讓我測試一個帳戶的有效性。它爲什麼這樣表現?

$loginfile = "d:\temp\Logins.csv" 
$csv = Import-Csv -path $loginfile 
ForEach($line in $csv){ 

    $poolid = "MyDomain\" + $line.Login; 
    Write-Host "Assigning User to Pool:" $poolid; 

    $testpool = get-item iis:\apppools\test; 
    $testpool.processModel.userName = $poolid; 
    $testpool.processModel.password = $line.Pwd; 
    $testpool.processModel.identityType = 3; 
    $testpool | Set-Item 
    $testpool.Stop(); 
    $testpool.Start(); 
    Write-Host "IIS Recycled"; 

    $testpool = get-item iis:\apppools\test; 
    write-host "New Pool User: " $testpool.processModel.userName; 
    write-host "New Pool PWd: " $testpool.processModel.password; 
} 
+5

這個問題本身就是一個夢幻般的答案,「我如何使用powershell將用戶名和密碼分配給應用程序池」。 – sfuqua 2015-10-07 15:14:03

回答

7

在設置池身份之前,您應始終驗證您的憑據。這可以通過PrincipalContext.NET類來完成 - 具體查看PrincipalContext.ValidateCredentials(user,password)。

樣品:

#-- Make sure the proper Assembly is loaded 
[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement") | out-null 

#-- Code to check user credentials -- put in function but here are the guts 
#-- Recommend you use SecureStrings and convert where needed 
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain 
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ct,"domainname" 
$isValid = $pc.ValidateCredentials("myuser","mypassword") 

如果本地帳戶更改$ CT爲 '機' ContextType。

4

開始停止是有些用詞不當。他們應該真的被命名爲啓用禁用

池的工作進程在實際需要服務請求之前不會「啓動」。

這就是認證發生的地方。如果用戶名和密碼無效,那麼您將在系統事件日誌中得到503 Service Unavailable響應和WAS記錄的三個事件(5021,5057和5059)。

在使用API​​時,沒有對池身份的有效性進行前期檢查。只有IIS管理控制檯執行這些檢查。

相關問題