Search code examples
powershelliis-7.5

Copy running IIS App Pool with Powershell WebAdministration


I am trying to copy an existing IIS 7.5 app pool using the Powershell WebAdministration module without stopping the application.

When I copy the app pool, with an application running and loaded, I receive a NullReferenceException.

$pool = 'app1-0'
$newpool = 'app1-1'
cp "iis:/apppools/$pool" "iis:/apppools/$newpool" -force

Output:

Copy-Item : Object reference not set to an instance of an object.

If I stop the pool, or start the app pool and do not load the application, the copy command succeeds.

Short of copying the properties one by one, is there a way to copy/clone a running, and loaded, app pool?


Solution

  • Have you tried using appcmd instead?

    Update: try a combination of both -

    Maybe add doesn't let you both import and specify commands. You could try something like this:

    appcmd list appool thing1 /xml > c:\tempfile.xml
    (Get-Content c:\tempfile.xml).Replace("thing1", "thing2") | Out-File c:\tempfile2.xml
    appcmd add apppool /in < c:\tempfile2.xml
    

    You may have to debug that script a bit :)