Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iis 7 通过Powershell将用户分配到IIS应用池_Iis 7_Powershell - Fatal编程技术网

Iis 7 通过Powershell将用户分配到IIS应用池

Iis 7 通过Powershell将用户分配到IIS应用池,iis-7,powershell,Iis 7,Powershell,我尝试了这个代码,它似乎工作得很好。但是,我注意到,如果您将用户名和密码分配给一个不存在的帐户,代码将继续运行,不会出现问题。此外,如果您分配了一个无效的帐户并调用stop()和start(),IIS池确实会停止和启动!!此外,当我转到InetMgr并启动、停止或重新引用池时,它也会停止和启动,没有任何抱怨 我希望添加一个无效的帐户将抛出一个错误,有效地允许我测试帐户的有效性。为什么会这样 $loginfile = "d:\temp\Logins.csv" $csv = Import-Csv -

我尝试了这个代码,它似乎工作得很好。但是,我注意到,如果您将用户名和密码分配给一个不存在的帐户,代码将继续运行,不会出现问题。此外,如果您分配了一个无效的帐户并调用stop()和start(),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;
}

开始停止有点用词不当。它们应该真正命名为启用禁用

在需要为请求提供服务之前,池的工作进程实际上不会“启动”

就在这一点上进行身份验证。如果用户名和密码无效,则您将得到503服务不可用响应和WAS在系统事件日志中记录的三个事件(5021、5057和5059)


使用API时,不需要预先检查池标识的有效性。只有IIS管理控制台执行这些检查。

在设置池标识之前,您应该始终验证凭据。这可以通过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。

对于“如何使用powershell为应用程序池分配用户名和密码”这一问题,这个问题本身就是一个极好的答案