利用PowerShell快速创建基于IIS的虚拟目录浏览环境

利用PowerShell快速创建基于IIS的虚拟目录浏览环境

一、使用背景

在网页类应用开发中,常遇到的一个问题就是发布的版本需要在http或https协议下才能正常浏览完整功能,对于开发人员来说,这个功能往往由IDE集成于其中,可以轻松地快速查看完整版本,但是将编译的文件包发给协同人员检查时,对方确未必会有编译环境,这个时候我们通常有两种解决方案:

  1. 用netbox之类绿色轻量的Web应用搭建工具建一个临时的本地服务器,将其放到文件包的根目录以便服务启动便可直接打开首页。
  2. 搭建一个中间服务器,专门用于放置项目预览包,这样做的好处是有一个固定的浏览地址可供查看,但每次需要开发人员将制作好的文件包传至中间服务器并告诉协同人员具体的访问路径(路径深度和名称不一样)。

今天我们尝试用一种新方式来实现本地快速浏览完整功能,它的原理也是基于上述第二种方案,不过使用脚本自动化的方式快速完成本地服务器的配置,而且也不需要往文件包中拷贝第三方应用程序便可实现此功能。

二、准备工作

2.1 测试环境:

  • Windows操作系统电脑(Windows 10或以上版本)

2.2 系统组件需求:

  • IIS(Internet Information Services)组件,安装基本应用功能及脚本管理功能
    Win10以上操作系统可随时通过控制面板中的『程序和功能 ⇒ 启用或关闭Windows功能』去安装或卸载相应组件,而且此操作只需要配置一次即可长期使用,因此相对适配成本较低。
  • WebAdministration模块
    此为Powershell中的网站服务管理模块,一般自带,如无此模块,可用Import-Module WebAdministration来安装它。

三、操作步骤

3.1 手动操作步骤

1️⃣ 网站上右键单击,选择添加虚拟目录
请添加图片描述请添加图片描述

2️⃣ 然后在添加的虚拟目录上右键,将其转化为应用程序
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

3️⃣ 此时便可在浏览器通过本地服务地址来访问此虚拟目录了。
请添加图片描述

3.2 自动化脚本实现

通过以上步骤可推测,WebAdministration模块中应有相应操作方法来达成同样目的。
看一下WebAdministration模块下面有哪些功能可供调用:
Get-Module WebAdministration | %{ $_.ExportedCommands }

Key                                  Value
---                                  -----
Add-WebConfiguration                 Add-WebConfiguration
Add-WebConfigurationLock             Add-WebConfigurationLock
Add-WebConfigurationProperty         Add-WebConfigurationProperty
Backup-WebConfiguration              Backup-WebConfiguration
Clear-WebCentralCertProvider         Clear-WebCentralCertProvider
Clear-WebConfiguration               Clear-WebConfiguration
Clear-WebRequestTracingSetting       Clear-WebRequestTracingSetting
Clear-WebRequestTracingSettings      Clear-WebRequestTracingSettings
ConvertTo-WebApplication             ConvertTo-WebApplication
Disable-WebCentralCertProvider       Disable-WebCentralCertProvider
Disable-WebGlobalModule              Disable-WebGlobalModule
Disable-WebRequestTracing            Disable-WebRequestTracing
Enable-WebCentralCertProvider        Enable-WebCentralCertProvider
Enable-WebGlobalModule               Enable-WebGlobalModule
Enable-WebRequestTracing             Enable-WebRequestTracing
Get-WebAppDomain                     Get-WebAppDomain
Get-WebApplication                   Get-WebApplication
Get-WebAppPoolState                  Get-WebAppPoolState
Get-WebBinding                       Get-WebBinding
Get-WebCentralCertProvider           Get-WebCentralCertProvider
Get-WebConfigFile                    Get-WebConfigFile
Get-WebConfiguration                 Get-WebConfiguration
Get-WebConfigurationBackup           Get-WebConfigurationBackup
Get-WebConfigurationLocation         Get-WebConfigurationLocation
Get-WebConfigurationLock             Get-WebConfigurationLock
Get-WebConfigurationProperty         Get-WebConfigurationProperty
Get-WebFilePath                      Get-WebFilePath
Get-WebGlobalModule                  Get-WebGlobalModule
Get-WebHandler                       Get-WebHandler
Get-WebItemState                     Get-WebItemState
Get-WebManagedModule                 Get-WebManagedModule
Get-WebRequest                       Get-WebRequest
Get-Website                          Get-Website
Get-WebsiteState                     Get-WebsiteState
Get-WebURL                           Get-WebURL
Get-WebVirtualDirectory              Get-WebVirtualDirectory
New-WebApplication                   New-WebApplication
New-WebAppPool                       New-WebAppPool
New-WebBinding                       New-WebBinding
New-WebFtpSite                       New-WebFtpSite
New-WebGlobalModule                  New-WebGlobalModule
New-WebHandler                       New-WebHandler
New-WebManagedModule                 New-WebManagedModule
New-Website                          New-Website
New-WebVirtualDirectory              New-WebVirtualDirectory
Remove-WebApplication                Remove-WebApplication
Remove-WebAppPool                    Remove-WebAppPool
Remove-WebBinding                    Remove-WebBinding
Remove-WebConfigurationBackup        Remove-WebConfigurationBackup
Remove-WebConfigurationLocation      Remove-WebConfigurationLocation
Remove-WebConfigurationLock          Remove-WebConfigurationLock
Remove-WebConfigurationProperty      Remove-WebConfigurationProperty
Remove-WebGlobalModule               Remove-WebGlobalModule
Remove-WebHandler                    Remove-WebHandler
Remove-WebManagedModule              Remove-WebManagedModule
Remove-Website                       Remove-Website
Remove-WebVirtualDirectory           Remove-WebVirtualDirectory
Rename-WebConfigurationLocation      Rename-WebConfigurationLocation
Restart-WebAppPool                   Restart-WebAppPool
Restart-WebItem                      Restart-WebItem
Restore-WebConfiguration             Restore-WebConfiguration
Select-WebConfiguration              Select-WebConfiguration
Set-WebBinding                       Set-WebBinding
Set-WebCentralCertProvider           Set-WebCentralCertProvider
Set-WebCentralCertProviderCredential Set-WebCentralCertProviderCredential
Set-WebConfiguration                 Set-WebConfiguration
Set-WebConfigurationProperty         Set-WebConfigurationProperty
Set-WebGlobalModule                  Set-WebGlobalModule
Set-WebHandler                       Set-WebHandler
Set-WebManagedModule                 Set-WebManagedModule
Start-WebAppPool                     Start-WebAppPool
Start-WebCommitDelay                 Start-WebCommitDelay
Start-WebItem                        Start-WebItem
Start-Website                        Start-Website
Stop-WebAppPool                      Stop-WebAppPool
Stop-WebCommitDelay                  Stop-WebCommitDelay
Stop-WebItem                         Stop-WebItem
Stop-Website                         Stop-Website

可以看到,其中与操作相关的对象有:

***-WebAppPool
***-WebVirtualDirectory
***-WebApplication

分别查下它们的详细用法,经过测试,初步达成以下内容:
CreateVirDirOnIIS.ps1

Param (
	[string]$InputPath
)

function pause() {
	Write-Host "Press any key to continue..."
	[Console]::ReadKey() | Out-Null
}

function main() {
	Write-Host "Step 1: Detecting if already exist virtual directory"
	$oVD = Get-WebVirtualDirectory -Site "Default Web Site" -Name tmp
	$oVD
	if ($oVD -ne $null) {
		Remove-WebApplication -Site "Default Web Site" -Name tmp
		#Remove-WebVirtualDirectory -Site "Default Web Site" -Application DefaultAppPool -Name tmp
	}
	#pause

	Write-Host "`n`n"
	Write-Host "Step 2: New a virtual directory named tmp"
	New-WebVirtualDirectory -Site "Default Web Site" -Name tmp -PhysicalPath "$PWD" -Force
	Write-Host "`tAnd convert it to web application"
	ConvertTo-WebApplication -Application DefaultAppPool -PSPath "IIS:\Sites\Default Web Site\tmp" -Force
	Sleep -Milliseconds 3000
	#pause

	Write-Host "`n`n"
	Write-Host "Step 3: Get virtual directory named tmp"
	$oVD = Get-WebVirtualDirectory -Site "Default Web Site" -Name tmp
	$oVD
	#pause

	Write-Host "`n`n"
	Write-Host "Step 4: Get binded port of http protocol"
	$oBinding = Get-WebBinding -Name "Default Web Site" -Protocol http
	$nPort = 80
	if ($oBinding.bindingInformation -match "/.*:(\d+):/") {
		$nPort = $matches[1]
	}
	Write-Host "Current site's binding port is: $nPort"
	#pause

	Write-Host "`n`n"
	Write-Host "Step 5: Start localhost virtual directory page"
	start http://localhost:${nPort}/tmp
}

$strPath = ""
if ("$InputPath" -eq "") {
	$strPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
} else {
	if (Test-Path "$InputPath" -PathType Container) {
		$strPath = "$InputPath"
	} else {
		$strPath = Split-Path -Parent "$InputPath"
	}
}
Write-Host "Current File Path: $strPath"
pushd $strPath

main
#pause

四、后续

由于WebAdministration模块属于系统配置性质的模块,因此它的运行对权限有要求,调用环境需要在拥有管理员控制权限的会话中才能被正常调用,因此使用它需要右键以管理员的身份运行。
创建虚拟目录指向的物理路径源自于脚本调用时传入的$InputPath参数,否则就将虚拟目录指向路径设置为脚本所在路径,所以要快速调用它,要么将脚本拷至导出的网页文件根目录下,要么先开启具有管理员权限的会话环境,然后在里面调用此ps脚本并传入$InputPath参数(由于只接收一个参数,可在脚本名后直接传入需要创建虚拟目录的路径名,不用显式指定$InputPath字段)。

😵似乎变得更麻烦了……

好在,到这里基本功能已经完成了,只需要将管理员身份运行和路径传入这两个搞定就行了!

4.1 快捷配置

将上述的CreateVirDirOnIIS.ps1脚本放入到系统的运行目录中,如C:\Windows
然后在该目录下建一个批处理,用来实现以管理员身份运行和传入路径这两件事:
IISThis.bat

@echo off

set "strPath="
if "%~1" EQU "" (
	set "strPath=%cd:"=%"
) else (
	set "strPath=%~1"
)

powershell -Command "Set-ExecutionPolicy -Scope Process Bypass; Start powershell -Verb runas -Args 'CreateVirDirOnIIS.ps1 ""%strPath%""'"

这样,在任意文件夹地址栏输入cmd回车,然后在其中输入iisthis即可快速配置基于此文件夹的名为tmp的虚拟目录,并自动在地址栏打开此地址了。
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

4.2 再省点操作

直接在文件夹地址栏输入iisthis回车,或将此命令加入注册表中,在任意文件夹上单击右键,选择IISThis即可快速浏览。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 24
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值