2

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 Visual 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 ?

1
  • any full source code sample ? Commented Jun 8, 2012 at 9:48

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, This is awesome. I could build around this to complete my PS script.
2

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.

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.