BIB
BIB

Reputation: 121

Creating application pool in Powershell with force option

I try to create application pool on the remote computer and if it exists already command fails:

Invoke-Command -ComputerName $ServerName -ScriptBlock {Import-Module WebAdministration;new-WebAppPool $($args[0]) -Force} -ArgumentList $proj

Filename: Error: Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'BIB ' + CategoryInfo : InvalidData: (:) [New-WebAppPool], COMException + FullyQualifiedErrorId : Filename: Error: Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'BIB ' ,Microsoft.IIs.PowerShell.Provider.NewAppPoolCommand

So, how can I force creating pool if it exists already? Or how can I previously check if it exists?

Upvotes: 1

Views: 2660

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174835

Test if the App Pool already exists, if yes, then remove it:

$AppPoolScriptBlock = {
    Import-Module WebAdministration
    if(Test-Path IIS:\AppPools\$($args[0]))
    {
        Remove-WebAppPool $args[0]
    }
    New-WebAppPool $($args[0]) -Force
}

Invoke-Command -ComputerName $ServerName -ScriptBlock $AppPoolScriptBlock -ArgumentList $proj

Upvotes: 4

Related Questions