【问题标题】:How to run the powershell code against each subfolder that is in the IIS root directory?如何针对 IIS 根目录中的每个子文件夹运行 powershell 代码?
【发布时间】:2013-07-08 04:48:18
【问题描述】:

我想针对 IIS 根目录中的每个子文件夹运行下面的 powershell 代码。每个子文件夹内容的输出应该是单独的 .htm 文件(仅包含该子文件夹中的文件)。如果你需要我澄清我的问题,尽管问。

$basedir = 'c:\inetpub\wwwroot'
$exp     = [regex]::Escape($basedir)
$server  = 'http://172.16.246.76'

function Create-HtmlList($fldr) {
  Get-ChildItem $fldr -Force |
    select ...
    ...
  } | Set-Content "$fldr.htm"
}

# list files in $basedir:
Create-HtmlList $basedir

# list files in all subfolders of $basedir:
Get-ChildItem $basedir -Recurse -Force |
  ? { $_.PSIsContainer } |
  % {
    Create-HtmlList $_.FullName
  }

【问题讨论】:

    标签: powershell each subdirectory


    【解决方案1】:

    您需要将文件夹遍历(递归)与文件处理(非递归)分开,例如像这样:

    $basedir = 'c:\inetpub\wwwroot'
    $exp     = [regex]::Escape($basedir)
    $server  = 'http://172.16.x.x'
    
    function Create-HtmlList($fldr) {
      Get-ChildItem $fldr -Force |
        ? { -not $_.PSIsContainer } |
        select ...
        ...
      } | Set-Content "$fldr.htm"
    }
    
    # list files in $basedir:
    Create-HtmlList $basedir
    
    # list files in all subfolders of $basedir:
    Get-ChildItem $basedir -Recurse -Force |
      ? { $_.PSIsContainer } |
      % {
        Create-HtmlList $_.FullName
      }
    

    输出文件将被放入相应的文件夹(以文件夹命名,并附加扩展名.htm)。如果您想要输出文件的名称或位置不同,则需要调整函数Create-HtmlList 中的Set-Content 行。

    【讨论】:

    • 我试图在不修改的情况下运行您的 crypt(IP 地址除外)。它给出以下错误:不允许使用空管道元素。 At H:\scrypts\GenHyperlinksInHtml_V2.ps1:9 char:6 + } | <<<< Set-Content "$fldr.htm" + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : EmptyPipeElement我不确定我必须在哪里修改代码才能正确。
    • 当然,您需要将省略号 (...) 替换为我为简洁起见省略的其余代码。请你自己做一些跑腿,好吗?
    • 我是 powershel 的新手,现在只使用了 9 天。当然,我会玩一下代码,看看我能不能让它工作。我会随着我的进展更新问题。
    • 这并不难,真的。您需要做的就是将新代码中selectSet-Content 之间的内容替换为旧代码中selectSet-Content 之间的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2014-12-06
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多