简体   繁体   English

如何对IIS根目录中的每个子文件夹运行powershell代码?

[英]How to run the powershell code against each subfolder that is in the IIS root directory?

I would like to run the powershell code below, against each sub-folder that is in the IIS root directory. 我想针对IIS根目录中的每个子文件夹运行下面的powershell代码。 The output should be separate .htm file for each sub-folder contents (containing only the files in that sub-folder). 对于每个子文件夹内容(仅包含该子文件夹中的文件),输出应该是单独的.htm文件。 If you need me to clarify my question, just ask. 如果您需要我澄清我的问题,请问。

$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
  }

You need to separate folder-traversal (recursive) from file processing (non-recursive), eg like this: 您需要将文件夹遍历(递归)与文件处理(非递归)分开,例如:

$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
  }

The output files will be put into the respective folder (named after the folder with the extension .htm appended). 输出文件将放入相应的文件夹(以附加扩展名.htm的文件夹命名)。 If you want a different name or location for the output files, you need to adjust the Set-Content line in the function Create-HtmlList . 如果要为输出文件使用不同的名称或位置,则需要在Create-HtmlList函数中调整Set-Content行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM