通过c#

进行IIS7远程管理 我一直在研究Microsoft.Web.Administration.dll和ServerManager类,试图控制我们的Windows Server 2008 IIS7实例。 我已启用远程管理,可以通过IIS远程管理工具进行连接,但是当我尝试使用以下内容时,我无法连接:
ServerManager.OpenRemote(serverName);
这个类不允许我在远程IIS7服务器上指定用户名和密码,就像IIS远程管理工具那样。 这都是通过我们的构建过程使用NAnt调用的。 其他人如何控制他们的远程IIS7服务器作为其CI设置的一部分?     
已邀请:
您需要在具有更改配置文件的权限的域用户(Active Directory用户)下运行该应用程序。 Windows身份验证将完成剩下的工作。     
正如Oded所说,您需要Active Directory才能使用
ServerManager
打开与远程服务器的连接。 假设您有管理员RDP访问服务器,那么您可以在构建脚本中使用WinRM和Remote PowerShell(最适合使用最新版WinRM的PowerShell 2.0):   Windows远程管理命令行工具(Winrm.cmd) 要为两台不在域中的计算机快速配置WinRM: 客户: winrm quickconfig(只说是) winrm set winrm / config / Client / Auth'@ {Basic =“true”}' ::如果不使用HTTPS,则仅执行下一行 winrm set winrm / config / Client'@ {AllowUnencrypted =“true”}' winrm set winrm / config / Client'@ {TrustedHosts =“hostname_or_ip”}' 服务器: winrm quickconfig(只说是) winrm set winrm / config / Service / Auth'@ {Basic =“true”}' ::请参阅:http://support.microsoft.com/kb/2019527关于https winrm quickconfig -transport:https ::如果不使用HTTPS,则只执行此操作并且您对发送凭据感到满意 ::明文。 winrm set winrm / config / Service'@ {AllowUnencrypted =“true”}' 现在有一些警告。 WinRM将在Windows防火墙中为侦听器打开端口5985和5986(如果运行Windows 2003,它将使用端口80和443)。这可能不符合您的喜好,您可能最好与您的网络管理员讨论如何确保这一点。 配置WinRM后,您将需要在作为管理员组成员的远程服务器上配置的用户帐户。完成后,您可以进行测试。在构建服务器上:
# the following line will prompt for a username and password, enter the name of the account
# you just configured on the IIS box
$cred = Get-Credential

# next test the connection
Test-WSMan -ComputerName <server_name_or_ip> -Authentication default `
           -Credential $cred
如果一切顺利,您应该看到以下响应: wsmid:http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.x                   SD ProtocolVersion:http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd ProductVendor:Microsoft Corporation ProductVersion:OS:6.1.7600 SP:0.0 Stack:2.0 接下来是连接到远程PowerShell会话:
Enter-PSSession <server_name_or_ip> -Authentication default -Credential $cred
如果成功,您应该在远程计算机上有PowerShell提示符。 然后,您可以使用Remote PowerShell加载适用于PowerShell的WebAdministration Provider,并将IIS的许多方面操作到您的内容中:   Windows PowerShell的Web管理(IIS)提供程序 要连接到远程服务器,您需要提供一个
PSCredential
对象。如上所述,您将使用以下方式提供:
$cred = Get-Credential
但是,这总是要求键盘进行一些交互以提供用户名和密码。显然这对自动化CI没有好处。 但是,您可以将密码存储在文件中。为此,只需运行以下一次(或密码更改时):
read-host -assecurestring | convertfrom-securestring | out-file C:securestring.txt
然后,当您需要创建
PSCredential
以对远程服务器进行身份验证时:
$username = "deployment_user"
$password = cat C:securestring.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password

$serverNameOrIp = "192.168.1.1"
Enter-PSSession $serverNameOrIp -Authentication default -Credential $cred
上面的脚本来自以下博客条目,但我已经复制以保留这里,以防文章变暗:   在没有提示的情况下使用PSCredentials - GeeksWithBlogs 无论如何,一旦您连接到远程服务器,您可以发出更多命令,例如:
Import-Module WebAdministration
CD IIS:Sites
等等。 如果这台机器面向互联网并且唯一的访问方式是通过互联网,则应谨慎处理上述大部分内容。如果是这种情况,请考虑将WinRM端口仅限制为VPN。     
我最后编写了一个WCF服务,它作为服务在远程计算机上运行。该服务在具有管理员权限的本地帐户下运行,以便可以更改该计算机上的本地IIS实例。 从我的NAnt脚本,我有一系列自定义任务,它们与WCF服务进行通信,并根据需要更改IIS设置。 由于这是一个内部开发环境,我不太关心安全性,我允许的IIS的实际更改是非常基本的。     

要回复问题请先登录注册