【问题标题】:Powershell list of all websites in IIS with a condition and change an attribute like physical pathIIS中所有网站的Powershell列表,具有条件并更改物理路径等属性
【发布时间】:2016-03-22 22:22:37
【问题描述】:

我想(在 Powershell 中)列出 IIS 中名称包含关键字“UK”的所有网站。然后,我想将返回站点的物理路径替换为另一个,例如“C:\Sites\Sitex”。

我在这里做错了什么?

到目前为止,我已经尝试过: get-website | select name,physicalpath | where { $_.name -like "*UK*" } 它可以很好地显示我的 IIS 中名称包含关键字“UK”的所有站点。 但是,当我这样做时: get-website | select physicalpath | where { $_.name -like "*UK*" } 它什么都不显示!

我想这可能是因为我的“where”子句中没有我的选择中不存在的字段! (虽然在 SQL 中这会起作用)。所以,我会尝试将初始查询包装到一个表中,然后从该表中只选择一列。 Powershell 中是否存在这样的嵌套查询?请帮助语法。 也欢迎其他解决方案。

然后我会做类似的事情

foreach ($sites in $sites) {

Set-ItemProperty ???? -name 物理路径 -value "C:\Sites\Sitex"

}

请在语法方面提供任何帮助,以及在 Set-ItemProperty 之后应该写什么参数。 非常感谢!

================= 同时尝试这样的事情:

Import-Module WebAdministration

dir IIS:\Sites # lists all sites

dir IIS:\Sites | ForEach-Object {

    # web site name $_Name 


    if($_.Name -like "*UK*") { 
    $_.Name 
    $_.PhysicalPath = "C:\Inetpub"

    }


}

没有错误,它似乎也不起作用。 if($_.Name -like "*UK*") 的行为与预期不符,因为它会输出名称中不包含 UK 的其他站点。 顺便说一句,我正在使用 Powershell_ISE.exe。

【问题讨论】:

    标签: asp.net powershell iis


    【解决方案1】:

    我没有为此答案导入 WebAdministration 模块。

    我使用 iis manager ui 的配置代码生成功能来生成 foreach 循环中使用的语句(参考答案后面的链接),脚本的其余部分是我自定义的。

    $searchWildCard="*UK*"
    $targetDirectory="<INSERT_NEW_DIRECTORY_PATH_HERE>"
    
     #Select the entire site object since i might make other config changes too.
     $sites=get-website | Where-Object { $_.name -like $searchWildCard }
    
    foreach ($site in $sites) {
    $filter=[string]::Format("system.applicationHost/sites/site[@name='{0}']/application[@path='/']/virtualDirectory[@path='/']",$site.name)
    
        # note the backticks below
        Set-WebConfigurationProperty `
        -pspath 'MACHINE/WEBROOT/APPHOST' `
        -filter $filter `
        -name "physicalPath" `
        -value $targetDirectory
    }
    
    # I tested without performing an iisreset. 
    # One definitely needs to refresh the
    #iis manager ui after running the script if it was open during execution.
    
    #iisreset
    

    脚本注释:

    脚本中的 cmets 指出了具体的注意事项。

    脚本将所有站点指向与您的 foreach 相同的目录 例子。

    测试日期:

    Windows 10

    Powershell 5

    SO 参考:

    code generation using iis management ui

    SO 相关:

    changing-the-physical-path-of-an-iis-website-on-a-remote-machine-via-powershell

    【讨论】:

      猜你喜欢
      • 2011-04-25
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多