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

发布于 2024-07-20 06:53:11 字数 362 浏览 3 评论 0原文

我需要在 IIS 站点中创建一个指向网络共享 \\servername\sharename\directory 的虚拟目录,并且需要为直通身份验证指定特定用户。

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

虽然目标 IIS 环境是 IIS7(WMI 命名空间 root/WebAdministration),但我更愿意使用与 IIS6 兼容的 WMI 类(root\MicrosoftIISv2),因为脚本的其余部分已经适用于 IIS6。

我知道我可以使用 IIS7 powershell cmdlet 或 appcmd 来完成此操作,但我正在努力保持 IIS6 兼容性。

I need to create a virtual directory within an IIS Site pointing at a network share \\servername\sharename\directory and I need to specify a specific user for the Pass-through authentication.

I am after the WMI script to do this which I intend to call from a Powershell script.

Although the target IIS environment is IIS7 (WMI namespace root/WebAdministration) I would prefer to use WMI classes that are IIS6 compatible (root\MicrosoftIISv2) as the rest of the script already works against IIS6.

I know I can probably do this with the IIS7 powershell cmdlets or appcmd but I am trying to maintain the IIS6 compatibility.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷情 2024-07-27 06:53:11
## 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 *
## 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 *
相对绾红妆 2024-07-27 06:53:11

这是我想出的两个替代 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() }
}

Here are two alternative powershell functions I came up with. I would prefer the second function which only uses WMI but the Powershell WMI error defect annoyed me enough that I resorted to using the ADSI interface. Both included for reference.

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() }
}
蓝礼 2024-07-27 06:53:11

请参考以下链接以获取使用 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 

Please refer following link for getting objects using "root\WebAdministration" namespace used by IIS7 in windows 2008:
http://discovery.bmc.com/confluence/display/Configipedia/Microsoft+Internet+Information+Services

Code for checking website and app pool status on Windows 2008 server using WMI:

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 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文