参考にしたサイトは、こちら こんなことできるんだ!
Building a web server with PowerShell | 4sysops
ほぼそのままのスクリプトに、ちょっとだけ改造させていただき、日本語の文字化け対応を行ったバージョン
このスクリプトを実行し、ブラウザから http://localhost:8080/ にアクセスするとスクリプトを格納しているフォルダー内のファイル一覧を取得することができる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
function Get-DirectoryContent { <# .SYNOPSIS Function to get directory content .EXAMPLE Get-DirectoryContent -Path "C:\" -HeaderName "poshserver.net" -RequestURL "http://poshserver.net" -SubfolderName "/" #> [CmdletBinding(SupportsShouldProcess = $true)] param ( [Parameter( Mandatory = $true, HelpMessage = 'Directory Path')] [string]$Path, [Parameter( Mandatory = $false, HelpMessage = 'Header Name')] [string]$HeaderName, [Parameter( Mandatory = $false, HelpMessage = 'Request URL')] [string]$RequestURL, [Parameter( Mandatory = $false, HelpMessage = 'Subfolder Name')] [string]$SubfolderName, [string]$Root ) @" <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>$($HeaderName)</title> </head> <body> <h1>$($HeaderName) - $($SubfolderName)</h1> <hr> "@ @" <a href="./../">[To Parent Directory]</a><br><br> <table cellpadding="5"> "@ $Files = (Get-ChildItem "$Path") foreach ($File in $Files) { $FileURL = ($File.FullName -replace [regex]::Escape($Root), "" ) -replace "\\", "/" if (!$File.Length) { $FileLength = "[dir]" } else { $FileLength = $File.Length } @" <tr> <td align="right">$($File.LastWriteTime)</td> <td align="right">$($FileLength)</td> <td align="left"><a href="$($FileURL)">$($File.Name)</a></td> </tr> "@ } @" </table> <hr> </body> </html> "@ Write-Host $File.Name } [System.Reflection.Assembly]::LoadWithPartialName("System.Web") $listener = New-Object System.Net.HttpListener $listener.Prefixes.Add("http://localhost:8080/") $listener.AuthenticationSchemes = [System.Net.AuthenticationSchemes]::IntegratedWindowsAuthentication $listener.Start() New-PSDrive -Name FileServe -PSProvider FileSystem -Root $PWD.Path $Root = $PWD.Path cd FileServe:\ do { $context = $listener.GetContext() $requestUrl = $context.Request.Url $response = $context.Response $context.User.Identity.Impersonate() Write-Host "> $requestUrl" $Content = "" $localPath = $requestUrl.LocalPath try { $RequestedItem = Get-Item -LiteralPath "FileServe:\$localPath" -Force -ErrorAction Stop $FullPath = $RequestedItem.FullName if ($RequestedItem.Attributes -match "Directory") { $Content = Get-DirectoryContent -Path $FullPath -HeaderName "PowerShell FileServer" -RequestURL "http://localhost:8080" -SubfolderName $localPath -Root $Root $Encoding = [system.Text.Encoding]::UTF8 $Content = $Encoding.GetBytes($Content) $response.ContentType = "text/html" } else { $Content = [System.IO.File]::ReadAllBytes($FullPath) $response.ContentType = [System.Web.MimeMapping]::GetMimeMapping($FullPath) } } catch [System.UnauthorizedAccessException] { Write-Host "Access Denied" Write-Host "Current user: $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)" Write-Host "Requested File: FileServe:\$localPath" $response.StatusCode = 404 $Content = [System.Text.Encoding]::UTF8.GetBytes("<h1>404 - Page not found</h1>") } catch [System.Management.Automation.ItemNotFoundException] { Write-Host "No route found for: FileServe:\$localPath" $response.StatusCode = 404 $Content = [System.Text.Encoding]::UTF8.GetBytes("<h1>404 - Page not found</h1>") } catch { $_ $Content = "$($_.InvocationInfo.MyCommand.Name) : $($_.Exception.Message)" $Content += "$($_.InvocationInfo.PositionMessage)" $Content += " + $($_.CategoryInfo.GetMessage())" $Content += " + $($_.FullyQualifiedErrorId)" $Content = [System.Text.Encoding]::UTF8.GetBytes($Content) $response.StatusCode = 500 } $response.ContentLength64 = $Content.Length $response.OutputStream.Write($Content, 0, $Content.Length) $response.Close() $responseStatus = $response.StatusCode Write-Host "< $responseStatus" } while ($listener.IsListening) |
コメント