Dumi
Dumi

Reputation: 1434

Create IIS website on remote server using PowerShell

I'm trying to create IIS website on a remote server from another server using powershell. When I execute it, the site has created in local server, not in the remote server. This is the powershell function. It is in function.ps1 file.

function CreateIISWebsite
{
 param (
        [string]$iisAppName,
        [string]$directoryPath,
        [string]$iisAppPoolName,
        [string]$rhost,
        [string]$un,
        [string]$pw
    )

$MSDeployExe = "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe"
Import-Module WebAdministration
$iisAppPoolDotNetVersion = "v4.0"

#navigate to the app pools root
cd IIS:\AppPools\

#check if the app pool exists
if (Test-Path $iisAppPoolName -pathType container)
{
    Remove-Item $iisAppPoolName -recurse   
}

#create the app pool
$appPool = New-Item $iisAppPoolName
$appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion

Set-ItemProperty IIS:\AppPools\$iisAppPoolName managedRuntimeVersion v4.0

Set-ItemProperty -Path IIS:\AppPools\$iisAppPoolName -Name processmodel.identityType -Value 3
Set-ItemProperty -Path IIS:\AppPools\$iisAppPoolName -Name processmodel.username -Value $un
Set-ItemProperty -Path IIS:\AppPools\$iisAppPoolName -Name processmodel.password -Value $pw


#navigate to the sites root
cd IIS:\Sites\

#check if the site exists
if (Test-Path $iisAppName -pathType container)
{
Remove-Item $iisAppName -recurse
}

#create the site
$iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=":80:" +     $iisAppName} -physicalPath $directoryPath
$iisApp | Set-ItemProperty -Name "applicationPool" -Value $iisAppPoolName

}

I call this function like this.

. ./function.ps1 CreateIISWebsite -iisAppName $sitename -directoryPath $path -iisAppPoolName $appPool -rhost $rhost -un $un -pw $pw

Even though i pass ip of the remote server as rhost i have no idea where i need to use it. So IIS site is creating in local successfully. Without using rhost parameter it won't create in server. So I need to use that parameter in correct place in the code. I have installed Web Deploy in both servers. Please suggest me a solution.

Upvotes: 3

Views: 6448

Answers (1)

user189198
user189198

Reputation:

Configure PowerShell Remoting on the local and remote systems (use Enable-PSRemoting -Force, and then deploy the script to the remote computer by using Invoke-Command.

UPDATE

e.g. On your local and remote systems write this in an elevated Powershell Command Prompt:

Enable-PSRemoting -Force

Remember also to Set-ExecutionPolicy (See here for more info) to something appropriate on the remote server if you have not already done so.

To invoke the powershell script on the remote machine from your local machine you can then do something like this:

Invoke-Command -ComputerName server01 -File c:\path\to\script.ps1

Upvotes: 2

Related Questions