开发者

Powershell script to create folder, website and deploy using MSDeploy.exe

I would like to deploy a web application on Windows 2008 R2. I know the separate PowerShell commands to do various tasks. But I would like to put this into a nice PowerShell script.

I just need the syntax, can you please help me to do the following actions:

  1. Test if C:\Inetpub\MyWebsite folder exists, if not, create it.

  2. Test in IIS7 if MyWebsite exists, if not create it (I know how to Import-Module WebAdministration and call New-WebSite)

  3. Now the complicated part. I deploy a Web site from a package prepared by Visua开发者_Python百科l Studio 2010. VS supplies a .cmd file where I just need to execute it from a DOS prompt. This means I have to leave the PS Console, open a DOS Console to run that cmd file. Is it possible to run a .cmd file from within a PowerShell console ?


To answer your questions:

Import-Module WebAdministration

# Check for physical path
$sitePath = "c:\inetpub\MyWebsite"
if (-not (Test-Path -path $sitePath))
{
    New-Item -Path $sitePath -type directory 
}

# Check for site
$siteName = "MyWebSite"
$site = Get-WebSite | where { $_.Name -eq $siteName }
if($site -eq $null)
{
    Write-Host "Creating site: $siteName"
    # Put your New-WebSite code here
}

# Execute your .cmd here
c:\PathToScript\MakeMySite.cmd

You can run .cmd scripts from within PowerShell just fine.


I have also changed a little bit. Using the same Test syntax to test if a website exists or not:

if (-not (Test-Path -path IIS:\Sites\$SiteName))
{
   New-WebSite -Name $SiteName ...etc...
}

Also for executing the *.cmd file, I lift some code from the web and saw that people use & to execute external command. Hope that you are OK:

& c:\PathToScript\MakeMySite.cmd arg1 arg2

Thank you very much for your help.


If you need to run the .cmd file as administrator, you can use the following code:

    Start-Process -FilePath C:\PathToScript\MakeMySite.cmd -Verb RunAs -ArgumentList "/y"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜