簡體   English   中英

通過Powershell將用戶分配給IIS AppPool

[英]Assign User to IIS AppPool via Powershell

我嘗試了這個代碼,似乎工作正常。 但是,我注意到如果您將用戶名和密碼分配給不存在的帳戶,代碼將繼續存在而不會出現問題。 此外,如果您分配一個無效的帳戶並調用stop()然后啟動(),IIS池確實會停止並啟動!! 此外,當我去InetMgr並啟動,停止或重新啟動游泳池時,它也停止並開始而沒有抱怨!

我希望添加無效帳戶會導致錯誤,從而有效地允許我測試帳戶的有效性。 為什么它會這樣?

$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;
}

在設置池標識之前,應始終驗證憑據。 這可以通過PrincipalContext .NET類完成 - 具體來看PrincipalContext.ValidateCredentials(用戶,密碼)。

樣品:

#-- 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更改為'Machine'ContextType。

開始停止是用詞不當。 它們應該被命名為EnableDisable

在需要為請求提供服務之前,池的工作進程實際上不會“啟動”。

就在那時,身份驗證就會發生。 如果用戶名和密碼無效,那么您將在系統事件日志中獲得WAS記錄的503 Service Unavailable響應和三個事件(5021,5057和5059)。

使用API​​時,沒有預先檢查池身份的有效性。 只有IIS管理控制台執行這些檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM