如何使用WMI的网络共享创建IIS虚拟目录

时间:2009-05-14 04:18:33

标签: iis powershell iis-6 wmi

我需要在IIS站点中创建指向网络共享 \\ servername \ sharename \ directory 的虚拟目录,我需要指定特定用户进行传递身份验证。

我正在使用WMI脚本执行此操作,我打算从Powershell脚本调用它。

虽然目标IIS环境是IIS7(WMI名称空间root / WebAdministration),但我更喜欢使用与IIS6兼容的WMI类(root \ MicrosoftIISv2),因为脚本的其余部分已经可以对IIS6起作用。

我知道我可以使用IIS7 powershell cmdlet或appcmd执行此操作,但我正在尝试维护IIS6兼容性。

4 个答案:

答案 0 :(得分:4)

## Settings
$siteName = 'Default Web Site'
$virtualDirectoryName = 'Test'
$physicalPath = '\\UNC-path'

## Init
$virtualDirectoryPath = "IIS:\Sites\$siteName\$virtualDirectoryName"

## Create Virtual Directory where physicalpath is an UNC-path (New-WebVirtualDirectory wont do)
New-Item $virtualDirectoryPath -type VirtualDirectory -physicalPath $physicalPath

## Change 'Connect As' settings (New-WebVirtualDirectory don't include Username and Password)
##userName must have the N capitalized
Set-ItemProperty $virtualDirectoryPath -Name userName -Value 'UserName'
Set-ItemProperty $virtualDirectoryPath -Name password -Value 'Password'

## Status
Get-Item -Path $virtualDirectoryPath | fl *

答案 1 :(得分:2)

答案 2 :(得分:2)

以下是我提出的两种替代PowerShell功能。我更喜欢仅使用WMI的第二个函数,但是Powershell WMI错误缺陷让我感到非常恼火,因此我使用了ADSI接口。两者都包括在内供参考

function CreateUNCVirtualDirectory(
    [string]$siteName = $(throw "Must provide a Site Name"),
    [string]$vDirName = $(throw "Must provide a Virtual Directory Name"),
    [string]$uncPath = $(throw "Must provide a UNC path"),
    [string]$uncUserName = $(throw "Must provide a UserName"),
    [string]$uncPassword = $(throw "Must provide a password")
    ) {

    $iisWebSite = Get-WmiObject -Namespace 'root\MicrosoftIISv2' -Class IISWebServerSetting -Filter "ServerComment = '$siteName'"

    $objIIS = new-object System.DirectoryServices.DirectoryEntry("IIS://localhost/" + $iisWebSite.Name + "/Root")
    $children = $objIIS.psbase.children
    $vDir = $children.add($vDirName,$objIIS.psbase.SchemaClassName)
    $vDir.psbase.CommitChanges()
    $vDir.Path = $uncPath
    $vDir.UNCUserName = $uncUserName
    $vDir.UNCPassword = $uncPassword
    $vDir.psbase.CommitChanges()
}

function CreateUNCVirtualDirectory2(
    [string]$siteName = $(throw "Must provide a Site Name"),
    [string]$vDirName = $(throw "Must provide a Virtual Directory Name"),
    [string]$uncPath = $(throw "Must provide a UNC path"),
    [string]$uncUserName = $(throw "Must provide a UserName"),
    [string]$uncPassword = $(throw "Must provide a password")
    ) {

    $iisWebSite = Get-WmiObject -Namespace 'root\MicrosoftIISv2' -Class IISWebServerSetting -Filter "ServerComment = '$siteName'"

    $virtualDirSettings = [wmiclass] "root\MicrosoftIISv2:IIsWebVirtualDirSetting"
    $newVDir = $virtualDirSettings.CreateInstance()
    $newVDir.Name = ($iisWebSite.Name + '/ROOT/' + $vDirName)
    $newVDir.Path = $uncPath
    $newVDir.UNCUserName = $uncUserName
    $newVDir.UNCPassword = $uncPassword

    # Call GetType() first so that Put does not fail.
    # http://blogs.msdn.com/powershell/archive/2008/08/12/some-wmi-instances-can-have-their-first-method-call-fail-and-get-member-not-work-in-powershell-v1.aspx
    Write-Warning 'Ignore one error message:Exception calling "GetType" with "0" argument(s): "You cannot call a method on a null-valued expression."'
    $newPool.GetType()

    $newVDir.Put();
    if (!$?) { $newVDir.Put() }
}

答案 3 :(得分:1)

请参阅以下链接,了解使用Windows 2008中IIS7使用的“root \ WebAdministration”命名空间获取对象: http://discovery.bmc.com/confluence/display/Configipedia/Microsoft+Internet+Information+Services

使用WMI检查Windows 2008服务器上的网站和应用程序池状态的代码:

Public Sub WebsiteAppPoolStatusCheckIISv7(ByVal computer As String, ByVal userName As String, ByVal password As String)
    Dim thisServer = System.Configuration.ConfigurationManager.AppSettings("ThisServer")
    Dim excludedWebSiteOrAppPool = System.Configuration.ConfigurationManager.AppSettings("ExcludedWebSiteOrAppPool")
    Dim WbemAuthenticationLevelPktPrivacy = 6
    Dim objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Dim objWMIService As Object
    If (computer = thisServer) Then
        objWMIService = objSWbemLocator.ConnectServer(computer, "root/WebAdministration")
    Else 'for remote servers
        objSWbemLocator.Security_.AuthenticationLevel = WbemAuthenticationLevelPktPrivacy
        objWMIService = objSWbemLocator.ConnectServer(computer, "root/WebAdministration", userName, password)
    End If
    'Code Start for Website status check
    Dim websites = objWMIService.ExecQuery("SELECT * FROM Site")
    For Each website As WbemScripting.SWbemObject In websites
        Dim WebSiteName = website.Name
        Dim webSiteStatus As String
    If (Convert.IsDBNull(website.GetState)) Then
            webSiteStatus = "Unknown"
        Else
            Select Case website.GetState
                Case 0
                    webSiteStatus = "Starting"
                Case 1
                    webSiteStatus = "Running"
                Case 2
                    webSiteStatus = "Stopping"
                Case 3
                    webSiteStatus = "Stopped"
                Case Else
                    webSiteStatus = "Unknown"
            End Select
        End If
    logFile.writeline("Server:= " & computer & ", WebSiteName:= " & WebSiteName & ", Status:= " & webSiteStatus)
    Next
    'Code Start for App pool status check
    Dim appPools As WbemScripting.SWbemObjectSet
    appPools = objWMIService.ExecQuery("Select * from ApplicationPool")
    'Iterate all the appPools of the server
    For Each appPool As WbemScripting.SWbemObject In appPools
        Dim appPoolName = appPool.Name
        Dim appPoolStatus As String
        If (Convert.IsDBNull(appPool.GetState)) Then
            appPoolStatus = "Unknown"
        Else
            Select Case appPool.GetState
                Case 0
                    appPoolStatus = "Starting"
                Case 1
                    appPoolStatus = "Running"
                Case 2
                    appPoolStatus = "Stopping"
                Case 3
                    appPoolStatus = "Stopped"
                Case Else
                    appPoolStatus = "Unknown"
            End Select
        End If
    logFile.writeline("Server:= " & computer & ", AppPoolName:= " & appPoolName & ", Status:= " & appPoolStatus)
    Next
End Sub