• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

remote application pool recycle using WMI \ PowerShell

B
#1
I'm having some trouble finding the best method for recycling remote IIS6
application pools using PowerShell.

In the past this has always been very easy using VB script:

Function PoolRecycle(strServer)

On Error Resume Next
Err.Clear

Dim objWMIService
Dim colItems
Dim objItem

'recycle application pool
WScript.Echo ""
Err.Clear
Set objWMIService = GetObject _
("winmgmts:{authenticationLevel=pktPrivacy}\\" _
& strServer & "\root\microsoftiisv2")

'connect to WMI
Set colItems = objWMIService.ExecQuery("Select * From IIsApplicationPool")

'Step through the objects to be recycled
For each objItem in colItems
objItem.Recycle
If err = 0 Then
WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
PoolRecycle = "success"
Else
WScript.Echo("Unable to recycle the application pool on " & strServer)
PoolRecycle = "failed"
End If
Next

End Function

I have attempted to duplicate the same thing in PowerShell, but I believe I
am still running into an issue with the pktPrivacy authentication setting.
The following code works when executing localy, but not on a remote host:

Function PoolRecycle($strServerName)
{
$objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
$objWMI.Scope.Path = "\\" + $strServerName + "\root\microsoftiisv2"
$objWMI.Scope.Options.Authentication = 6
$pools = $objWMI.Get()
foreach ($pool in $pools)
{
$pool.recycle()
if (!$?)
{
Write-Host $pool.name " - ERROR"
}
else
{
Write-Host $pool.name " - Recycled"
}
}
}


Any help or better ways of accomplishing this in PowerShell would be very
much appreciated!

Thanks
Bill
 

My Computer

S

Shay Levi

#2
If you know the application pool name (the function returns true/false respectively:

function Recycle-AppPool{
param([string]$server,[string]$appPool)

([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='W3SVC/AppPools/$appPool'").recycle();
if($? -ne 0}{$false} else {$true}
}


To get a list of application pool names:

$computer = "your Server name/ip"
gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2" -computer
$computer | % {$_.name}



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> I'm having some trouble finding the best method for recycling remote
> IIS6 application pools using PowerShell.
>
> In the past this has always been very easy using VB script:
>
> Function PoolRecycle(strServer)
>
> On Error Resume Next
> Err.Clear
> Dim objWMIService
> Dim colItems
> Dim objItem
> 'recycle application pool
> WScript.Echo ""
> Err.Clear
> Set objWMIService = GetObject _
> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
> & strServer & "\root\microsoftiisv2")
> 'connect to WMI
> Set colItems = objWMIService.ExecQuery("Select * From
> IIsApplicationPool")
> 'Step through the objects to be recycled
> For each objItem in colItems
> objItem.Recycle
> If err = 0 Then
> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
> PoolRecycle = "success"
> Else
> WScript.Echo("Unable to recycle the application pool on " &
> strServer)
> PoolRecycle = "failed"
> End If
> Next
> End Function
>
> I have attempted to duplicate the same thing in PowerShell, but I
> believe I am still running into an issue with the pktPrivacy
> authentication setting. The following code works when executing
> localy, but not on a remote host:
>
> Function PoolRecycle($strServerName)
> {
> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> $objWMI.Scope.Path = "\\" + $strServerName + "\root\microsoftiisv2"
> $objWMI.Scope.Options.Authentication = 6
> $pools = $objWMI.Get()
> foreach ($pool in $pools)
> {
> $pool.recycle()
> if (!$?)
> {
> Write-Host $pool.name " - ERROR"
> }
> else
> {
> Write-Host $pool.name " - Recycled"
> }
> }
> }
> Any help or better ways of accomplishing this in PowerShell would be
> very much appreciated!
>
> Thanks
> Bill
 

My Computer

S

Shay Levi

#3
Pinging the remote machine before trying to recycle pools is a recommended
practice.

Also, I wrote:

gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2" -computer
$computer | % {$_.name}

for those who are not familiar with gwmi and %, I should say that they are
aliases for:
gwmi = get-wmiobject
% = foreach-object

I should have written it like:

get-wmiobject -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
-computer $computer | foreach-object {$_.name}



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> If you know the application pool name (the function returns true/false
> respectively:
>
> function Recycle-AppPool{
> param([string]$server,[string]$appPool)
> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='W3SVC/A
> ppPools/$appPool'").recycle();
> if($? -ne 0}{$false} else {$true}
> }
> To get a list of application pool names:
>
> $computer = "your Server name/ip"
> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
> -computer
> $computer | % {$_.name}
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com

>> I'm having some trouble finding the best method for recycling remote
>> IIS6 application pools using PowerShell.
>>
>> In the past this has always been very easy using VB script:
>>
>> Function PoolRecycle(strServer)
>>
>> On Error Resume Next
>> Err.Clear
>> Dim objWMIService
>> Dim colItems
>> Dim objItem
>> 'recycle application pool
>> WScript.Echo ""
>> Err.Clear
>> Set objWMIService = GetObject _
>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>> & strServer & "\root\microsoftiisv2")
>> 'connect to WMI
>> Set colItems = objWMIService.ExecQuery("Select * From
>> IIsApplicationPool")
>> 'Step through the objects to be recycled
>> For each objItem in colItems
>> objItem.Recycle
>> If err = 0 Then
>> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
>> PoolRecycle = "success"
>> Else
>> WScript.Echo("Unable to recycle the application pool on " &
>> strServer)
>> PoolRecycle = "failed"
>> End If
>> Next
>> End Function
>> I have attempted to duplicate the same thing in PowerShell, but I
>> believe I am still running into an issue with the pktPrivacy
>> authentication setting. The following code works when executing
>> localy, but not on a remote host:
>>
>> Function PoolRecycle($strServerName)
>> {
>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>> $objWMI.Scope.Path = "\\" + $strServerName + "\root\microsoftiisv2"
>> $objWMI.Scope.Options.Authentication = 6
>> $pools = $objWMI.Get()
>> foreach ($pool in $pools)
>> {
>> $pool.recycle()
>> if (!$?)
>> {
>> Write-Host $pool.name " - ERROR"
>> }
>> else
>> {
>> Write-Host $pool.name " - Recycled"
>> }
>> }
>> }
>> Any help or better ways of accomplishing this in PowerShell would be
>> very much appreciated!
>> Thanks
>> Bill
 

My Computer

B
#4
Shay,

Thanks for the help, but I see the namespace "root\MicrosoftIISv2" operate
differently when attempting to access it on a remote IIS6 computer (default
W2K3 installs).


The code examples you pointed out all work fine when targeting the namespace
locally, but I recieve access denied messages when useing the same commands
remotely.

I have tried from multiple machines to multiple servers. The VB Script I
supplied still works in all cases, which leads me to believe I am not setting
the PowerShell WMI object authentication to use pktPrivacy correctly.

Any advice would be appreciated.

Thanks
Bill

"Shay Levi" wrote:

> Pinging the remote machine before trying to recycle pools is a recommended
> practice.
>
> Also, I wrote:
>
> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2" -computer
> $computer | % {$_.name}
>
> for those who are not familiar with gwmi and %, I should say that they are
> aliases for:
> gwmi = get-wmiobject
> % = foreach-object
>
> I should have written it like:
>
> get-wmiobject -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
> -computer $computer | foreach-object {$_.name}
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
>
>

> > If you know the application pool name (the function returns true/false
> > respectively:
> >
> > function Recycle-AppPool{
> > param([string]$server,[string]$appPool)
> > ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='W3SVC/A
> > ppPools/$appPool'").recycle();
> > if($? -ne 0}{$false} else {$true}
> > }
> > To get a list of application pool names:
> >
> > $computer = "your Server name/ip"
> > gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
> > -computer
> > $computer | % {$_.name}
> > -----
> > Shay Levi
> > $cript Fanatic
> > http://scriptolog.blogspot.com

> >> I'm having some trouble finding the best method for recycling remote
> >> IIS6 application pools using PowerShell.
> >>
> >> In the past this has always been very easy using VB script:
> >>
> >> Function PoolRecycle(strServer)
> >>
> >> On Error Resume Next
> >> Err.Clear
> >> Dim objWMIService
> >> Dim colItems
> >> Dim objItem
> >> 'recycle application pool
> >> WScript.Echo ""
> >> Err.Clear
> >> Set objWMIService = GetObject _
> >> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
> >> & strServer & "\root\microsoftiisv2")
> >> 'connect to WMI
> >> Set colItems = objWMIService.ExecQuery("Select * From
> >> IIsApplicationPool")
> >> 'Step through the objects to be recycled
> >> For each objItem in colItems
> >> objItem.Recycle
> >> If err = 0 Then
> >> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
> >> PoolRecycle = "success"
> >> Else
> >> WScript.Echo("Unable to recycle the application pool on " &
> >> strServer)
> >> PoolRecycle = "failed"
> >> End If
> >> Next
> >> End Function
> >> I have attempted to duplicate the same thing in PowerShell, but I
> >> believe I am still running into an issue with the pktPrivacy
> >> authentication setting. The following code works when executing
> >> localy, but not on a remote host:
> >>
> >> Function PoolRecycle($strServerName)
> >> {
> >> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> >> $objWMI.Scope.Path = "\\" + $strServerName + "\root\microsoftiisv2"
> >> $objWMI.Scope.Options.Authentication = 6
> >> $pools = $objWMI.Get()
> >> foreach ($pool in $pools)
> >> {
> >> $pool.recycle()
> >> if (!$?)
> >> {
> >> Write-Host $pool.name " - ERROR"
> >> }
> >> else
> >> {
> >> Write-Host $pool.name " - Recycled"
> >> }
> >> }
> >> }
> >> Any help or better ways of accomplishing this in PowerShell would be
> >> very much appreciated!
> >> Thanks
> >> Bill
>
>
>
 

My Computer

S

Shay Levi

#5
Bill

I can't test it right now. Try this on the remote server(s:)

1. Start > Run > dcomcnfg > ENTER
2. In the left pane (Compnent Services.msc) expand "Component Services" >
Computers
3. Right click "My Computer"
4. In the "Default Properties" tab tick the "Enable COM Internet Services
on this computer" checkbox
5. Reboot remote server
6. Test again

I can't recall if you need to add Access Permissions in the "COM Security"
tab.



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> Shay,
>
> Thanks for the help, but I see the namespace "root\MicrosoftIISv2"
> operate differently when attempting to access it on a remote IIS6
> computer (default W2K3 installs).
>
> The code examples you pointed out all work fine when targeting the
> namespace locally, but I recieve access denied messages when useing
> the same commands remotely.
>
> I have tried from multiple machines to multiple servers. The VB
> Script I supplied still works in all cases, which leads me to believe
> I am not setting the PowerShell WMI object authentication to use
> pktPrivacy correctly.
>
> Any advice would be appreciated.
>
> Thanks
> Bill
> "Shay Levi" wrote:
>

>> Pinging the remote machine before trying to recycle pools is a
>> recommended practice.
>>
>> Also, I wrote:
>>
>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
>> -computer $computer | % {$_.name}
>>
>> for those who are not familiar with gwmi and %, I should say that
>> they are
>> aliases for:
>> gwmi = get-wmiobject
>> % = foreach-object
>> I should have written it like:
>>
>> get-wmiobject -class IIsApplicationPool -namespace
>> "root\MicrosoftIISv2" -computer $computer | foreach-object {$_.name}
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com

>>> If you know the application pool name (the function returns
>>> true/false respectively:
>>>
>>> function Recycle-AppPool{
>>> param([string]$server,[string]$appPool)
>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='W3SVC/
>>> A
>>> ppPools/$appPool'").recycle();
>>> if($? -ne 0}{$false} else {$true}
>>> }
>>> To get a list of application pool names:
>>> $computer = "your Server name/ip"
>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
>>> -computer
>>> $computer | % {$_.name}
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> I'm having some trouble finding the best method for recycling
>>>> remote IIS6 application pools using PowerShell.
>>>>
>>>> In the past this has always been very easy using VB script:
>>>>
>>>> Function PoolRecycle(strServer)
>>>>
>>>> On Error Resume Next
>>>> Err.Clear
>>>> Dim objWMIService
>>>> Dim colItems
>>>> Dim objItem
>>>> 'recycle application pool
>>>> WScript.Echo ""
>>>> Err.Clear
>>>> Set objWMIService = GetObject _
>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>> & strServer & "\root\microsoftiisv2")
>>>> 'connect to WMI
>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>> IIsApplicationPool")
>>>> 'Step through the objects to be recycled
>>>> For each objItem in colItems
>>>> objItem.Recycle
>>>> If err = 0 Then
>>>> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
>>>> PoolRecycle = "success"
>>>> Else
>>>> WScript.Echo("Unable to recycle the application pool on " &
>>>> strServer)
>>>> PoolRecycle = "failed"
>>>> End If
>>>> Next
>>>> End Function
>>>> I have attempted to duplicate the same thing in PowerShell, but I
>>>> believe I am still running into an issue with the pktPrivacy
>>>> authentication setting. The following code works when executing
>>>> localy, but not on a remote host:
>>>> Function PoolRecycle($strServerName)
>>>> {
>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>> $objWMI.Scope.Path = "\\" + $strServerName + "\root\microsoftiisv2"
>>>> $objWMI.Scope.Options.Authentication = 6
>>>> $pools = $objWMI.Get()
>>>> foreach ($pool in $pools)
>>>> {
>>>> $pool.recycle()
>>>> if (!$?)
>>>> {
>>>> Write-Host $pool.name " - ERROR"
>>>> }
>>>> else
>>>> {
>>>> Write-Host $pool.name " - Recycled"
>>>> }
>>>> }
>>>> }
>>>> Any help or better ways of accomplishing this in PowerShell would
>>>> be
>>>> very much appreciated!
>>>> Thanks
>>>> Bill
 

My Computer

B
#6
Hi Shay,

I have tested this and it still does not resolve the issue.

I guess I must be missing something. The fact that the VB Script works and
the PowerShell does not, leads me to believe that I have an issue with my
script and not the security on the target server.

I may have to use PowerShell to call the actual .Net class instead of using
the built in commands.

Funny, I would have never believed that something as simple as recycling a
remote application pool would be so hard :)

Thanks again for the help.
Bill


"Shay Levi" wrote:

> Bill
>
> I can't test it right now. Try this on the remote server(s:)
>
> 1. Start > Run > dcomcnfg > ENTER
> 2. In the left pane (Compnent Services.msc) expand "Component Services" >
> Computers
> 3. Right click "My Computer"
> 4. In the "Default Properties" tab tick the "Enable COM Internet Services
> on this computer" checkbox
> 5. Reboot remote server
> 6. Test again
>
> I can't recall if you need to add Access Permissions in the "COM Security"
> tab.
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
>
>

> > Shay,
> >
> > Thanks for the help, but I see the namespace "root\MicrosoftIISv2"
> > operate differently when attempting to access it on a remote IIS6
> > computer (default W2K3 installs).
> >
> > The code examples you pointed out all work fine when targeting the
> > namespace locally, but I recieve access denied messages when useing
> > the same commands remotely.
> >
> > I have tried from multiple machines to multiple servers. The VB
> > Script I supplied still works in all cases, which leads me to believe
> > I am not setting the PowerShell WMI object authentication to use
> > pktPrivacy correctly.
> >
> > Any advice would be appreciated.
> >
> > Thanks
> > Bill
> > "Shay Levi" wrote:
> >

> >> Pinging the remote machine before trying to recycle pools is a
> >> recommended practice.
> >>
> >> Also, I wrote:
> >>
> >> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
> >> -computer $computer | % {$_.name}
> >>
> >> for those who are not familiar with gwmi and %, I should say that
> >> they are
> >> aliases for:
> >> gwmi = get-wmiobject
> >> % = foreach-object
> >> I should have written it like:
> >>
> >> get-wmiobject -class IIsApplicationPool -namespace
> >> "root\MicrosoftIISv2" -computer $computer | foreach-object {$_.name}
> >>
> >> -----
> >> Shay Levi
> >> $cript Fanatic
> >> http://scriptolog.blogspot.com
> >>> If you know the application pool name (the function returns
> >>> true/false respectively:
> >>>
> >>> function Recycle-AppPool{
> >>> param([string]$server,[string]$appPool)
> >>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='W3SVC/
> >>> A
> >>> ppPools/$appPool'").recycle();
> >>> if($? -ne 0}{$false} else {$true}
> >>> }
> >>> To get a list of application pool names:
> >>> $computer = "your Server name/ip"
> >>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
> >>> -computer
> >>> $computer | % {$_.name}
> >>> -----
> >>> Shay Levi
> >>> $cript Fanatic
> >>> http://scriptolog.blogspot.com
> >>>> I'm having some trouble finding the best method for recycling
> >>>> remote IIS6 application pools using PowerShell.
> >>>>
> >>>> In the past this has always been very easy using VB script:
> >>>>
> >>>> Function PoolRecycle(strServer)
> >>>>
> >>>> On Error Resume Next
> >>>> Err.Clear
> >>>> Dim objWMIService
> >>>> Dim colItems
> >>>> Dim objItem
> >>>> 'recycle application pool
> >>>> WScript.Echo ""
> >>>> Err.Clear
> >>>> Set objWMIService = GetObject _
> >>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
> >>>> & strServer & "\root\microsoftiisv2")
> >>>> 'connect to WMI
> >>>> Set colItems = objWMIService.ExecQuery("Select * From
> >>>> IIsApplicationPool")
> >>>> 'Step through the objects to be recycled
> >>>> For each objItem in colItems
> >>>> objItem.Recycle
> >>>> If err = 0 Then
> >>>> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
> >>>> PoolRecycle = "success"
> >>>> Else
> >>>> WScript.Echo("Unable to recycle the application pool on " &
> >>>> strServer)
> >>>> PoolRecycle = "failed"
> >>>> End If
> >>>> Next
> >>>> End Function
> >>>> I have attempted to duplicate the same thing in PowerShell, but I
> >>>> believe I am still running into an issue with the pktPrivacy
> >>>> authentication setting. The following code works when executing
> >>>> localy, but not on a remote host:
> >>>> Function PoolRecycle($strServerName)
> >>>> {
> >>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> >>>> $objWMI.Scope.Path = "\\" + $strServerName + "\root\microsoftiisv2"
> >>>> $objWMI.Scope.Options.Authentication = 6
> >>>> $pools = $objWMI.Get()
> >>>> foreach ($pool in $pools)
> >>>> {
> >>>> $pool.recycle()
> >>>> if (!$?)
> >>>> {
> >>>> Write-Host $pool.name " - ERROR"
> >>>> }
> >>>> else
> >>>> {
> >>>> Write-Host $pool.name " - Recycled"
> >>>> }
> >>>> }
> >>>> }
> >>>> Any help or better ways of accomplishing this in PowerShell would
> >>>> be
> >>>> very much appreciated!
> >>>> Thanks
> >>>> Bill
>
>
>
 

My Computer

S

Shay Levi

#7
Are you able to list the Application poll names?

$server="server"
$objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
$objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
$objWMI.Scope.Options.Authentication = [System.Management.AuthenticationLevel]::PacketPrivacy
$pools = $objWMI.Get()
$pools | foreach { $_.name }


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> Hi Shay,
>
> I have tested this and it still does not resolve the issue.
>
> I guess I must be missing something. The fact that the VB Script
> works and the PowerShell does not, leads me to believe that I have an
> issue with my script and not the security on the target server.
>
> I may have to use PowerShell to call the actual .Net class instead of
> using the built in commands.
>
> Funny, I would have never believed that something as simple as
> recycling a remote application pool would be so hard :)
>
> Thanks again for the help.
> Bill
> "Shay Levi" wrote:
>

>> Bill
>>
>> I can't test it right now. Try this on the remote server(s:)
>>
>> 1. Start > Run > dcomcnfg > ENTER
>> 2. In the left pane (Compnent Services.msc) expand "Component
>> Services" >
>> Computers
>> 3. Right click "My Computer"
>> 4. In the "Default Properties" tab tick the "Enable COM Internet
>> Services
>> on this computer" checkbox
>> 5. Reboot remote server
>> 6. Test again
>> I can't recall if you need to add Access Permissions in the "COM
>> Security" tab.
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com

>>> Shay,
>>>
>>> Thanks for the help, but I see the namespace "root\MicrosoftIISv2"
>>> operate differently when attempting to access it on a remote IIS6
>>> computer (default W2K3 installs).
>>>
>>> The code examples you pointed out all work fine when targeting the
>>> namespace locally, but I recieve access denied messages when useing
>>> the same commands remotely.
>>>
>>> I have tried from multiple machines to multiple servers. The VB
>>> Script I supplied still works in all cases, which leads me to
>>> believe I am not setting the PowerShell WMI object authentication to
>>> use pktPrivacy correctly.
>>>
>>> Any advice would be appreciated.
>>>
>>> Thanks
>>> Bill
>>> "Shay Levi" wrote:
>>>> Pinging the remote machine before trying to recycle pools is a
>>>> recommended practice.
>>>>
>>>> Also, I wrote:
>>>>
>>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
>>>> -computer $computer | % {$_.name}
>>>>
>>>> for those who are not familiar with gwmi and %, I should say that
>>>> they are
>>>> aliases for:
>>>> gwmi = get-wmiobject
>>>> % = foreach-object
>>>> I should have written it like:
>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
>>>> {$_.name}
>>>>
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>>> If you know the application pool name (the function returns
>>>>> true/false respectively:
>>>>>
>>>>> function Recycle-AppPool{
>>>>> param([string]$server,[string]$appPool)
>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='W3SV
>>>>> C/
>>>>> A
>>>>> ppPools/$appPool'").recycle();
>>>>> if($? -ne 0}{$false} else {$true}
>>>>> }
>>>>> To get a list of application pool names:
>>>>> $computer = "your Server name/ip"
>>>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
>>>>> -computer
>>>>> $computer | % {$_.name}
>>>>> -----
>>>>> Shay Levi
>>>>> $cript Fanatic
>>>>> http://scriptolog.blogspot.com
>>>>>> I'm having some trouble finding the best method for recycling
>>>>>> remote IIS6 application pools using PowerShell.
>>>>>>
>>>>>> In the past this has always been very easy using VB script:
>>>>>>
>>>>>> Function PoolRecycle(strServer)
>>>>>>
>>>>>> On Error Resume Next
>>>>>> Err.Clear
>>>>>> Dim objWMIService
>>>>>> Dim colItems
>>>>>> Dim objItem
>>>>>> 'recycle application pool
>>>>>> WScript.Echo ""
>>>>>> Err.Clear
>>>>>> Set objWMIService = GetObject _
>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>>>> & strServer & "\root\microsoftiisv2")
>>>>>> 'connect to WMI
>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>>>> IIsApplicationPool")
>>>>>> 'Step through the objects to be recycled
>>>>>> For each objItem in colItems
>>>>>> objItem.Recycle
>>>>>> If err = 0 Then
>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
>>>>>> PoolRecycle = "success"
>>>>>> Else
>>>>>> WScript.Echo("Unable to recycle the application pool on " &
>>>>>> strServer)
>>>>>> PoolRecycle = "failed"
>>>>>> End If
>>>>>> Next
>>>>>> End Function
>>>>>> I have attempted to duplicate the same thing in PowerShell, but I
>>>>>> believe I am still running into an issue with the pktPrivacy
>>>>>> authentication setting. The following code works when executing
>>>>>> localy, but not on a remote host:
>>>>>> Function PoolRecycle($strServerName)
>>>>>> {
>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
>>>>>> "\root\microsoftiisv2"
>>>>>> $objWMI.Scope.Options.Authentication = 6
>>>>>> $pools = $objWMI.Get()
>>>>>> foreach ($pool in $pools)
>>>>>> {
>>>>>> $pool.recycle()
>>>>>> if (!$?)
>>>>>> {
>>>>>> Write-Host $pool.name " - ERROR"
>>>>>> }
>>>>>> else
>>>>>> {
>>>>>> Write-Host $pool.name " - Recycled"
>>>>>> }
>>>>>> }
>>>>>> }
>>>>>> Any help or better ways of accomplishing this in PowerShell would
>>>>>> be
>>>>>> very much appreciated!
>>>>>> Thanks
>>>>>> Bill
 

My Computer

S

Shay Levi

#8
Bill

Just to clarify, I'm not able to recycle remote app pools, just list their
names (Access denied).
Even adding full security on IIsApplicationPool (WMI Properties) didn't resolve
it.
I'm still on it...

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> Are you able to list the Application poll names?
>
> $server="server"
> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
> $objWMI.Scope.Options.Authentication =
> [System.Management.AuthenticationLevel]::PacketPrivacy
> $pools = $objWMI.Get()
> $pools | foreach { $_.name }
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com

>> Hi Shay,
>>
>> I have tested this and it still does not resolve the issue.
>>
>> I guess I must be missing something. The fact that the VB Script
>> works and the PowerShell does not, leads me to believe that I have an
>> issue with my script and not the security on the target server.
>>
>> I may have to use PowerShell to call the actual .Net class instead of
>> using the built in commands.
>>
>> Funny, I would have never believed that something as simple as
>> recycling a remote application pool would be so hard :)
>>
>> Thanks again for the help.
>> Bill
>> "Shay Levi" wrote:

>>> Bill
>>>
>>> I can't test it right now. Try this on the remote server(s:)
>>>
>>> 1. Start > Run > dcomcnfg > ENTER
>>> 2. In the left pane (Compnent Services.msc) expand "Component
>>> Services" >
>>> Computers
>>> 3. Right click "My Computer"
>>> 4. In the "Default Properties" tab tick the "Enable COM Internet
>>> Services
>>> on this computer" checkbox
>>> 5. Reboot remote server
>>> 6. Test again
>>> I can't recall if you need to add Access Permissions in the "COM
>>> Security" tab.
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> Shay,
>>>>
>>>> Thanks for the help, but I see the namespace "root\MicrosoftIISv2"
>>>> operate differently when attempting to access it on a remote IIS6
>>>> computer (default W2K3 installs).
>>>>
>>>> The code examples you pointed out all work fine when targeting the
>>>> namespace locally, but I recieve access denied messages when useing
>>>> the same commands remotely.
>>>>
>>>> I have tried from multiple machines to multiple servers. The VB
>>>> Script I supplied still works in all cases, which leads me to
>>>> believe I am not setting the PowerShell WMI object authentication
>>>> to use pktPrivacy correctly.
>>>>
>>>> Any advice would be appreciated.
>>>>
>>>> Thanks
>>>> Bill
>>>> "Shay Levi" wrote:
>>>>> Pinging the remote machine before trying to recycle pools is a
>>>>> recommended practice.
>>>>>
>>>>> Also, I wrote:
>>>>>
>>>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
>>>>> -computer $computer | % {$_.name}
>>>>>
>>>>> for those who are not familiar with gwmi and %, I should say that
>>>>> they are
>>>>> aliases for:
>>>>> gwmi = get-wmiobject
>>>>> % = foreach-object
>>>>> I should have written it like:
>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
>>>>> {$_.name}
>>>>> -----
>>>>> Shay Levi
>>>>> $cript Fanatic
>>>>> http://scriptolog.blogspot.com
>>>>>> If you know the application pool name (the function returns
>>>>>> true/false respectively:
>>>>>>
>>>>>> function Recycle-AppPool{
>>>>>> param([string]$server,[string]$appPool)
>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='W3S
>>>>>> V
>>>>>> C/
>>>>>> A
>>>>>> ppPools/$appPool'").recycle();
>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>> }
>>>>>> To get a list of application pool names:
>>>>>> $computer = "your Server name/ip"
>>>>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
>>>>>> -computer
>>>>>> $computer | % {$_.name}
>>>>>> -----
>>>>>> Shay Levi
>>>>>> $cript Fanatic
>>>>>> http://scriptolog.blogspot.com
>>>>>>> I'm having some trouble finding the best method for recycling
>>>>>>> remote IIS6 application pools using PowerShell.
>>>>>>>
>>>>>>> In the past this has always been very easy using VB script:
>>>>>>>
>>>>>>> Function PoolRecycle(strServer)
>>>>>>>
>>>>>>> On Error Resume Next
>>>>>>> Err.Clear
>>>>>>> Dim objWMIService
>>>>>>> Dim colItems
>>>>>>> Dim objItem
>>>>>>> 'recycle application pool
>>>>>>> WScript.Echo ""
>>>>>>> Err.Clear
>>>>>>> Set objWMIService = GetObject _
>>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>>>>> & strServer & "\root\microsoftiisv2")
>>>>>>> 'connect to WMI
>>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>>>>> IIsApplicationPool")
>>>>>>> 'Step through the objects to be recycled
>>>>>>> For each objItem in colItems
>>>>>>> objItem.Recycle
>>>>>>> If err = 0 Then
>>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
>>>>>>> PoolRecycle = "success"
>>>>>>> Else
>>>>>>> WScript.Echo("Unable to recycle the application pool on " &
>>>>>>> strServer)
>>>>>>> PoolRecycle = "failed"
>>>>>>> End If
>>>>>>> Next
>>>>>>> End Function
>>>>>>> I have attempted to duplicate the same thing in PowerShell, but
>>>>>>> I
>>>>>>> believe I am still running into an issue with the pktPrivacy
>>>>>>> authentication setting. The following code works when executing
>>>>>>> localy, but not on a remote host:
>>>>>>> Function PoolRecycle($strServerName)
>>>>>>> {
>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
>>>>>>> "\root\microsoftiisv2"
>>>>>>> $objWMI.Scope.Options.Authentication = 6
>>>>>>> $pools = $objWMI.Get()
>>>>>>> foreach ($pool in $pools)
>>>>>>> {
>>>>>>> $pool.recycle()
>>>>>>> if (!$?)
>>>>>>> {
>>>>>>> Write-Host $pool.name " - ERROR"
>>>>>>> }
>>>>>>> else
>>>>>>> {
>>>>>>> Write-Host $pool.name " - Recycled"
>>>>>>> }
>>>>>>> }
>>>>>>> }
>>>>>>> Any help or better ways of accomplishing this in PowerShell
>>>>>>> would
>>>>>>> be
>>>>>>> very much appreciated!
>>>>>>> Thanks
>>>>>>> Bill
 

My Computer

B
#9
Hi Shay,

Yeah, I can list the pools, but only after specifing the authentication
setting.

It's almost like it does not carry to the other objects.

I appreciate the help.

Thanks
Bill


"Shay Levi" wrote:

> Bill
>
> Just to clarify, I'm not able to recycle remote app pools, just list their
> names (Access denied).
> Even adding full security on IIsApplicationPool (WMI Properties) didn't resolve
> it.
> I'm still on it...
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
>
>

> > Are you able to list the Application poll names?
> >
> > $server="server"
> > $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> > $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
> > $objWMI.Scope.Options.Authentication =
> > [System.Management.AuthenticationLevel]::PacketPrivacy
> > $pools = $objWMI.Get()
> > $pools | foreach { $_.name }
> > -----
> > Shay Levi
> > $cript Fanatic
> > http://scriptolog.blogspot.com

> >> Hi Shay,
> >>
> >> I have tested this and it still does not resolve the issue.
> >>
> >> I guess I must be missing something. The fact that the VB Script
> >> works and the PowerShell does not, leads me to believe that I have an
> >> issue with my script and not the security on the target server.
> >>
> >> I may have to use PowerShell to call the actual .Net class instead of
> >> using the built in commands.
> >>
> >> Funny, I would have never believed that something as simple as
> >> recycling a remote application pool would be so hard :)
> >>
> >> Thanks again for the help.
> >> Bill
> >> "Shay Levi" wrote:
> >>> Bill
> >>>
> >>> I can't test it right now. Try this on the remote server(s:)
> >>>
> >>> 1. Start > Run > dcomcnfg > ENTER
> >>> 2. In the left pane (Compnent Services.msc) expand "Component
> >>> Services" >
> >>> Computers
> >>> 3. Right click "My Computer"
> >>> 4. In the "Default Properties" tab tick the "Enable COM Internet
> >>> Services
> >>> on this computer" checkbox
> >>> 5. Reboot remote server
> >>> 6. Test again
> >>> I can't recall if you need to add Access Permissions in the "COM
> >>> Security" tab.
> >>> -----
> >>> Shay Levi
> >>> $cript Fanatic
> >>> http://scriptolog.blogspot.com
> >>>> Shay,
> >>>>
> >>>> Thanks for the help, but I see the namespace "root\MicrosoftIISv2"
> >>>> operate differently when attempting to access it on a remote IIS6
> >>>> computer (default W2K3 installs).
> >>>>
> >>>> The code examples you pointed out all work fine when targeting the
> >>>> namespace locally, but I recieve access denied messages when useing
> >>>> the same commands remotely.
> >>>>
> >>>> I have tried from multiple machines to multiple servers. The VB
> >>>> Script I supplied still works in all cases, which leads me to
> >>>> believe I am not setting the PowerShell WMI object authentication
> >>>> to use pktPrivacy correctly.
> >>>>
> >>>> Any advice would be appreciated.
> >>>>
> >>>> Thanks
> >>>> Bill
> >>>> "Shay Levi" wrote:
> >>>>> Pinging the remote machine before trying to recycle pools is a
> >>>>> recommended practice.
> >>>>>
> >>>>> Also, I wrote:
> >>>>>
> >>>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
> >>>>> -computer $computer | % {$_.name}
> >>>>>
> >>>>> for those who are not familiar with gwmi and %, I should say that
> >>>>> they are
> >>>>> aliases for:
> >>>>> gwmi = get-wmiobject
> >>>>> % = foreach-object
> >>>>> I should have written it like:
> >>>>> get-wmiobject -class IIsApplicationPool -namespace
> >>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
> >>>>> {$_.name}
> >>>>> -----
> >>>>> Shay Levi
> >>>>> $cript Fanatic
> >>>>> http://scriptolog.blogspot.com
> >>>>>> If you know the application pool name (the function returns
> >>>>>> true/false respectively:
> >>>>>>
> >>>>>> function Recycle-AppPool{
> >>>>>> param([string]$server,[string]$appPool)
> >>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='W3S
> >>>>>> V
> >>>>>> C/
> >>>>>> A
> >>>>>> ppPools/$appPool'").recycle();
> >>>>>> if($? -ne 0}{$false} else {$true}
> >>>>>> }
> >>>>>> To get a list of application pool names:
> >>>>>> $computer = "your Server name/ip"
> >>>>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
> >>>>>> -computer
> >>>>>> $computer | % {$_.name}
> >>>>>> -----
> >>>>>> Shay Levi
> >>>>>> $cript Fanatic
> >>>>>> http://scriptolog.blogspot.com
> >>>>>>> I'm having some trouble finding the best method for recycling
> >>>>>>> remote IIS6 application pools using PowerShell.
> >>>>>>>
> >>>>>>> In the past this has always been very easy using VB script:
> >>>>>>>
> >>>>>>> Function PoolRecycle(strServer)
> >>>>>>>
> >>>>>>> On Error Resume Next
> >>>>>>> Err.Clear
> >>>>>>> Dim objWMIService
> >>>>>>> Dim colItems
> >>>>>>> Dim objItem
> >>>>>>> 'recycle application pool
> >>>>>>> WScript.Echo ""
> >>>>>>> Err.Clear
> >>>>>>> Set objWMIService = GetObject _
> >>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
> >>>>>>> & strServer & "\root\microsoftiisv2")
> >>>>>>> 'connect to WMI
> >>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
> >>>>>>> IIsApplicationPool")
> >>>>>>> 'Step through the objects to be recycled
> >>>>>>> For each objItem in colItems
> >>>>>>> objItem.Recycle
> >>>>>>> If err = 0 Then
> >>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
> >>>>>>> PoolRecycle = "success"
> >>>>>>> Else
> >>>>>>> WScript.Echo("Unable to recycle the application pool on " &
> >>>>>>> strServer)
> >>>>>>> PoolRecycle = "failed"
> >>>>>>> End If
> >>>>>>> Next
> >>>>>>> End Function
> >>>>>>> I have attempted to duplicate the same thing in PowerShell, but
> >>>>>>> I
> >>>>>>> believe I am still running into an issue with the pktPrivacy
> >>>>>>> authentication setting. The following code works when executing
> >>>>>>> localy, but not on a remote host:
> >>>>>>> Function PoolRecycle($strServerName)
> >>>>>>> {
> >>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> >>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
> >>>>>>> "\root\microsoftiisv2"
> >>>>>>> $objWMI.Scope.Options.Authentication = 6
> >>>>>>> $pools = $objWMI.Get()
> >>>>>>> foreach ($pool in $pools)
> >>>>>>> {
> >>>>>>> $pool.recycle()
> >>>>>>> if (!$?)
> >>>>>>> {
> >>>>>>> Write-Host $pool.name " - ERROR"
> >>>>>>> }
> >>>>>>> else
> >>>>>>> {
> >>>>>>> Write-Host $pool.name " - Recycled"
> >>>>>>> }
> >>>>>>> }
> >>>>>>> }
> >>>>>>> Any help or better ways of accomplishing this in PowerShell
> >>>>>>> would
> >>>>>>> be
> >>>>>>> very much appreciated!
> >>>>>>> Thanks
> >>>>>>> Bill
>
>
>
 

My Computer

S

Shay Levi

#10
I'll keep looking for it :-)

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> Hi Shay,
>
> Yeah, I can list the pools, but only after specifing the
> authentication setting.
>
> It's almost like it does not carry to the other objects.
>
> I appreciate the help.
>
> Thanks
> Bill
> "Shay Levi" wrote:
>

>> Bill
>>
>> Just to clarify, I'm not able to recycle remote app pools, just list
>> their
>> names (Access denied).
>> Even adding full security on IIsApplicationPool (WMI Properties)
>> didn't resolve
>> it.
>> I'm still on it...
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com

>>> Are you able to list the Application poll names?
>>>
>>> $server="server"
>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>> $objWMI.Scope.Options.Authentication =
>>> [System.Management.AuthenticationLevel]::PacketPrivacy
>>> $pools = $objWMI.Get()
>>> $pools | foreach { $_.name }
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> Hi Shay,
>>>>
>>>> I have tested this and it still does not resolve the issue.
>>>>
>>>> I guess I must be missing something. The fact that the VB Script
>>>> works and the PowerShell does not, leads me to believe that I have
>>>> an issue with my script and not the security on the target server.
>>>>
>>>> I may have to use PowerShell to call the actual .Net class instead
>>>> of using the built in commands.
>>>>
>>>> Funny, I would have never believed that something as simple as
>>>> recycling a remote application pool would be so hard :)
>>>>
>>>> Thanks again for the help.
>>>> Bill
>>>> "Shay Levi" wrote:
>>>>> Bill
>>>>>
>>>>> I can't test it right now. Try this on the remote server(s:)
>>>>>
>>>>> 1. Start > Run > dcomcnfg > ENTER
>>>>> 2. In the left pane (Compnent Services.msc) expand "Component
>>>>> Services" >
>>>>> Computers
>>>>> 3. Right click "My Computer"
>>>>> 4. In the "Default Properties" tab tick the "Enable COM Internet
>>>>> Services
>>>>> on this computer" checkbox
>>>>> 5. Reboot remote server
>>>>> 6. Test again
>>>>> I can't recall if you need to add Access Permissions in the "COM
>>>>> Security" tab.
>>>>> -----
>>>>> Shay Levi
>>>>> $cript Fanatic
>>>>> http://scriptolog.blogspot.com
>>>>>> Shay,
>>>>>>
>>>>>> Thanks for the help, but I see the namespace
>>>>>> "root\MicrosoftIISv2" operate differently when attempting to
>>>>>> access it on a remote IIS6 computer (default W2K3 installs).
>>>>>>
>>>>>> The code examples you pointed out all work fine when targeting
>>>>>> the namespace locally, but I recieve access denied messages when
>>>>>> useing the same commands remotely.
>>>>>>
>>>>>> I have tried from multiple machines to multiple servers. The VB
>>>>>> Script I supplied still works in all cases, which leads me to
>>>>>> believe I am not setting the PowerShell WMI object authentication
>>>>>> to use pktPrivacy correctly.
>>>>>>
>>>>>> Any advice would be appreciated.
>>>>>>
>>>>>> Thanks
>>>>>> Bill
>>>>>> "Shay Levi" wrote:
>>>>>>> Pinging the remote machine before trying to recycle pools is a
>>>>>>> recommended practice.
>>>>>>>
>>>>>>> Also, I wrote:
>>>>>>>
>>>>>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
>>>>>>> -computer $computer | % {$_.name}
>>>>>>>
>>>>>>> for those who are not familiar with gwmi and %, I should say
>>>>>>> that
>>>>>>> they are
>>>>>>> aliases for:
>>>>>>> gwmi = get-wmiobject
>>>>>>> % = foreach-object
>>>>>>> I should have written it like:
>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
>>>>>>> {$_.name}
>>>>>>> -----
>>>>>>> Shay Levi
>>>>>>> $cript Fanatic
>>>>>>> http://scriptolog.blogspot.com
>>>>>>>> If you know the application pool name (the function returns
>>>>>>>> true/false respectively:
>>>>>>>>
>>>>>>>> function Recycle-AppPool{
>>>>>>>> param([string]$server,[string]$appPool)
>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='W
>>>>>>>> 3S
>>>>>>>> V
>>>>>>>> C/
>>>>>>>> A
>>>>>>>> ppPools/$appPool'").recycle();
>>>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>>>> }
>>>>>>>> To get a list of application pool names:
>>>>>>>> $computer = "your Server name/ip"
>>>>>>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
>>>>>>>> -computer
>>>>>>>> $computer | % {$_.name}
>>>>>>>> -----
>>>>>>>> Shay Levi
>>>>>>>> $cript Fanatic
>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>> I'm having some trouble finding the best method for recycling
>>>>>>>>> remote IIS6 application pools using PowerShell.
>>>>>>>>>
>>>>>>>>> In the past this has always been very easy using VB script:
>>>>>>>>>
>>>>>>>>> Function PoolRecycle(strServer)
>>>>>>>>>
>>>>>>>>> On Error Resume Next
>>>>>>>>> Err.Clear
>>>>>>>>> Dim objWMIService
>>>>>>>>> Dim colItems
>>>>>>>>> Dim objItem
>>>>>>>>> 'recycle application pool
>>>>>>>>> WScript.Echo ""
>>>>>>>>> Err.Clear
>>>>>>>>> Set objWMIService = GetObject _
>>>>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>>>>>>> & strServer & "\root\microsoftiisv2")
>>>>>>>>> 'connect to WMI
>>>>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>>>>>>> IIsApplicationPool")
>>>>>>>>> 'Step through the objects to be recycled
>>>>>>>>> For each objItem in colItems
>>>>>>>>> objItem.Recycle
>>>>>>>>> If err = 0 Then
>>>>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
>>>>>>>>> PoolRecycle = "success"
>>>>>>>>> Else
>>>>>>>>> WScript.Echo("Unable to recycle the application pool on " &
>>>>>>>>> strServer)
>>>>>>>>> PoolRecycle = "failed"
>>>>>>>>> End If
>>>>>>>>> Next
>>>>>>>>> End Function
>>>>>>>>> I have attempted to duplicate the same thing in PowerShell,
>>>>>>>>> but
>>>>>>>>> I
>>>>>>>>> believe I am still running into an issue with the pktPrivacy
>>>>>>>>> authentication setting. The following code works when
>>>>>>>>> executing
>>>>>>>>> localy, but not on a remote host:
>>>>>>>>> Function PoolRecycle($strServerName)
>>>>>>>>> {
>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
>>>>>>>>> "\root\microsoftiisv2"
>>>>>>>>> $objWMI.Scope.Options.Authentication = 6
>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>> foreach ($pool in $pools)
>>>>>>>>> {
>>>>>>>>> $pool.recycle()
>>>>>>>>> if (!$?)
>>>>>>>>> {
>>>>>>>>> Write-Host $pool.name " - ERROR"
>>>>>>>>> }
>>>>>>>>> else
>>>>>>>>> {
>>>>>>>>> Write-Host $pool.name " - Recycled"
>>>>>>>>> }
>>>>>>>>> }
>>>>>>>>> }
>>>>>>>>> Any help or better ways of accomplishing this in PowerShell
>>>>>>>>> would
>>>>>>>>> be
>>>>>>>>> very much appreciated!
>>>>>>>>> Thanks
>>>>>>>>> Bill
 

My Computer

S

Shay Levi

#11
Bill

Try this, I get no errors on my machines.


$server="server"
$objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
$objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
$objWMI.Scope.Options.Authentication = [System.Management.AuthenticationLevel]::PacketPrivacy;
$objWMI.Scope.Options.EnablePrivileges=$true;
$pools = $objWMI.Get()
$pools | foreach {$_.recycle}


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> I'll keep looking for it :-)
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com

>> Hi Shay,
>>
>> Yeah, I can list the pools, but only after specifing the
>> authentication setting.
>>
>> It's almost like it does not carry to the other objects.
>>
>> I appreciate the help.
>>
>> Thanks
>> Bill
>> "Shay Levi" wrote:

>>> Bill
>>>
>>> Just to clarify, I'm not able to recycle remote app pools, just list
>>> their
>>> names (Access denied).
>>> Even adding full security on IIsApplicationPool (WMI Properties)
>>> didn't resolve
>>> it.
>>> I'm still on it...
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> Are you able to list the Application poll names?
>>>>
>>>> $server="server"
>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>> $objWMI.Scope.Options.Authentication =
>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
>>>> $pools = $objWMI.Get()
>>>> $pools | foreach { $_.name }
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>>> Hi Shay,
>>>>>
>>>>> I have tested this and it still does not resolve the issue.
>>>>>
>>>>> I guess I must be missing something. The fact that the VB Script
>>>>> works and the PowerShell does not, leads me to believe that I have
>>>>> an issue with my script and not the security on the target server.
>>>>>
>>>>> I may have to use PowerShell to call the actual .Net class instead
>>>>> of using the built in commands.
>>>>>
>>>>> Funny, I would have never believed that something as simple as
>>>>> recycling a remote application pool would be so hard :)
>>>>>
>>>>> Thanks again for the help.
>>>>> Bill
>>>>> "Shay Levi" wrote:
>>>>>> Bill
>>>>>>
>>>>>> I can't test it right now. Try this on the remote server(s:)
>>>>>>
>>>>>> 1. Start > Run > dcomcnfg > ENTER
>>>>>> 2. In the left pane (Compnent Services.msc) expand "Component
>>>>>> Services" >
>>>>>> Computers
>>>>>> 3. Right click "My Computer"
>>>>>> 4. In the "Default Properties" tab tick the "Enable COM Internet
>>>>>> Services
>>>>>> on this computer" checkbox
>>>>>> 5. Reboot remote server
>>>>>> 6. Test again
>>>>>> I can't recall if you need to add Access Permissions in the "COM
>>>>>> Security" tab.
>>>>>> -----
>>>>>> Shay Levi
>>>>>> $cript Fanatic
>>>>>> http://scriptolog.blogspot.com
>>>>>>> Shay,
>>>>>>>
>>>>>>> Thanks for the help, but I see the namespace
>>>>>>> "root\MicrosoftIISv2" operate differently when attempting to
>>>>>>> access it on a remote IIS6 computer (default W2K3 installs).
>>>>>>>
>>>>>>> The code examples you pointed out all work fine when targeting
>>>>>>> the namespace locally, but I recieve access denied messages when
>>>>>>> useing the same commands remotely.
>>>>>>>
>>>>>>> I have tried from multiple machines to multiple servers. The VB
>>>>>>> Script I supplied still works in all cases, which leads me to
>>>>>>> believe I am not setting the PowerShell WMI object
>>>>>>> authentication to use pktPrivacy correctly.
>>>>>>>
>>>>>>> Any advice would be appreciated.
>>>>>>>
>>>>>>> Thanks
>>>>>>> Bill
>>>>>>> "Shay Levi" wrote:
>>>>>>>> Pinging the remote machine before trying to recycle pools is a
>>>>>>>> recommended practice.
>>>>>>>>
>>>>>>>> Also, I wrote:
>>>>>>>>
>>>>>>>> gwmi -class IIsApplicationPool -namespace "root\MicrosoftIISv2"
>>>>>>>> -computer $computer | % {$_.name}
>>>>>>>>
>>>>>>>> for those who are not familiar with gwmi and %, I should say
>>>>>>>> that
>>>>>>>> they are
>>>>>>>> aliases for:
>>>>>>>> gwmi = get-wmiobject
>>>>>>>> % = foreach-object
>>>>>>>> I should have written it like:
>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
>>>>>>>> {$_.name}
>>>>>>>> -----
>>>>>>>> Shay Levi
>>>>>>>> $cript Fanatic
>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>> If you know the application pool name (the function returns
>>>>>>>>> true/false respectively:
>>>>>>>>>
>>>>>>>>> function Recycle-AppPool{
>>>>>>>>> param([string]$server,[string]$appPool)
>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name='
>>>>>>>>> W
>>>>>>>>> 3S
>>>>>>>>> V
>>>>>>>>> C/
>>>>>>>>> A
>>>>>>>>> ppPools/$appPool'").recycle();
>>>>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>>>>> }
>>>>>>>>> To get a list of application pool names:
>>>>>>>>> $computer = "your Server name/ip"
>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>> "root\MicrosoftIISv2"
>>>>>>>>> -computer
>>>>>>>>> $computer | % {$_.name}
>>>>>>>>> -----
>>>>>>>>> Shay Levi
>>>>>>>>> $cript Fanatic
>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>> I'm having some trouble finding the best method for recycling
>>>>>>>>>> remote IIS6 application pools using PowerShell.
>>>>>>>>>>
>>>>>>>>>> In the past this has always been very easy using VB script:
>>>>>>>>>>
>>>>>>>>>> Function PoolRecycle(strServer)
>>>>>>>>>>
>>>>>>>>>> On Error Resume Next
>>>>>>>>>> Err.Clear
>>>>>>>>>> Dim objWMIService
>>>>>>>>>> Dim colItems
>>>>>>>>>> Dim objItem
>>>>>>>>>> 'recycle application pool
>>>>>>>>>> WScript.Echo ""
>>>>>>>>>> Err.Clear
>>>>>>>>>> Set objWMIService = GetObject _
>>>>>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>>>>>>>> & strServer & "\root\microsoftiisv2")
>>>>>>>>>> 'connect to WMI
>>>>>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>>>>>>>> IIsApplicationPool")
>>>>>>>>>> 'Step through the objects to be recycled
>>>>>>>>>> For each objItem in colItems
>>>>>>>>>> objItem.Recycle
>>>>>>>>>> If err = 0 Then
>>>>>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
>>>>>>>>>> PoolRecycle = "success"
>>>>>>>>>> Else
>>>>>>>>>> WScript.Echo("Unable to recycle the application pool on " &
>>>>>>>>>> strServer)
>>>>>>>>>> PoolRecycle = "failed"
>>>>>>>>>> End If
>>>>>>>>>> Next
>>>>>>>>>> End Function
>>>>>>>>>> I have attempted to duplicate the same thing in PowerShell,
>>>>>>>>>> but
>>>>>>>>>> I
>>>>>>>>>> believe I am still running into an issue with the pktPrivacy
>>>>>>>>>> authentication setting. The following code works when
>>>>>>>>>> executing
>>>>>>>>>> localy, but not on a remote host:
>>>>>>>>>> Function PoolRecycle($strServerName)
>>>>>>>>>> {
>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
>>>>>>>>>> "\root\microsoftiisv2"
>>>>>>>>>> $objWMI.Scope.Options.Authentication = 6
>>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>>> foreach ($pool in $pools)
>>>>>>>>>> {
>>>>>>>>>> $pool.recycle()
>>>>>>>>>> if (!$?)
>>>>>>>>>> {
>>>>>>>>>> Write-Host $pool.name " - ERROR"
>>>>>>>>>> }
>>>>>>>>>> else
>>>>>>>>>> {
>>>>>>>>>> Write-Host $pool.name " - Recycled"
>>>>>>>>>> }
>>>>>>>>>> }
>>>>>>>>>> }
>>>>>>>>>> Any help or better ways of accomplishing this in PowerShell
>>>>>>>>>> would
>>>>>>>>>> be
>>>>>>>>>> very much appreciated!
>>>>>>>>>> Thanks
>>>>>>>>>> Bill
 

My Computer

S

Shay Levi

#12
You can even connect with alternate credentials:

$wmi.Scope.Options.Username="domain\user"
$wmi.Scope.Options.Password="yourPassword"


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> Bill
>
> Try this, I get no errors on my machines.
>
> $server="server"
> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
> $objWMI.Scope.Options.Authentication =
> [System.Management.AuthenticationLevel]::PacketPrivacy;
> $objWMI.Scope.Options.EnablePrivileges=$true;
> $pools = $objWMI.Get()
> $pools | foreach {$_.recycle}
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com

>> I'll keep looking for it :-)
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com

>>> Hi Shay,
>>>
>>> Yeah, I can list the pools, but only after specifing the
>>> authentication setting.
>>>
>>> It's almost like it does not carry to the other objects.
>>>
>>> I appreciate the help.
>>>
>>> Thanks
>>> Bill
>>> "Shay Levi" wrote:
>>>> Bill
>>>>
>>>> Just to clarify, I'm not able to recycle remote app pools, just
>>>> list
>>>> their
>>>> names (Access denied).
>>>> Even adding full security on IIsApplicationPool (WMI Properties)
>>>> didn't resolve
>>>> it.
>>>> I'm still on it...
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>>> Are you able to list the Application poll names?
>>>>>
>>>>> $server="server"
>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>> $objWMI.Scope.Options.Authentication =
>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
>>>>> $pools = $objWMI.Get()
>>>>> $pools | foreach { $_.name }
>>>>> -----
>>>>> Shay Levi
>>>>> $cript Fanatic
>>>>> http://scriptolog.blogspot.com
>>>>>> Hi Shay,
>>>>>>
>>>>>> I have tested this and it still does not resolve the issue.
>>>>>>
>>>>>> I guess I must be missing something. The fact that the VB Script
>>>>>> works and the PowerShell does not, leads me to believe that I
>>>>>> have an issue with my script and not the security on the target
>>>>>> server.
>>>>>>
>>>>>> I may have to use PowerShell to call the actual .Net class
>>>>>> instead of using the built in commands.
>>>>>>
>>>>>> Funny, I would have never believed that something as simple as
>>>>>> recycling a remote application pool would be so hard :)
>>>>>>
>>>>>> Thanks again for the help.
>>>>>> Bill
>>>>>> "Shay Levi" wrote:
>>>>>>> Bill
>>>>>>>
>>>>>>> I can't test it right now. Try this on the remote server(s:)
>>>>>>>
>>>>>>> 1. Start > Run > dcomcnfg > ENTER
>>>>>>> 2. In the left pane (Compnent Services.msc) expand "Component
>>>>>>> Services" >
>>>>>>> Computers
>>>>>>> 3. Right click "My Computer"
>>>>>>> 4. In the "Default Properties" tab tick the "Enable COM Internet
>>>>>>> Services
>>>>>>> on this computer" checkbox
>>>>>>> 5. Reboot remote server
>>>>>>> 6. Test again
>>>>>>> I can't recall if you need to add Access Permissions in the "COM
>>>>>>> Security" tab.
>>>>>>> -----
>>>>>>> Shay Levi
>>>>>>> $cript Fanatic
>>>>>>> http://scriptolog.blogspot.com
>>>>>>>> Shay,
>>>>>>>>
>>>>>>>> Thanks for the help, but I see the namespace
>>>>>>>> "root\MicrosoftIISv2" operate differently when attempting to
>>>>>>>> access it on a remote IIS6 computer (default W2K3 installs).
>>>>>>>>
>>>>>>>> The code examples you pointed out all work fine when targeting
>>>>>>>> the namespace locally, but I recieve access denied messages
>>>>>>>> when useing the same commands remotely.
>>>>>>>>
>>>>>>>> I have tried from multiple machines to multiple servers. The
>>>>>>>> VB Script I supplied still works in all cases, which leads me
>>>>>>>> to believe I am not setting the PowerShell WMI object
>>>>>>>> authentication to use pktPrivacy correctly.
>>>>>>>>
>>>>>>>> Any advice would be appreciated.
>>>>>>>>
>>>>>>>> Thanks
>>>>>>>> Bill
>>>>>>>> "Shay Levi" wrote:
>>>>>>>>> Pinging the remote machine before trying to recycle pools is a
>>>>>>>>> recommended practice.
>>>>>>>>>
>>>>>>>>> Also, I wrote:
>>>>>>>>>
>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | % {$_.name}
>>>>>>>>>
>>>>>>>>> for those who are not familiar with gwmi and %, I should say
>>>>>>>>> that
>>>>>>>>> they are
>>>>>>>>> aliases for:
>>>>>>>>> gwmi = get-wmiobject
>>>>>>>>> % = foreach-object
>>>>>>>>> I should have written it like:
>>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
>>>>>>>>> {$_.name}
>>>>>>>>> -----
>>>>>>>>> Shay Levi
>>>>>>>>> $cript Fanatic
>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>> If you know the application pool name (the function returns
>>>>>>>>>> true/false respectively:
>>>>>>>>>>
>>>>>>>>>> function Recycle-AppPool{
>>>>>>>>>> param([string]$server,[string]$appPool)
>>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name=
>>>>>>>>>> '
>>>>>>>>>> W
>>>>>>>>>> 3S
>>>>>>>>>> V
>>>>>>>>>> C/
>>>>>>>>>> A
>>>>>>>>>> ppPools/$appPool'").recycle();
>>>>>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>>>>>> }
>>>>>>>>>> To get a list of application pool names:
>>>>>>>>>> $computer = "your Server name/ip"
>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>> "root\MicrosoftIISv2"
>>>>>>>>>> -computer
>>>>>>>>>> $computer | % {$_.name}
>>>>>>>>>> -----
>>>>>>>>>> Shay Levi
>>>>>>>>>> $cript Fanatic
>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>> I'm having some trouble finding the best method for
>>>>>>>>>>> recycling remote IIS6 application pools using PowerShell.
>>>>>>>>>>>
>>>>>>>>>>> In the past this has always been very easy using VB script:
>>>>>>>>>>>
>>>>>>>>>>> Function PoolRecycle(strServer)
>>>>>>>>>>>
>>>>>>>>>>> On Error Resume Next
>>>>>>>>>>> Err.Clear
>>>>>>>>>>> Dim objWMIService
>>>>>>>>>>> Dim colItems
>>>>>>>>>>> Dim objItem
>>>>>>>>>>> 'recycle application pool
>>>>>>>>>>> WScript.Echo ""
>>>>>>>>>>> Err.Clear
>>>>>>>>>>> Set objWMIService = GetObject _
>>>>>>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>>>>>>>>> & strServer & "\root\microsoftiisv2")
>>>>>>>>>>> 'connect to WMI
>>>>>>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>>>>>>>>> IIsApplicationPool")
>>>>>>>>>>> 'Step through the objects to be recycled
>>>>>>>>>>> For each objItem in colItems
>>>>>>>>>>> objItem.Recycle
>>>>>>>>>>> If err = 0 Then
>>>>>>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
>>>>>>>>>>> PoolRecycle = "success"
>>>>>>>>>>> Else
>>>>>>>>>>> WScript.Echo("Unable to recycle the application pool on " &
>>>>>>>>>>> strServer)
>>>>>>>>>>> PoolRecycle = "failed"
>>>>>>>>>>> End If
>>>>>>>>>>> Next
>>>>>>>>>>> End Function
>>>>>>>>>>> I have attempted to duplicate the same thing in PowerShell,
>>>>>>>>>>> but
>>>>>>>>>>> I
>>>>>>>>>>> believe I am still running into an issue with the pktPrivacy
>>>>>>>>>>> authentication setting. The following code works when
>>>>>>>>>>> executing
>>>>>>>>>>> localy, but not on a remote host:
>>>>>>>>>>> Function PoolRecycle($strServerName)
>>>>>>>>>>> {
>>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
>>>>>>>>>>> "\root\microsoftiisv2"
>>>>>>>>>>> $objWMI.Scope.Options.Authentication = 6
>>>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>>>> foreach ($pool in $pools)
>>>>>>>>>>> {
>>>>>>>>>>> $pool.recycle()
>>>>>>>>>>> if (!$?)
>>>>>>>>>>> {
>>>>>>>>>>> Write-Host $pool.name " - ERROR"
>>>>>>>>>>> }
>>>>>>>>>>> else
>>>>>>>>>>> {
>>>>>>>>>>> Write-Host $pool.name " - Recycled"
>>>>>>>>>>> }
>>>>>>>>>>> }
>>>>>>>>>>> }
>>>>>>>>>>> Any help or better ways of accomplishing this in PowerShell
>>>>>>>>>>> would
>>>>>>>>>>> be
>>>>>>>>>>> very much appreciated!
>>>>>>>>>>> Thanks
>>>>>>>>>>> Bill
 

My Computer

S

Shay Levi

#13
I came up with another working version:

$server="server"

$co = new-object System.Management.ConnectionOptions;
$co.Username="domain\username";
$co.Password="password";
$co.Authentication=[System.Management.AuthenticationLevel]::PacketPrivacy;
$co.EnablePrivileges=$true;

$wmi = New-Object System.Management.ManagementObjectSearcher;
$wmi.Query="Select * From IIsApplicationPool";
$wmi.Scope.Path="\\$server\root\microsoftiisv2";
$wmi.Scope.Options=$co;

$wmi.Get() | foreach {
$_.recycle;
if($?){
$_.name + " recycled";
} else {
$_.name + " error";
}
}


I hope it solves your problem.

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> You can even connect with alternate credentials:
>
> $wmi.Scope.Options.Username="domain\user"
> $wmi.Scope.Options.Password="yourPassword"
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com

>> Bill
>>
>> Try this, I get no errors on my machines.
>>
>> $server="server"
>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>> $objWMI.Scope.Options.Authentication =
>> [System.Management.AuthenticationLevel]::PacketPrivacy;
>> $objWMI.Scope.Options.EnablePrivileges=$true;
>> $pools = $objWMI.Get()
>> $pools | foreach {$_.recycle}
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com

>>> I'll keep looking for it :-)
>>>
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> Hi Shay,
>>>>
>>>> Yeah, I can list the pools, but only after specifing the
>>>> authentication setting.
>>>>
>>>> It's almost like it does not carry to the other objects.
>>>>
>>>> I appreciate the help.
>>>>
>>>> Thanks
>>>> Bill
>>>> "Shay Levi" wrote:
>>>>> Bill
>>>>>
>>>>> Just to clarify, I'm not able to recycle remote app pools, just
>>>>> list
>>>>> their
>>>>> names (Access denied).
>>>>> Even adding full security on IIsApplicationPool (WMI Properties)
>>>>> didn't resolve
>>>>> it.
>>>>> I'm still on it...
>>>>> -----
>>>>> Shay Levi
>>>>> $cript Fanatic
>>>>> http://scriptolog.blogspot.com
>>>>>> Are you able to list the Application poll names?
>>>>>>
>>>>>> $server="server"
>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>>> $objWMI.Scope.Options.Authentication =
>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
>>>>>> $pools = $objWMI.Get()
>>>>>> $pools | foreach { $_.name }
>>>>>> -----
>>>>>> Shay Levi
>>>>>> $cript Fanatic
>>>>>> http://scriptolog.blogspot.com
>>>>>>> Hi Shay,
>>>>>>>
>>>>>>> I have tested this and it still does not resolve the issue.
>>>>>>>
>>>>>>> I guess I must be missing something. The fact that the VB
>>>>>>> Script works and the PowerShell does not, leads me to believe
>>>>>>> that I have an issue with my script and not the security on the
>>>>>>> target server.
>>>>>>>
>>>>>>> I may have to use PowerShell to call the actual .Net class
>>>>>>> instead of using the built in commands.
>>>>>>>
>>>>>>> Funny, I would have never believed that something as simple as
>>>>>>> recycling a remote application pool would be so hard :)
>>>>>>>
>>>>>>> Thanks again for the help.
>>>>>>> Bill
>>>>>>> "Shay Levi" wrote:
>>>>>>>> Bill
>>>>>>>>
>>>>>>>> I can't test it right now. Try this on the remote server(s:)
>>>>>>>>
>>>>>>>> 1. Start > Run > dcomcnfg > ENTER
>>>>>>>> 2. In the left pane (Compnent Services.msc) expand "Component
>>>>>>>> Services" >
>>>>>>>> Computers
>>>>>>>> 3. Right click "My Computer"
>>>>>>>> 4. In the "Default Properties" tab tick the "Enable COM
>>>>>>>> Internet
>>>>>>>> Services
>>>>>>>> on this computer" checkbox
>>>>>>>> 5. Reboot remote server
>>>>>>>> 6. Test again
>>>>>>>> I can't recall if you need to add Access Permissions in the
>>>>>>>> "COM
>>>>>>>> Security" tab.
>>>>>>>> -----
>>>>>>>> Shay Levi
>>>>>>>> $cript Fanatic
>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>> Shay,
>>>>>>>>>
>>>>>>>>> Thanks for the help, but I see the namespace
>>>>>>>>> "root\MicrosoftIISv2" operate differently when attempting to
>>>>>>>>> access it on a remote IIS6 computer (default W2K3 installs).
>>>>>>>>>
>>>>>>>>> The code examples you pointed out all work fine when targeting
>>>>>>>>> the namespace locally, but I recieve access denied messages
>>>>>>>>> when useing the same commands remotely.
>>>>>>>>>
>>>>>>>>> I have tried from multiple machines to multiple servers. The
>>>>>>>>> VB Script I supplied still works in all cases, which leads me
>>>>>>>>> to believe I am not setting the PowerShell WMI object
>>>>>>>>> authentication to use pktPrivacy correctly.
>>>>>>>>>
>>>>>>>>> Any advice would be appreciated.
>>>>>>>>>
>>>>>>>>> Thanks
>>>>>>>>> Bill
>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>> Pinging the remote machine before trying to recycle pools is
>>>>>>>>>> a recommended practice.
>>>>>>>>>>
>>>>>>>>>> Also, I wrote:
>>>>>>>>>>
>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | % {$_.name}
>>>>>>>>>> for those who are not familiar with gwmi and %, I should say
>>>>>>>>>> that
>>>>>>>>>> they are
>>>>>>>>>> aliases for:
>>>>>>>>>> gwmi = get-wmiobject
>>>>>>>>>> % = foreach-object
>>>>>>>>>> I should have written it like:
>>>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
>>>>>>>>>> {$_.name}
>>>>>>>>>> -----
>>>>>>>>>> Shay Levi
>>>>>>>>>> $cript Fanatic
>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>> If you know the application pool name (the function returns
>>>>>>>>>>> true/false respectively:
>>>>>>>>>>>
>>>>>>>>>>> function Recycle-AppPool{
>>>>>>>>>>> param([string]$server,[string]$appPool)
>>>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name
>>>>>>>>>>> =
>>>>>>>>>>> '
>>>>>>>>>>> W
>>>>>>>>>>> 3S
>>>>>>>>>>> V
>>>>>>>>>>> C/
>>>>>>>>>>> A
>>>>>>>>>>> ppPools/$appPool'").recycle();
>>>>>>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>>>>>>> }
>>>>>>>>>>> To get a list of application pool names:
>>>>>>>>>>> $computer = "your Server name/ip"
>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>> "root\MicrosoftIISv2"
>>>>>>>>>>> -computer
>>>>>>>>>>> $computer | % {$_.name}
>>>>>>>>>>> -----
>>>>>>>>>>> Shay Levi
>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>> I'm having some trouble finding the best method for
>>>>>>>>>>>> recycling remote IIS6 application pools using PowerShell.
>>>>>>>>>>>>
>>>>>>>>>>>> In the past this has always been very easy using VB script:
>>>>>>>>>>>>
>>>>>>>>>>>> Function PoolRecycle(strServer)
>>>>>>>>>>>>
>>>>>>>>>>>> On Error Resume Next
>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>> Dim objWMIService
>>>>>>>>>>>> Dim colItems
>>>>>>>>>>>> Dim objItem
>>>>>>>>>>>> 'recycle application pool
>>>>>>>>>>>> WScript.Echo ""
>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>> Set objWMIService = GetObject _
>>>>>>>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>>>>>>>>>> & strServer & "\root\microsoftiisv2")
>>>>>>>>>>>> 'connect to WMI
>>>>>>>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>>>>>>>>>> IIsApplicationPool")
>>>>>>>>>>>> 'Step through the objects to be recycled
>>>>>>>>>>>> For each objItem in colItems
>>>>>>>>>>>> objItem.Recycle
>>>>>>>>>>>> If err = 0 Then
>>>>>>>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & "
>>>>>>>>>>>> Recycled")
>>>>>>>>>>>> PoolRecycle = "success"
>>>>>>>>>>>> Else
>>>>>>>>>>>> WScript.Echo("Unable to recycle the application pool on " &
>>>>>>>>>>>> strServer)
>>>>>>>>>>>> PoolRecycle = "failed"
>>>>>>>>>>>> End If
>>>>>>>>>>>> Next
>>>>>>>>>>>> End Function
>>>>>>>>>>>> I have attempted to duplicate the same thing in PowerShell,
>>>>>>>>>>>> but
>>>>>>>>>>>> I
>>>>>>>>>>>> believe I am still running into an issue with the
>>>>>>>>>>>> pktPrivacy
>>>>>>>>>>>> authentication setting. The following code works when
>>>>>>>>>>>> executing
>>>>>>>>>>>> localy, but not on a remote host:
>>>>>>>>>>>> Function PoolRecycle($strServerName)
>>>>>>>>>>>> {
>>>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
>>>>>>>>>>>> "\root\microsoftiisv2"
>>>>>>>>>>>> $objWMI.Scope.Options.Authentication = 6
>>>>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>>>>> foreach ($pool in $pools)
>>>>>>>>>>>> {
>>>>>>>>>>>> $pool.recycle()
>>>>>>>>>>>> if (!$?)
>>>>>>>>>>>> {
>>>>>>>>>>>> Write-Host $pool.name " - ERROR"
>>>>>>>>>>>> }
>>>>>>>>>>>> else
>>>>>>>>>>>> {
>>>>>>>>>>>> Write-Host $pool.name " - Recycled"
>>>>>>>>>>>> }
>>>>>>>>>>>> }
>>>>>>>>>>>> }
>>>>>>>>>>>> Any help or better ways of accomplishing this in PowerShell
>>>>>>>>>>>> would
>>>>>>>>>>>> be
>>>>>>>>>>>> very much appreciated!
>>>>>>>>>>>> Thanks
>>>>>>>>>>>> Bill
 

My Computer

B
#14
Hi Shay,

This last set of code seems to have come the closest. I no longer get an
access denied error, but the application pools on the remote server don't
actually recycle.

I have gotten some new ideas from these scripts though and I will try some
things.

Thank You for the continued effort.
Bill


"Shay Levi" wrote:

> I came up with another working version:
>
> $server="server"
>
> $co = new-object System.Management.ConnectionOptions;
> $co.Username="domain\username";
> $co.Password="password";
> $co.Authentication=[System.Management.AuthenticationLevel]::PacketPrivacy;
> $co.EnablePrivileges=$true;
>
> $wmi = New-Object System.Management.ManagementObjectSearcher;
> $wmi.Query="Select * From IIsApplicationPool";
> $wmi.Scope.Path="\\$server\root\microsoftiisv2";
> $wmi.Scope.Options=$co;
>
> $wmi.Get() | foreach {
> $_.recycle;
> if($?){
> $_.name + " recycled";
> } else {
> $_.name + " error";
> }
> }
>
>
> I hope it solves your problem.
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
>
>

> > You can even connect with alternate credentials:
> >
> > $wmi.Scope.Options.Username="domain\user"
> > $wmi.Scope.Options.Password="yourPassword"
> > -----
> > Shay Levi
> > $cript Fanatic
> > http://scriptolog.blogspot.com

> >> Bill
> >>
> >> Try this, I get no errors on my machines.
> >>
> >> $server="server"
> >> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> >> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
> >> $objWMI.Scope.Options.Authentication =
> >> [System.Management.AuthenticationLevel]::PacketPrivacy;
> >> $objWMI.Scope.Options.EnablePrivileges=$true;
> >> $pools = $objWMI.Get()
> >> $pools | foreach {$_.recycle}
> >> -----
> >> Shay Levi
> >> $cript Fanatic
> >> http://scriptolog.blogspot.com
> >>> I'll keep looking for it :-)
> >>>
> >>> -----
> >>> Shay Levi
> >>> $cript Fanatic
> >>> http://scriptolog.blogspot.com
> >>>> Hi Shay,
> >>>>
> >>>> Yeah, I can list the pools, but only after specifing the
> >>>> authentication setting.
> >>>>
> >>>> It's almost like it does not carry to the other objects.
> >>>>
> >>>> I appreciate the help.
> >>>>
> >>>> Thanks
> >>>> Bill
> >>>> "Shay Levi" wrote:
> >>>>> Bill
> >>>>>
> >>>>> Just to clarify, I'm not able to recycle remote app pools, just
> >>>>> list
> >>>>> their
> >>>>> names (Access denied).
> >>>>> Even adding full security on IIsApplicationPool (WMI Properties)
> >>>>> didn't resolve
> >>>>> it.
> >>>>> I'm still on it...
> >>>>> -----
> >>>>> Shay Levi
> >>>>> $cript Fanatic
> >>>>> http://scriptolog.blogspot.com
> >>>>>> Are you able to list the Application poll names?
> >>>>>>
> >>>>>> $server="server"
> >>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> >>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
> >>>>>> $objWMI.Scope.Options.Authentication =
> >>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
> >>>>>> $pools = $objWMI.Get()
> >>>>>> $pools | foreach { $_.name }
> >>>>>> -----
> >>>>>> Shay Levi
> >>>>>> $cript Fanatic
> >>>>>> http://scriptolog.blogspot.com
> >>>>>>> Hi Shay,
> >>>>>>>
> >>>>>>> I have tested this and it still does not resolve the issue.
> >>>>>>>
> >>>>>>> I guess I must be missing something. The fact that the VB
> >>>>>>> Script works and the PowerShell does not, leads me to believe
> >>>>>>> that I have an issue with my script and not the security on the
> >>>>>>> target server.
> >>>>>>>
> >>>>>>> I may have to use PowerShell to call the actual .Net class
> >>>>>>> instead of using the built in commands.
> >>>>>>>
> >>>>>>> Funny, I would have never believed that something as simple as
> >>>>>>> recycling a remote application pool would be so hard :)
> >>>>>>>
> >>>>>>> Thanks again for the help.
> >>>>>>> Bill
> >>>>>>> "Shay Levi" wrote:
> >>>>>>>> Bill
> >>>>>>>>
> >>>>>>>> I can't test it right now. Try this on the remote server(s:)
> >>>>>>>>
> >>>>>>>> 1. Start > Run > dcomcnfg > ENTER
> >>>>>>>> 2. In the left pane (Compnent Services.msc) expand "Component
> >>>>>>>> Services" >
> >>>>>>>> Computers
> >>>>>>>> 3. Right click "My Computer"
> >>>>>>>> 4. In the "Default Properties" tab tick the "Enable COM
> >>>>>>>> Internet
> >>>>>>>> Services
> >>>>>>>> on this computer" checkbox
> >>>>>>>> 5. Reboot remote server
> >>>>>>>> 6. Test again
> >>>>>>>> I can't recall if you need to add Access Permissions in the
> >>>>>>>> "COM
> >>>>>>>> Security" tab.
> >>>>>>>> -----
> >>>>>>>> Shay Levi
> >>>>>>>> $cript Fanatic
> >>>>>>>> http://scriptolog.blogspot.com
> >>>>>>>>> Shay,
> >>>>>>>>>
> >>>>>>>>> Thanks for the help, but I see the namespace
> >>>>>>>>> "root\MicrosoftIISv2" operate differently when attempting to
> >>>>>>>>> access it on a remote IIS6 computer (default W2K3 installs).
> >>>>>>>>>
> >>>>>>>>> The code examples you pointed out all work fine when targeting
> >>>>>>>>> the namespace locally, but I recieve access denied messages
> >>>>>>>>> when useing the same commands remotely.
> >>>>>>>>>
> >>>>>>>>> I have tried from multiple machines to multiple servers. The
> >>>>>>>>> VB Script I supplied still works in all cases, which leads me
> >>>>>>>>> to believe I am not setting the PowerShell WMI object
> >>>>>>>>> authentication to use pktPrivacy correctly.
> >>>>>>>>>
> >>>>>>>>> Any advice would be appreciated.
> >>>>>>>>>
> >>>>>>>>> Thanks
> >>>>>>>>> Bill
> >>>>>>>>> "Shay Levi" wrote:
> >>>>>>>>>> Pinging the remote machine before trying to recycle pools is
> >>>>>>>>>> a recommended practice.
> >>>>>>>>>>
> >>>>>>>>>> Also, I wrote:
> >>>>>>>>>>
> >>>>>>>>>> gwmi -class IIsApplicationPool -namespace
> >>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | % {$_.name}
> >>>>>>>>>> for those who are not familiar with gwmi and %, I should say
> >>>>>>>>>> that
> >>>>>>>>>> they are
> >>>>>>>>>> aliases for:
> >>>>>>>>>> gwmi = get-wmiobject
> >>>>>>>>>> % = foreach-object
> >>>>>>>>>> I should have written it like:
> >>>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
> >>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
> >>>>>>>>>> {$_.name}
> >>>>>>>>>> -----
> >>>>>>>>>> Shay Levi
> >>>>>>>>>> $cript Fanatic
> >>>>>>>>>> http://scriptolog.blogspot.com
> >>>>>>>>>>> If you know the application pool name (the function returns
> >>>>>>>>>>> true/false respectively:
> >>>>>>>>>>>
> >>>>>>>>>>> function Recycle-AppPool{
> >>>>>>>>>>> param([string]$server,[string]$appPool)
> >>>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Name
> >>>>>>>>>>> =
> >>>>>>>>>>> '
> >>>>>>>>>>> W
> >>>>>>>>>>> 3S
> >>>>>>>>>>> V
> >>>>>>>>>>> C/
> >>>>>>>>>>> A
> >>>>>>>>>>> ppPools/$appPool'").recycle();
> >>>>>>>>>>> if($? -ne 0}{$false} else {$true}
> >>>>>>>>>>> }
> >>>>>>>>>>> To get a list of application pool names:
> >>>>>>>>>>> $computer = "your Server name/ip"
> >>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
> >>>>>>>>>>> "root\MicrosoftIISv2"
> >>>>>>>>>>> -computer
> >>>>>>>>>>> $computer | % {$_.name}
> >>>>>>>>>>> -----
> >>>>>>>>>>> Shay Levi
> >>>>>>>>>>> $cript Fanatic
> >>>>>>>>>>> http://scriptolog.blogspot.com
> >>>>>>>>>>>> I'm having some trouble finding the best method for
> >>>>>>>>>>>> recycling remote IIS6 application pools using PowerShell.
> >>>>>>>>>>>>
> >>>>>>>>>>>> In the past this has always been very easy using VB script:
> >>>>>>>>>>>>
> >>>>>>>>>>>> Function PoolRecycle(strServer)
> >>>>>>>>>>>>
> >>>>>>>>>>>> On Error Resume Next
> >>>>>>>>>>>> Err.Clear
> >>>>>>>>>>>> Dim objWMIService
> >>>>>>>>>>>> Dim colItems
> >>>>>>>>>>>> Dim objItem
> >>>>>>>>>>>> 'recycle application pool
> >>>>>>>>>>>> WScript.Echo ""
> >>>>>>>>>>>> Err.Clear
> >>>>>>>>>>>> Set objWMIService = GetObject _
> >>>>>>>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
> >>>>>>>>>>>> & strServer & "\root\microsoftiisv2")
> >>>>>>>>>>>> 'connect to WMI
> >>>>>>>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
> >>>>>>>>>>>> IIsApplicationPool")
> >>>>>>>>>>>> 'Step through the objects to be recycled
> >>>>>>>>>>>> For each objItem in colItems
> >>>>>>>>>>>> objItem.Recycle
> >>>>>>>>>>>> If err = 0 Then
> >>>>>>>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & "
> >>>>>>>>>>>> Recycled")
> >>>>>>>>>>>> PoolRecycle = "success"
> >>>>>>>>>>>> Else
> >>>>>>>>>>>> WScript.Echo("Unable to recycle the application pool on " &
> >>>>>>>>>>>> strServer)
> >>>>>>>>>>>> PoolRecycle = "failed"
> >>>>>>>>>>>> End If
> >>>>>>>>>>>> Next
> >>>>>>>>>>>> End Function
> >>>>>>>>>>>> I have attempted to duplicate the same thing in PowerShell,
> >>>>>>>>>>>> but
> >>>>>>>>>>>> I
> >>>>>>>>>>>> believe I am still running into an issue with the
> >>>>>>>>>>>> pktPrivacy
> >>>>>>>>>>>> authentication setting. The following code works when
> >>>>>>>>>>>> executing
> >>>>>>>>>>>> localy, but not on a remote host:
> >>>>>>>>>>>> Function PoolRecycle($strServerName)
> >>>>>>>>>>>> {
> >>>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> >>>>>>>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
> >>>>>>>>>>>> "\root\microsoftiisv2"
> >>>>>>>>>>>> $objWMI.Scope.Options.Authentication = 6
> >>>>>>>>>>>> $pools = $objWMI.Get()
> >>>>>>>>>>>> foreach ($pool in $pools)
> >>>>>>>>>>>> {
> >>>>>>>>>>>> $pool.recycle()
> >>>>>>>>>>>> if (!$?)
> >>>>>>>>>>>> {
> >>>>>>>>>>>> Write-Host $pool.name " - ERROR"
> >>>>>>>>>>>> }
> >>>>>>>>>>>> else
> >>>>>>>>>>>> {
> >>>>>>>>>>>> Write-Host $pool.name " - Recycled"
> >>>>>>>>>>>> }
> >>>>>>>>>>>> }
> >>>>>>>>>>>> }
> >>>>>>>>>>>> Any help or better ways of accomplishing this in PowerShell
> >>>>>>>>>>>> would
> >>>>>>>>>>>> be
> >>>>>>>>>>>> very much appreciated!
> >>>>>>>>>>>> Thanks
> >>>>>>>>>>>> Bill
>
>
>
 

My Computer

S

Shay Levi

#15
Bill

You're right. The pools aren't recycling. PowerShell doesn't return any error
on $_.recycle
and I assumed no errors returned = recycled.
This turned out to be my typo error. I should have write $_.recycle().
Now its givin me the ACCESS_DENIED again.

I'll check on it some more and see what I can find.

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> Hi Shay,
>
> This last set of code seems to have come the closest. I no longer get
> an access denied error, but the application pools on the remote server
> don't actually recycle.
>
> I have gotten some new ideas from these scripts though and I will try
> some things.
>
> Thank You for the continued effort.
> Bill
> "Shay Levi" wrote:
>

>> I came up with another working version:
>>
>> $server="server"
>>
>> $co = new-object System.Management.ConnectionOptions;
>> $co.Username="domain\username";
>> $co.Password="password";
>> $co.Authentication=[System.Management.AuthenticationLevel]::PacketPri
>> vacy;
>> $co.EnablePrivileges=$true;
>> $wmi = New-Object System.Management.ManagementObjectSearcher;
>> $wmi.Query="Select * From IIsApplicationPool";
>> $wmi.Scope.Path="\\$server\root\microsoftiisv2";
>> $wmi.Scope.Options=$co;
>>
>> $wmi.Get() | foreach {
>> $_.recycle;
>> if($?){
>> $_.name + " recycled";
>> } else {
>> $_.name + " error";
>> }
>> }
>> I hope it solves your problem.
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com

>>> You can even connect with alternate credentials:
>>>
>>> $wmi.Scope.Options.Username="domain\user"
>>> $wmi.Scope.Options.Password="yourPassword"
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> Bill
>>>>
>>>> Try this, I get no errors on my machines.
>>>>
>>>> $server="server"
>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>> $objWMI.Scope.Options.Authentication =
>>>> [System.Management.AuthenticationLevel]::PacketPrivacy;
>>>> $objWMI.Scope.Options.EnablePrivileges=$true;
>>>> $pools = $objWMI.Get()
>>>> $pools | foreach {$_.recycle}
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>>> I'll keep looking for it :-)
>>>>>
>>>>> -----
>>>>> Shay Levi
>>>>> $cript Fanatic
>>>>> http://scriptolog.blogspot.com
>>>>>> Hi Shay,
>>>>>>
>>>>>> Yeah, I can list the pools, but only after specifing the
>>>>>> authentication setting.
>>>>>>
>>>>>> It's almost like it does not carry to the other objects.
>>>>>>
>>>>>> I appreciate the help.
>>>>>>
>>>>>> Thanks
>>>>>> Bill
>>>>>> "Shay Levi" wrote:
>>>>>>> Bill
>>>>>>>
>>>>>>> Just to clarify, I'm not able to recycle remote app pools, just
>>>>>>> list
>>>>>>> their
>>>>>>> names (Access denied).
>>>>>>> Even adding full security on IIsApplicationPool (WMI Properties)
>>>>>>> didn't resolve
>>>>>>> it.
>>>>>>> I'm still on it...
>>>>>>> -----
>>>>>>> Shay Levi
>>>>>>> $cript Fanatic
>>>>>>> http://scriptolog.blogspot.com
>>>>>>>> Are you able to list the Application poll names?
>>>>>>>>
>>>>>>>> $server="server"
>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>>>>> $objWMI.Scope.Options.Authentication =
>>>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
>>>>>>>> $pools = $objWMI.Get()
>>>>>>>> $pools | foreach { $_.name }
>>>>>>>> -----
>>>>>>>> Shay Levi
>>>>>>>> $cript Fanatic
>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>> Hi Shay,
>>>>>>>>>
>>>>>>>>> I have tested this and it still does not resolve the issue.
>>>>>>>>>
>>>>>>>>> I guess I must be missing something. The fact that the VB
>>>>>>>>> Script works and the PowerShell does not, leads me to believe
>>>>>>>>> that I have an issue with my script and not the security on
>>>>>>>>> the target server.
>>>>>>>>>
>>>>>>>>> I may have to use PowerShell to call the actual .Net class
>>>>>>>>> instead of using the built in commands.
>>>>>>>>>
>>>>>>>>> Funny, I would have never believed that something as simple as
>>>>>>>>> recycling a remote application pool would be so hard :)
>>>>>>>>>
>>>>>>>>> Thanks again for the help.
>>>>>>>>> Bill
>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>> Bill
>>>>>>>>>>
>>>>>>>>>> I can't test it right now. Try this on the remote server(s:)
>>>>>>>>>>
>>>>>>>>>> 1. Start > Run > dcomcnfg > ENTER
>>>>>>>>>> 2. In the left pane (Compnent Services.msc) expand "Component
>>>>>>>>>> Services" >
>>>>>>>>>> Computers
>>>>>>>>>> 3. Right click "My Computer"
>>>>>>>>>> 4. In the "Default Properties" tab tick the "Enable COM
>>>>>>>>>> Internet
>>>>>>>>>> Services
>>>>>>>>>> on this computer" checkbox
>>>>>>>>>> 5. Reboot remote server
>>>>>>>>>> 6. Test again
>>>>>>>>>> I can't recall if you need to add Access Permissions in the
>>>>>>>>>> "COM
>>>>>>>>>> Security" tab.
>>>>>>>>>> -----
>>>>>>>>>> Shay Levi
>>>>>>>>>> $cript Fanatic
>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>> Shay,
>>>>>>>>>>>
>>>>>>>>>>> Thanks for the help, but I see the namespace
>>>>>>>>>>> "root\MicrosoftIISv2" operate differently when attempting to
>>>>>>>>>>> access it on a remote IIS6 computer (default W2K3 installs).
>>>>>>>>>>>
>>>>>>>>>>> The code examples you pointed out all work fine when
>>>>>>>>>>> targeting the namespace locally, but I recieve access denied
>>>>>>>>>>> messages when useing the same commands remotely.
>>>>>>>>>>>
>>>>>>>>>>> I have tried from multiple machines to multiple servers.
>>>>>>>>>>> The VB Script I supplied still works in all cases, which
>>>>>>>>>>> leads me to believe I am not setting the PowerShell WMI
>>>>>>>>>>> object authentication to use pktPrivacy correctly.
>>>>>>>>>>>
>>>>>>>>>>> Any advice would be appreciated.
>>>>>>>>>>>
>>>>>>>>>>> Thanks
>>>>>>>>>>> Bill
>>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>>> Pinging the remote machine before trying to recycle pools
>>>>>>>>>>>> is a recommended practice.
>>>>>>>>>>>>
>>>>>>>>>>>> Also, I wrote:
>>>>>>>>>>>>
>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | % {$_.name}
>>>>>>>>>>>> for those who are not familiar with gwmi and %, I should
>>>>>>>>>>>> say
>>>>>>>>>>>> that
>>>>>>>>>>>> they are
>>>>>>>>>>>> aliases for:
>>>>>>>>>>>> gwmi = get-wmiobject
>>>>>>>>>>>> % = foreach-object
>>>>>>>>>>>> I should have written it like:
>>>>>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
>>>>>>>>>>>> {$_.name}
>>>>>>>>>>>> -----
>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>> If you know the application pool name (the function
>>>>>>>>>>>>> returns true/false respectively:
>>>>>>>>>>>>>
>>>>>>>>>>>>> function Recycle-AppPool{
>>>>>>>>>>>>> param([string]$server,[string]$appPool)
>>>>>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.Na
>>>>>>>>>>>>> me
>>>>>>>>>>>>> =
>>>>>>>>>>>>> '
>>>>>>>>>>>>> W
>>>>>>>>>>>>> 3S
>>>>>>>>>>>>> V
>>>>>>>>>>>>> C/
>>>>>>>>>>>>> A
>>>>>>>>>>>>> ppPools/$appPool'").recycle();
>>>>>>>>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>>>>>>>>> }
>>>>>>>>>>>>> To get a list of application pool names:
>>>>>>>>>>>>> $computer = "your Server name/ip"
>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>>> "root\MicrosoftIISv2"
>>>>>>>>>>>>> -computer
>>>>>>>>>>>>> $computer | % {$_.name}
>>>>>>>>>>>>> -----
>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>> I'm having some trouble finding the best method for
>>>>>>>>>>>>>> recycling remote IIS6 application pools using PowerShell.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> In the past this has always been very easy using VB
>>>>>>>>>>>>>> script:
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Function PoolRecycle(strServer)
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> On Error Resume Next
>>>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>>>> Dim objWMIService
>>>>>>>>>>>>>> Dim colItems
>>>>>>>>>>>>>> Dim objItem
>>>>>>>>>>>>>> 'recycle application pool
>>>>>>>>>>>>>> WScript.Echo ""
>>>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>>>> Set objWMIService = GetObject _
>>>>>>>>>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>>>>>>>>>>>> & strServer & "\root\microsoftiisv2")
>>>>>>>>>>>>>> 'connect to WMI
>>>>>>>>>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>>>>>>>>>>>> IIsApplicationPool")
>>>>>>>>>>>>>> 'Step through the objects to be recycled
>>>>>>>>>>>>>> For each objItem in colItems
>>>>>>>>>>>>>> objItem.Recycle
>>>>>>>>>>>>>> If err = 0 Then
>>>>>>>>>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & "
>>>>>>>>>>>>>> Recycled")
>>>>>>>>>>>>>> PoolRecycle = "success"
>>>>>>>>>>>>>> Else
>>>>>>>>>>>>>> WScript.Echo("Unable to recycle the application pool on "
>>>>>>>>>>>>>> &
>>>>>>>>>>>>>> strServer)
>>>>>>>>>>>>>> PoolRecycle = "failed"
>>>>>>>>>>>>>> End If
>>>>>>>>>>>>>> Next
>>>>>>>>>>>>>> End Function
>>>>>>>>>>>>>> I have attempted to duplicate the same thing in
>>>>>>>>>>>>>> PowerShell,
>>>>>>>>>>>>>> but
>>>>>>>>>>>>>> I
>>>>>>>>>>>>>> believe I am still running into an issue with the
>>>>>>>>>>>>>> pktPrivacy
>>>>>>>>>>>>>> authentication setting. The following code works when
>>>>>>>>>>>>>> executing
>>>>>>>>>>>>>> localy, but not on a remote host:
>>>>>>>>>>>>>> Function PoolRecycle($strServerName)
>>>>>>>>>>>>>> {
>>>>>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From
>>>>>>>>>>>>>> IIsApplicationPool"
>>>>>>>>>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
>>>>>>>>>>>>>> "\root\microsoftiisv2"
>>>>>>>>>>>>>> $objWMI.Scope.Options.Authentication = 6
>>>>>>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>>>>>>> foreach ($pool in $pools)
>>>>>>>>>>>>>> {
>>>>>>>>>>>>>> $pool.recycle()
>>>>>>>>>>>>>> if (!$?)
>>>>>>>>>>>>>> {
>>>>>>>>>>>>>> Write-Host $pool.name " - ERROR"
>>>>>>>>>>>>>> }
>>>>>>>>>>>>>> else
>>>>>>>>>>>>>> {
>>>>>>>>>>>>>> Write-Host $pool.name " - Recycled"
>>>>>>>>>>>>>> }
>>>>>>>>>>>>>> }
>>>>>>>>>>>>>> }
>>>>>>>>>>>>>> Any help or better ways of accomplishing this in
>>>>>>>>>>>>>> PowerShell
>>>>>>>>>>>>>> would
>>>>>>>>>>>>>> be
>>>>>>>>>>>>>> very much appreciated!
>>>>>>>>>>>>>> Thanks
>>>>>>>>>>>>>> Bill
 

My Computer

S

Shay Levi

#16
I monitored the remote server's WMI log file and found this (truncated) while
executing the code:

CALL ConnectionLogin::NTLMLogin
wszNetworkResource = \\server\root\MicrosoftIISv2
pPreferredLocale =
lFlags = 0x0
DCOM connection from DOMAIN\ShayL at authentiction level Packet, AuthnSvc
= 9, AuthzSvc = 1, Capabilities = 0
CALL CWbemNamespace::ExecQuery
BSTR QueryFormat = WQL
BSTR Query = select * from IIsApplicationPool
IEnumWbemClassObject **pEnum = 0xB9130
Access to the root\MicrosoftIISv2 namespace was denied. The namespace is
marked with RequiresEncryption but the client connection was attempted with
an authentication level below Pkt_Privacy.Re try the connection using Pkt_Privacy
authentication level.


Odd... the connection AuthenticationLevel was specified to PacketPrivacy
$co.Authentication=[System.Management.AuthenticationLevel]::PacketPrivacy;

Why do I still get access denied?



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> Bill
>
> You're right. The pools aren't recycling. PowerShell doesn't return
> any error
> on $_.recycle
> and I assumed no errors returned = recycled.
> This turned out to be my typo error. I should have write $_.recycle().
> Now its givin me the ACCESS_DENIED again.
> I'll check on it some more and see what I can find.
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com

>> Hi Shay,
>>
>> This last set of code seems to have come the closest. I no longer
>> get an access denied error, but the application pools on the remote
>> server don't actually recycle.
>>
>> I have gotten some new ideas from these scripts though and I will try
>> some things.
>>
>> Thank You for the continued effort.
>> Bill
>> "Shay Levi" wrote:

>>> I came up with another working version:
>>>
>>> $server="server"
>>>
>>> $co = new-object System.Management.ConnectionOptions;
>>> $co.Username="domain\username";
>>> $co.Password="password";
>>> $co.Authentication=[System.Management.AuthenticationLevel]::PacketPr
>>> i
>>> vacy;
>>> $co.EnablePrivileges=$true;
>>> $wmi = New-Object System.Management.ManagementObjectSearcher;
>>> $wmi.Query="Select * From IIsApplicationPool";
>>> $wmi.Scope.Path="\\$server\root\microsoftiisv2";
>>> $wmi.Scope.Options=$co;
>>> $wmi.Get() | foreach {
>>> $_.recycle;
>>> if($?){
>>> $_.name + " recycled";
>>> } else {
>>> $_.name + " error";
>>> }
>>> }
>>> I hope it solves your problem.
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> You can even connect with alternate credentials:
>>>>
>>>> $wmi.Scope.Options.Username="domain\user"
>>>> $wmi.Scope.Options.Password="yourPassword"
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>>> Bill
>>>>>
>>>>> Try this, I get no errors on my machines.
>>>>>
>>>>> $server="server"
>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>> $objWMI.Scope.Options.Authentication =
>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy;
>>>>> $objWMI.Scope.Options.EnablePrivileges=$true;
>>>>> $pools = $objWMI.Get()
>>>>> $pools | foreach {$_.recycle}
>>>>> -----
>>>>> Shay Levi
>>>>> $cript Fanatic
>>>>> http://scriptolog.blogspot.com
>>>>>> I'll keep looking for it :-)
>>>>>>
>>>>>> -----
>>>>>> Shay Levi
>>>>>> $cript Fanatic
>>>>>> http://scriptolog.blogspot.com
>>>>>>> Hi Shay,
>>>>>>>
>>>>>>> Yeah, I can list the pools, but only after specifing the
>>>>>>> authentication setting.
>>>>>>>
>>>>>>> It's almost like it does not carry to the other objects.
>>>>>>>
>>>>>>> I appreciate the help.
>>>>>>>
>>>>>>> Thanks
>>>>>>> Bill
>>>>>>> "Shay Levi" wrote:
>>>>>>>> Bill
>>>>>>>>
>>>>>>>> Just to clarify, I'm not able to recycle remote app pools, just
>>>>>>>> list
>>>>>>>> their
>>>>>>>> names (Access denied).
>>>>>>>> Even adding full security on IIsApplicationPool (WMI
>>>>>>>> Properties)
>>>>>>>> didn't resolve
>>>>>>>> it.
>>>>>>>> I'm still on it...
>>>>>>>> -----
>>>>>>>> Shay Levi
>>>>>>>> $cript Fanatic
>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>> Are you able to list the Application poll names?
>>>>>>>>>
>>>>>>>>> $server="server"
>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>>>>>> $objWMI.Scope.Options.Authentication =
>>>>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>> $pools | foreach { $_.name }
>>>>>>>>> -----
>>>>>>>>> Shay Levi
>>>>>>>>> $cript Fanatic
>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>> Hi Shay,
>>>>>>>>>>
>>>>>>>>>> I have tested this and it still does not resolve the issue.
>>>>>>>>>>
>>>>>>>>>> I guess I must be missing something. The fact that the VB
>>>>>>>>>> Script works and the PowerShell does not, leads me to believe
>>>>>>>>>> that I have an issue with my script and not the security on
>>>>>>>>>> the target server.
>>>>>>>>>>
>>>>>>>>>> I may have to use PowerShell to call the actual .Net class
>>>>>>>>>> instead of using the built in commands.
>>>>>>>>>>
>>>>>>>>>> Funny, I would have never believed that something as simple
>>>>>>>>>> as recycling a remote application pool would be so hard :)
>>>>>>>>>>
>>>>>>>>>> Thanks again for the help.
>>>>>>>>>> Bill
>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>> Bill
>>>>>>>>>>>
>>>>>>>>>>> I can't test it right now. Try this on the remote server(s:)
>>>>>>>>>>>
>>>>>>>>>>> 1. Start > Run > dcomcnfg > ENTER
>>>>>>>>>>> 2. In the left pane (Compnent Services.msc) expand
>>>>>>>>>>> "Component
>>>>>>>>>>> Services" >
>>>>>>>>>>> Computers
>>>>>>>>>>> 3. Right click "My Computer"
>>>>>>>>>>> 4. In the "Default Properties" tab tick the "Enable COM
>>>>>>>>>>> Internet
>>>>>>>>>>> Services
>>>>>>>>>>> on this computer" checkbox
>>>>>>>>>>> 5. Reboot remote server
>>>>>>>>>>> 6. Test again
>>>>>>>>>>> I can't recall if you need to add Access Permissions in the
>>>>>>>>>>> "COM
>>>>>>>>>>> Security" tab.
>>>>>>>>>>> -----
>>>>>>>>>>> Shay Levi
>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>> Shay,
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks for the help, but I see the namespace
>>>>>>>>>>>> "root\MicrosoftIISv2" operate differently when attempting
>>>>>>>>>>>> to access it on a remote IIS6 computer (default W2K3
>>>>>>>>>>>> installs).
>>>>>>>>>>>>
>>>>>>>>>>>> The code examples you pointed out all work fine when
>>>>>>>>>>>> targeting the namespace locally, but I recieve access
>>>>>>>>>>>> denied messages when useing the same commands remotely.
>>>>>>>>>>>>
>>>>>>>>>>>> I have tried from multiple machines to multiple servers.
>>>>>>>>>>>> The VB Script I supplied still works in all cases, which
>>>>>>>>>>>> leads me to believe I am not setting the PowerShell WMI
>>>>>>>>>>>> object authentication to use pktPrivacy correctly.
>>>>>>>>>>>>
>>>>>>>>>>>> Any advice would be appreciated.
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks
>>>>>>>>>>>> Bill
>>>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>>>> Pinging the remote machine before trying to recycle pools
>>>>>>>>>>>>> is a recommended practice.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Also, I wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | % {$_.name}
>>>>>>>>>>>>> for those who are not familiar with gwmi and %, I should
>>>>>>>>>>>>> say
>>>>>>>>>>>>> that
>>>>>>>>>>>>> they are
>>>>>>>>>>>>> aliases for:
>>>>>>>>>>>>> gwmi = get-wmiobject
>>>>>>>>>>>>> % = foreach-object
>>>>>>>>>>>>> I should have written it like:
>>>>>>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | foreach-object
>>>>>>>>>>>>> {$_.name}
>>>>>>>>>>>>> -----
>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>> If you know the application pool name (the function
>>>>>>>>>>>>>> returns true/false respectively:
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> function Recycle-AppPool{
>>>>>>>>>>>>>> param([string]$server,[string]$appPool)
>>>>>>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.N
>>>>>>>>>>>>>> a
>>>>>>>>>>>>>> me
>>>>>>>>>>>>>> =
>>>>>>>>>>>>>> '
>>>>>>>>>>>>>> W
>>>>>>>>>>>>>> 3S
>>>>>>>>>>>>>> V
>>>>>>>>>>>>>> C/
>>>>>>>>>>>>>> A
>>>>>>>>>>>>>> ppPools/$appPool'").recycle();
>>>>>>>>>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>>>>>>>>>> }
>>>>>>>>>>>>>> To get a list of application pool names:
>>>>>>>>>>>>>> $computer = "your Server name/ip"
>>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>>>> "root\MicrosoftIISv2"
>>>>>>>>>>>>>> -computer
>>>>>>>>>>>>>> $computer | % {$_.name}
>>>>>>>>>>>>>> -----
>>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>>> I'm having some trouble finding the best method for
>>>>>>>>>>>>>>> recycling remote IIS6 application pools using
>>>>>>>>>>>>>>> PowerShell.
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> In the past this has always been very easy using VB
>>>>>>>>>>>>>>> script:
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Function PoolRecycle(strServer)
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> On Error Resume Next
>>>>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>>>>> Dim objWMIService
>>>>>>>>>>>>>>> Dim colItems
>>>>>>>>>>>>>>> Dim objItem
>>>>>>>>>>>>>>> 'recycle application pool
>>>>>>>>>>>>>>> WScript.Echo ""
>>>>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>>>>> Set objWMIService = GetObject _
>>>>>>>>>>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>>>>>>>>>>>>> & strServer & "\root\microsoftiisv2")
>>>>>>>>>>>>>>> 'connect to WMI
>>>>>>>>>>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>>>>>>>>>>>>> IIsApplicationPool")
>>>>>>>>>>>>>>> 'Step through the objects to be recycled
>>>>>>>>>>>>>>> For each objItem in colItems
>>>>>>>>>>>>>>> objItem.Recycle
>>>>>>>>>>>>>>> If err = 0 Then
>>>>>>>>>>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & "
>>>>>>>>>>>>>>> Recycled")
>>>>>>>>>>>>>>> PoolRecycle = "success"
>>>>>>>>>>>>>>> Else
>>>>>>>>>>>>>>> WScript.Echo("Unable to recycle the application pool on
>>>>>>>>>>>>>>> "
>>>>>>>>>>>>>>> &
>>>>>>>>>>>>>>> strServer)
>>>>>>>>>>>>>>> PoolRecycle = "failed"
>>>>>>>>>>>>>>> End If
>>>>>>>>>>>>>>> Next
>>>>>>>>>>>>>>> End Function
>>>>>>>>>>>>>>> I have attempted to duplicate the same thing in
>>>>>>>>>>>>>>> PowerShell,
>>>>>>>>>>>>>>> but
>>>>>>>>>>>>>>> I
>>>>>>>>>>>>>>> believe I am still running into an issue with the
>>>>>>>>>>>>>>> pktPrivacy
>>>>>>>>>>>>>>> authentication setting. The following code works when
>>>>>>>>>>>>>>> executing
>>>>>>>>>>>>>>> localy, but not on a remote host:
>>>>>>>>>>>>>>> Function PoolRecycle($strServerName)
>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From
>>>>>>>>>>>>>>> IIsApplicationPool"
>>>>>>>>>>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
>>>>>>>>>>>>>>> "\root\microsoftiisv2"
>>>>>>>>>>>>>>> $objWMI.Scope.Options.Authentication = 6
>>>>>>>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>>>>>>>> foreach ($pool in $pools)
>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>> $pool.recycle()
>>>>>>>>>>>>>>> if (!$?)
>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>> Write-Host $pool.name " - ERROR"
>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>> else
>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>> Write-Host $pool.name " - Recycled"
>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>> Any help or better ways of accomplishing this in
>>>>>>>>>>>>>>> PowerShell
>>>>>>>>>>>>>>> would
>>>>>>>>>>>>>>> be
>>>>>>>>>>>>>>> very much appreciated!
>>>>>>>>>>>>>>> Thanks
>>>>>>>>>>>>>>> Bill
 

My Computer

S

Shay Levi

#17
I also found this:

Encrypting Data When Running WMI–Based Remote Administration Scripts (IIS
6.0)
http://www.microsoft.com/technet/pr...ea9-0639-475f-b6c7-883cbde6f3a0.mspx?mfr=true

Authentication level seems to be in place. Anyone???

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> I monitored the remote server's WMI log file and found this
> (truncated) while executing the code:
>
> CALL ConnectionLogin::NTLMLogin
> wszNetworkResource = \\server\root\MicrosoftIISv2
> pPreferredLocale =
> lFlags = 0x0
> DCOM connection from DOMAIN\ShayL at authentiction level Packet,
> AuthnSvc
> = 9, AuthzSvc = 1, Capabilities = 0
> CALL CWbemNamespace::ExecQuery
> BSTR QueryFormat = WQL
> BSTR Query = select * from IIsApplicationPool
> IEnumWbemClassObject **pEnum = 0xB9130
> Access to the root\MicrosoftIISv2 namespace was denied. The
> namespace is
> marked with RequiresEncryption but the client connection was attempted
> with an authentication level below Pkt_Privacy.Re try the connection
> using Pkt_Privacy authentication level.
>
> Odd... the connection AuthenticationLevel was specified to
> PacketPrivacy
> $co.Authentication=[System.Management.AuthenticationLevel]::PacketPriv
> acy;
>
> Why do I still get access denied?
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com

>> Bill
>>
>> You're right. The pools aren't recycling. PowerShell doesn't return
>> any error
>> on $_.recycle
>> and I assumed no errors returned = recycled.
>> This turned out to be my typo error. I should have write
>> $_.recycle().
>> Now its givin me the ACCESS_DENIED again.
>> I'll check on it some more and see what I can find.
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com

>>> Hi Shay,
>>>
>>> This last set of code seems to have come the closest. I no longer
>>> get an access denied error, but the application pools on the remote
>>> server don't actually recycle.
>>>
>>> I have gotten some new ideas from these scripts though and I will
>>> try some things.
>>>
>>> Thank You for the continued effort.
>>> Bill
>>> "Shay Levi" wrote:
>>>> I came up with another working version:
>>>>
>>>> $server="server"
>>>>
>>>> $co = new-object System.Management.ConnectionOptions;
>>>> $co.Username="domain\username";
>>>> $co.Password="password";
>>>> $co.Authentication=[System.Management.AuthenticationLevel]::PacketP
>>>> r
>>>> i
>>>> vacy;
>>>> $co.EnablePrivileges=$true;
>>>> $wmi = New-Object System.Management.ManagementObjectSearcher;
>>>> $wmi.Query="Select * From IIsApplicationPool";
>>>> $wmi.Scope.Path="\\$server\root\microsoftiisv2";
>>>> $wmi.Scope.Options=$co;
>>>> $wmi.Get() | foreach {
>>>> $_.recycle;
>>>> if($?){
>>>> $_.name + " recycled";
>>>> } else {
>>>> $_.name + " error";
>>>> }
>>>> }
>>>> I hope it solves your problem.
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>>> You can even connect with alternate credentials:
>>>>>
>>>>> $wmi.Scope.Options.Username="domain\user"
>>>>> $wmi.Scope.Options.Password="yourPassword"
>>>>> -----
>>>>> Shay Levi
>>>>> $cript Fanatic
>>>>> http://scriptolog.blogspot.com
>>>>>> Bill
>>>>>>
>>>>>> Try this, I get no errors on my machines.
>>>>>>
>>>>>> $server="server"
>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>>> $objWMI.Scope.Options.Authentication =
>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy;
>>>>>> $objWMI.Scope.Options.EnablePrivileges=$true;
>>>>>> $pools = $objWMI.Get()
>>>>>> $pools | foreach {$_.recycle}
>>>>>> -----
>>>>>> Shay Levi
>>>>>> $cript Fanatic
>>>>>> http://scriptolog.blogspot.com
>>>>>>> I'll keep looking for it :-)
>>>>>>>
>>>>>>> -----
>>>>>>> Shay Levi
>>>>>>> $cript Fanatic
>>>>>>> http://scriptolog.blogspot.com
>>>>>>>> Hi Shay,
>>>>>>>>
>>>>>>>> Yeah, I can list the pools, but only after specifing the
>>>>>>>> authentication setting.
>>>>>>>>
>>>>>>>> It's almost like it does not carry to the other objects.
>>>>>>>>
>>>>>>>> I appreciate the help.
>>>>>>>>
>>>>>>>> Thanks
>>>>>>>> Bill
>>>>>>>> "Shay Levi" wrote:
>>>>>>>>> Bill
>>>>>>>>>
>>>>>>>>> Just to clarify, I'm not able to recycle remote app pools,
>>>>>>>>> just
>>>>>>>>> list
>>>>>>>>> their
>>>>>>>>> names (Access denied).
>>>>>>>>> Even adding full security on IIsApplicationPool (WMI
>>>>>>>>> Properties)
>>>>>>>>> didn't resolve
>>>>>>>>> it.
>>>>>>>>> I'm still on it...
>>>>>>>>> -----
>>>>>>>>> Shay Levi
>>>>>>>>> $cript Fanatic
>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>> Are you able to list the Application poll names?
>>>>>>>>>>
>>>>>>>>>> $server="server"
>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>>>>>>> $objWMI.Scope.Options.Authentication =
>>>>>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
>>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>>> $pools | foreach { $_.name }
>>>>>>>>>> -----
>>>>>>>>>> Shay Levi
>>>>>>>>>> $cript Fanatic
>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>> Hi Shay,
>>>>>>>>>>>
>>>>>>>>>>> I have tested this and it still does not resolve the issue.
>>>>>>>>>>>
>>>>>>>>>>> I guess I must be missing something. The fact that the VB
>>>>>>>>>>> Script works and the PowerShell does not, leads me to
>>>>>>>>>>> believe that I have an issue with my script and not the
>>>>>>>>>>> security on the target server.
>>>>>>>>>>>
>>>>>>>>>>> I may have to use PowerShell to call the actual .Net class
>>>>>>>>>>> instead of using the built in commands.
>>>>>>>>>>>
>>>>>>>>>>> Funny, I would have never believed that something as simple
>>>>>>>>>>> as recycling a remote application pool would be so hard :)
>>>>>>>>>>>
>>>>>>>>>>> Thanks again for the help.
>>>>>>>>>>> Bill
>>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>>> Bill
>>>>>>>>>>>>
>>>>>>>>>>>> I can't test it right now. Try this on the remote
>>>>>>>>>>>> server(s:)
>>>>>>>>>>>>
>>>>>>>>>>>> 1. Start > Run > dcomcnfg > ENTER
>>>>>>>>>>>> 2. In the left pane (Compnent Services.msc) expand
>>>>>>>>>>>> "Component
>>>>>>>>>>>> Services" >
>>>>>>>>>>>> Computers
>>>>>>>>>>>> 3. Right click "My Computer"
>>>>>>>>>>>> 4. In the "Default Properties" tab tick the "Enable COM
>>>>>>>>>>>> Internet
>>>>>>>>>>>> Services
>>>>>>>>>>>> on this computer" checkbox
>>>>>>>>>>>> 5. Reboot remote server
>>>>>>>>>>>> 6. Test again
>>>>>>>>>>>> I can't recall if you need to add Access Permissions in the
>>>>>>>>>>>> "COM
>>>>>>>>>>>> Security" tab.
>>>>>>>>>>>> -----
>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>> Shay,
>>>>>>>>>>>>>
>>>>>>>>>>>>> Thanks for the help, but I see the namespace
>>>>>>>>>>>>> "root\MicrosoftIISv2" operate differently when attempting
>>>>>>>>>>>>> to access it on a remote IIS6 computer (default W2K3
>>>>>>>>>>>>> installs).
>>>>>>>>>>>>>
>>>>>>>>>>>>> The code examples you pointed out all work fine when
>>>>>>>>>>>>> targeting the namespace locally, but I recieve access
>>>>>>>>>>>>> denied messages when useing the same commands remotely.
>>>>>>>>>>>>>
>>>>>>>>>>>>> I have tried from multiple machines to multiple servers.
>>>>>>>>>>>>> The VB Script I supplied still works in all cases, which
>>>>>>>>>>>>> leads me to believe I am not setting the PowerShell WMI
>>>>>>>>>>>>> object authentication to use pktPrivacy correctly.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Any advice would be appreciated.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Thanks
>>>>>>>>>>>>> Bill
>>>>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>>>>> Pinging the remote machine before trying to recycle pools
>>>>>>>>>>>>>> is a recommended practice.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Also, I wrote:
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | % {$_.name}
>>>>>>>>>>>>>> for those who are not familiar with gwmi and %, I should
>>>>>>>>>>>>>> say
>>>>>>>>>>>>>> that
>>>>>>>>>>>>>> they are
>>>>>>>>>>>>>> aliases for:
>>>>>>>>>>>>>> gwmi = get-wmiobject
>>>>>>>>>>>>>> % = foreach-object
>>>>>>>>>>>>>> I should have written it like:
>>>>>>>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer |
>>>>>>>>>>>>>> foreach-object
>>>>>>>>>>>>>> {$_.name}
>>>>>>>>>>>>>> -----
>>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>>> If you know the application pool name (the function
>>>>>>>>>>>>>>> returns true/false respectively:
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> function Recycle-AppPool{
>>>>>>>>>>>>>>> param([string]$server,[string]$appPool)
>>>>>>>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.
>>>>>>>>>>>>>>> N
>>>>>>>>>>>>>>> a
>>>>>>>>>>>>>>> me
>>>>>>>>>>>>>>> =
>>>>>>>>>>>>>>> '
>>>>>>>>>>>>>>> W
>>>>>>>>>>>>>>> 3S
>>>>>>>>>>>>>>> V
>>>>>>>>>>>>>>> C/
>>>>>>>>>>>>>>> A
>>>>>>>>>>>>>>> ppPools/$appPool'").recycle();
>>>>>>>>>>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>> To get a list of application pool names:
>>>>>>>>>>>>>>> $computer = "your Server name/ip"
>>>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>>>>> "root\MicrosoftIISv2"
>>>>>>>>>>>>>>> -computer
>>>>>>>>>>>>>>> $computer | % {$_.name}
>>>>>>>>>>>>>>> -----
>>>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>>>> I'm having some trouble finding the best method for
>>>>>>>>>>>>>>>> recycling remote IIS6 application pools using
>>>>>>>>>>>>>>>> PowerShell.
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> In the past this has always been very easy using VB
>>>>>>>>>>>>>>>> script:
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Function PoolRecycle(strServer)
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> On Error Resume Next
>>>>>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>>>>>> Dim objWMIService
>>>>>>>>>>>>>>>> Dim colItems
>>>>>>>>>>>>>>>> Dim objItem
>>>>>>>>>>>>>>>> 'recycle application pool
>>>>>>>>>>>>>>>> WScript.Echo ""
>>>>>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>>>>>> Set objWMIService = GetObject _
>>>>>>>>>>>>>>>> ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
>>>>>>>>>>>>>>>> & strServer & "\root\microsoftiisv2")
>>>>>>>>>>>>>>>> 'connect to WMI
>>>>>>>>>>>>>>>> Set colItems = objWMIService.ExecQuery("Select * From
>>>>>>>>>>>>>>>> IIsApplicationPool")
>>>>>>>>>>>>>>>> 'Step through the objects to be recycled
>>>>>>>>>>>>>>>> For each objItem in colItems
>>>>>>>>>>>>>>>> objItem.Recycle
>>>>>>>>>>>>>>>> If err = 0 Then
>>>>>>>>>>>>>>>> WScript.Echo(strServer & " - " & ObjItem.Name & "
>>>>>>>>>>>>>>>> Recycled")
>>>>>>>>>>>>>>>> PoolRecycle = "success"
>>>>>>>>>>>>>>>> Else
>>>>>>>>>>>>>>>> WScript.Echo("Unable to recycle the application pool on
>>>>>>>>>>>>>>>> "
>>>>>>>>>>>>>>>> &
>>>>>>>>>>>>>>>> strServer)
>>>>>>>>>>>>>>>> PoolRecycle = "failed"
>>>>>>>>>>>>>>>> End If
>>>>>>>>>>>>>>>> Next
>>>>>>>>>>>>>>>> End Function
>>>>>>>>>>>>>>>> I have attempted to duplicate the same thing in
>>>>>>>>>>>>>>>> PowerShell,
>>>>>>>>>>>>>>>> but
>>>>>>>>>>>>>>>> I
>>>>>>>>>>>>>>>> believe I am still running into an issue with the
>>>>>>>>>>>>>>>> pktPrivacy
>>>>>>>>>>>>>>>> authentication setting. The following code works when
>>>>>>>>>>>>>>>> executing
>>>>>>>>>>>>>>>> localy, but not on a remote host:
>>>>>>>>>>>>>>>> Function PoolRecycle($strServerName)
>>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From
>>>>>>>>>>>>>>>> IIsApplicationPool"
>>>>>>>>>>>>>>>> $objWMI.Scope.Path = "\\" + $strServerName +
>>>>>>>>>>>>>>>> "\root\microsoftiisv2"
>>>>>>>>>>>>>>>> $objWMI.Scope.Options.Authentication = 6
>>>>>>>>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>>>>>>>>> foreach ($pool in $pools)
>>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>>> $pool.recycle()
>>>>>>>>>>>>>>>> if (!$?)
>>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>>> Write-Host $pool.name " - ERROR"
>>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>>> else
>>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>>> Write-Host $pool.name " - Recycled"
>>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>>> Any help or better ways of accomplishing this in
>>>>>>>>>>>>>>>> PowerShell
>>>>>>>>>>>>>>>> would
>>>>>>>>>>>>>>>> be
>>>>>>>>>>>>>>>> very much appreciated!
>>>>>>>>>>>>>>>> Thanks
>>>>>>>>>>>>>>>> Bill
 

My Computer

B
#18
Hi Shay,

I'm still pluggin around, but I wanted to run a couple of things by you.

Do you think it could be related to the Timeout option? I'm not sure how to
translate the value, but if it was short our connection could have timed out.

Another possibility could be that the connection options are not carrying
over to the other objects. Maybe reset the $_.psbase.options on each object??

Thanks
Bill


"Shay Levi" wrote:

> I also found this:
>
> Encrypting Data When Running WMI–Based Remote Administration Scripts (IIS
> 6.0)
> http://www.microsoft.com/technet/pr...ea9-0639-475f-b6c7-883cbde6f3a0.mspx?mfr=true
>
> Authentication level seems to be in place. Anyone???
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
>
>

> > I monitored the remote server's WMI log file and found this
> > (truncated) while executing the code:
> >
> > CALL ConnectionLogin::NTLMLogin
> > wszNetworkResource = \\server\root\MicrosoftIISv2
> > pPreferredLocale =
> > lFlags = 0x0
> > DCOM connection from DOMAIN\ShayL at authentiction level Packet,
> > AuthnSvc
> > = 9, AuthzSvc = 1, Capabilities = 0
> > CALL CWbemNamespace::ExecQuery
> > BSTR QueryFormat = WQL
> > BSTR Query = select * from IIsApplicationPool
> > IEnumWbemClassObject **pEnum = 0xB9130
> > Access to the root\MicrosoftIISv2 namespace was denied. The
> > namespace is
> > marked with RequiresEncryption but the client connection was attempted
> > with an authentication level below Pkt_Privacy.Re try the connection
> > using Pkt_Privacy authentication level.
> >
> > Odd... the connection AuthenticationLevel was specified to
> > PacketPrivacy
> > $co.Authentication=[System.Management.AuthenticationLevel]::PacketPriv
> > acy;
> >
> > Why do I still get access denied?
> >
> > -----
> > Shay Levi
> > $cript Fanatic
> > http://scriptolog.blogspot.com

> >> Bill
> >>
> >> You're right. The pools aren't recycling. PowerShell doesn't return
> >> any error
> >> on $_.recycle
> >> and I assumed no errors returned = recycled.
> >> This turned out to be my typo error. I should have write
> >> $_.recycle().
> >> Now its givin me the ACCESS_DENIED again.
> >> I'll check on it some more and see what I can find.
> >> -----
> >> Shay Levi
> >> $cript Fanatic
> >> http://scriptolog.blogspot.com
> >>> Hi Shay,
> >>>
> >>> This last set of code seems to have come the closest. I no longer
> >>> get an access denied error, but the application pools on the remote
> >>> server don't actually recycle.
> >>>
> >>> I have gotten some new ideas from these scripts though and I will
> >>> try some things.
> >>>
> >>> Thank You for the continued effort.
> >>> Bill
> >>> "Shay Levi" wrote:
> >>>> I came up with another working version:
> >>>>
> >>>> $server="server"
> >>>>
> >>>> $co = new-object System.Management.ConnectionOptions;
> >>>> $co.Username="domain\username";
> >>>> $co.Password="password";
> >>>> $co.Authentication=[System.Management.AuthenticationLevel]::PacketP
> >>>> r
> >>>> i
> >>>> vacy;
> >>>> $co.EnablePrivileges=$true;
> >>>> $wmi = New-Object System.Management.ManagementObjectSearcher;
> >>>> $wmi.Query="Select * From IIsApplicationPool";
> >>>> $wmi.Scope.Path="\\$server\root\microsoftiisv2";
> >>>> $wmi.Scope.Options=$co;
> >>>> $wmi.Get() | foreach {
> >>>> $_.recycle;
> >>>> if($?){
> >>>> $_.name + " recycled";
> >>>> } else {
> >>>> $_.name + " error";
> >>>> }
> >>>> }
> >>>> I hope it solves your problem.
> >>>> -----
> >>>> Shay Levi
> >>>> $cript Fanatic
> >>>> http://scriptolog.blogspot.com
> >>>>> You can even connect with alternate credentials:
> >>>>>
> >>>>> $wmi.Scope.Options.Username="domain\user"
> >>>>> $wmi.Scope.Options.Password="yourPassword"
> >>>>> -----
> >>>>> Shay Levi
> >>>>> $cript Fanatic
> >>>>> http://scriptolog.blogspot.com
> >>>>>> Bill
> >>>>>>
> >>>>>> Try this, I get no errors on my machines.
> >>>>>>
> >>>>>> $server="server"
> >>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> >>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
> >>>>>> $objWMI.Scope.Options.Authentication =
> >>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy;
> >>>>>> $objWMI.Scope.Options.EnablePrivileges=$true;
> >>>>>> $pools = $objWMI.Get()
> >>>>>> $pools | foreach {$_.recycle}
> >>>>>> -----
> >>>>>> Shay Levi
> >>>>>> $cript Fanatic
> >>>>>> http://scriptolog.blogspot.com
> >>>>>>> I'll keep looking for it :-)
> >>>>>>>
> >>>>>>> -----
> >>>>>>> Shay Levi
> >>>>>>> $cript Fanatic
> >>>>>>> http://scriptolog.blogspot.com
> >>>>>>>> Hi Shay,
> >>>>>>>>
> >>>>>>>> Yeah, I can list the pools, but only after specifing the
> >>>>>>>> authentication setting.
> >>>>>>>>
> >>>>>>>> It's almost like it does not carry to the other objects.
> >>>>>>>>
> >>>>>>>> I appreciate the help.
> >>>>>>>>
> >>>>>>>> Thanks
> >>>>>>>> Bill
> >>>>>>>> "Shay Levi" wrote:
> >>>>>>>>> Bill
> >>>>>>>>>
> >>>>>>>>> Just to clarify, I'm not able to recycle remote app pools,
> >>>>>>>>> just
> >>>>>>>>> list
> >>>>>>>>> their
> >>>>>>>>> names (Access denied).
> >>>>>>>>> Even adding full security on IIsApplicationPool (WMI
> >>>>>>>>> Properties)
> >>>>>>>>> didn't resolve
> >>>>>>>>> it.
> >>>>>>>>> I'm still on it...
> >>>>>>>>> -----
> >>>>>>>>> Shay Levi
> >>>>>>>>> $cript Fanatic
> >>>>>>>>> http://scriptolog.blogspot.com
> >>>>>>>>>> Are you able to list the Application poll names?
> >>>>>>>>>>
> >>>>>>>>>> $server="server"
> >>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
> >>>>>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
> >>>>>>>>>> $objWMI.Scope.Options.Authentication =
> >>>>>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
> >>>>>>>>>> $pools = $objWMI.Get()
> >>>>>>>>>> $pools | foreach { $_.name }
> >>>>>>>>>> -----
> >>>>>>>>>> Shay Levi
> >>>>>>>>>> $cript Fanatic
> >>>>>>>>>> http://scriptolog.blogspot.com
> >>>>>>>>>>> Hi Shay,
> >>>>>>>>>>>
> >>>>>>>>>>> I have tested this and it still does not resolve the issue.
> >>>>>>>>>>>
> >>>>>>>>>>> I guess I must be missing something. The fact that the VB
> >>>>>>>>>>> Script works and the PowerShell does not, leads me to
> >>>>>>>>>>> believe that I have an issue with my script and not the
> >>>>>>>>>>> security on the target server.
> >>>>>>>>>>>
> >>>>>>>>>>> I may have to use PowerShell to call the actual .Net class
> >>>>>>>>>>> instead of using the built in commands.
> >>>>>>>>>>>
> >>>>>>>>>>> Funny, I would have never believed that something as simple
> >>>>>>>>>>> as recycling a remote application pool would be so hard :)
> >>>>>>>>>>>
> >>>>>>>>>>> Thanks again for the help.
> >>>>>>>>>>> Bill
> >>>>>>>>>>> "Shay Levi" wrote:
> >>>>>>>>>>>> Bill
> >>>>>>>>>>>>
> >>>>>>>>>>>> I can't test it right now. Try this on the remote
> >>>>>>>>>>>> server(s:)
> >>>>>>>>>>>>
> >>>>>>>>>>>> 1. Start > Run > dcomcnfg > ENTER
> >>>>>>>>>>>> 2. In the left pane (Compnent Services.msc) expand
> >>>>>>>>>>>> "Component
> >>>>>>>>>>>> Services" >
> >>>>>>>>>>>> Computers
> >>>>>>>>>>>> 3. Right click "My Computer"
> >>>>>>>>>>>> 4. In the "Default Properties" tab tick the "Enable COM
> >>>>>>>>>>>> Internet
> >>>>>>>>>>>> Services
> >>>>>>>>>>>> on this computer" checkbox
> >>>>>>>>>>>> 5. Reboot remote server
> >>>>>>>>>>>> 6. Test again
> >>>>>>>>>>>> I can't recall if you need to add Access Permissions in the
> >>>>>>>>>>>> "COM
> >>>>>>>>>>>> Security" tab.
> >>>>>>>>>>>> -----
> >>>>>>>>>>>> Shay Levi
> >>>>>>>>>>>> $cript Fanatic
> >>>>>>>>>>>> http://scriptolog.blogspot.com
> >>>>>>>>>>>>> Shay,
> >>>>>>>>>>>>>
> >>>>>>>>>>>>> Thanks for the help, but I see the namespace
> >>>>>>>>>>>>> "root\MicrosoftIISv2" operate differently when attempting
> >>>>>>>>>>>>> to access it on a remote IIS6 computer (default W2K3
> >>>>>>>>>>>>> installs).
> >>>>>>>>>>>>>
> >>>>>>>>>>>>> The code examples you pointed out all work fine when
> >>>>>>>>>>>>> targeting the namespace locally, but I recieve access
> >>>>>>>>>>>>> denied messages when useing the same commands remotely.
> >>>>>>>>>>>>>
> >>>>>>>>>>>>> I have tried from multiple machines to multiple servers.
> >>>>>>>>>>>>> The VB Script I supplied still works in all cases, which
> >>>>>>>>>>>>> leads me to believe I am not setting the PowerShell WMI
> >>>>>>>>>>>>> object authentication to use pktPrivacy correctly.
> >>>>>>>>>>>>>
> >>>>>>>>>>>>> Any advice would be appreciated.
> >>>>>>>>>>>>>
> >>>>>>>>>>>>> Thanks
> >>>>>>>>>>>>> Bill
> >>>>>>>>>>>>> "Shay Levi" wrote:
> >>>>>>>>>>>>>> Pinging the remote machine before trying to recycle pools
> >>>>>>>>>>>>>> is a recommended practice.
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>> Also, I wrote:
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
> >>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | % {$_.name}
> >>>>>>>>>>>>>> for those who are not familiar with gwmi and %, I should
> >>>>>>>>>>>>>> say
> >>>>>>>>>>>>>> that
> >>>>>>>>>>>>>> they are
> >>>>>>>>>>>>>> aliases for:
> >>>>>>>>>>>>>> gwmi = get-wmiobject
> >>>>>>>>>>>>>> % = foreach-object
> >>>>>>>>>>>>>> I should have written it like:
> >>>>>>>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
> >>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer |
> >>>>>>>>>>>>>> foreach-object
> >>>>>>>>>>>>>> {$_.name}
> >>>>>>>>>>>>>> -----
> >>>>>>>>>>>>>> Shay Levi
> >>>>>>>>>>>>>> $cript Fanatic
> >>>>>>>>>>>>>> http://scriptolog.blogspot.com
> >>>>>>>>>>>>>>> If you know the application pool name (the function
> >>>>>>>>>>>>>>> returns true/false respectively:
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>> function Recycle-AppPool{
> >>>>>>>>>>>>>>> param([string]$server,[string]$appPool)
> >>>>>>>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPool.
> >>>>>>>>>>>>>>> N
> >>>>>>>>>>>>>>> a
> >>>>>>>>>>>>>>> me
> >>>>>>>>>>>>>>> =
> >>>>>>>>>>>>>>> '
> >>>>>>>>>>>>>>> W
> >>>>>>>>>>>>>>> 3S
> >>>>>>>>>>>>>>> V
> >>>>>>>>>>>>>>> C/
> >>>>>>>>>>>>>>> A
> >>>>>>>>>>>>>>> ppPools/$appPool'").recycle();
> >>>>>>>>>>>>>>> if($? -ne 0}{$false} else {$true}
> >>>>>>>>>>>>>>> }
> >>>>>>>>>>>>>>> To get a list of application pool names:
> >>>>>>>>>>>>>>> $computer = "your Server name/ip"
> >>>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
> >>>>>>>>>>>>>>> "root\MicrosoftIISv2"
> >>>>>>>>>>>>>>> -computer
> >>>>>>>>>>>>>>> $computer | % {$_.name}
> >>>>>>>>>>>>>>> -----
> >>>>>>>>>>>>>>> Shay Levi
> >>>>>>>>>>>>>>> $cript Fanatic
> >>>>>>>>>>>>>>> http://scriptolog.blogspot.com
> >>>>>>>>>>>>>>>> I'm having some trouble finding the best method for
> >>>>>>>>>>>>>>>> recycling remote IIS6 application pools using
> >>>>>>>>>>>>>>>> PowerShell.
> >>>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>> In the past this has always been very easy using VB
> >>>>>>>>>>>>>>>> script:
> >>>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>> Function PoolRecycle(strServer)
> >>>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>> On Error Resume Next
> >>>>>>>>>>>>>>>> Err.Clear
> >>>>>>>>>>>>>>>> Dim objWMIService
> >>>>>>>>>>>>>>>> Dim colItems
 

My Computer

S

Shay Levi

#19
IMO, its a permissions issue.

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> Hi Shay,
>
> I'm still pluggin around, but I wanted to run a couple of things by
> you.
>
> Do you think it could be related to the Timeout option? I'm not sure
> how to translate the value, but if it was short our connection could
> have timed out.
>
> Another possibility could be that the connection options are not
> carrying over to the other objects. Maybe reset the $_.psbase.options
> on each object??
>
> Thanks
> Bill
> "Shay Levi" wrote:
>

>> I also found this:
>>
>> Encrypting Data When Running WMI–Based Remote Administration Scripts
>> (IIS
>>
>> 6.0)
>>
>> http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Librar
>> y/IIS/aa7f3ea9-0639-475f-b6c7-883cbde6f3a0.mspx?mfr=true
>>
>> Authentication level seems to be in place. Anyone???
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com

>>> I monitored the remote server's WMI log file and found this
>>> (truncated) while executing the code:
>>>
>>> CALL ConnectionLogin::NTLMLogin
>>> wszNetworkResource = \\server\root\MicrosoftIISv2
>>> pPreferredLocale =
>>> lFlags = 0x0
>>> DCOM connection from DOMAIN\ShayL at authentiction level Packet,
>>> AuthnSvc
>>> = 9, AuthzSvc = 1, Capabilities = 0
>>> CALL CWbemNamespace::ExecQuery
>>> BSTR QueryFormat = WQL
>>> BSTR Query = select * from IIsApplicationPool
>>> IEnumWbemClassObject **pEnum = 0xB9130
>>> Access to the root\MicrosoftIISv2 namespace was denied. The
>>> namespace is
>>> marked with RequiresEncryption but the client connection was
>>> attempted
>>> with an authentication level below Pkt_Privacy.Re try the connection
>>> using Pkt_Privacy authentication level.
>>> Odd... the connection AuthenticationLevel was specified to
>>> PacketPrivacy
>>> $co.Authentication=[System.Management.AuthenticationLevel]::PacketPr
>>> iv acy;
>>>
>>> Why do I still get access denied?
>>>
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> Bill
>>>>
>>>> You're right. The pools aren't recycling. PowerShell doesn't return
>>>> any error
>>>> on $_.recycle
>>>> and I assumed no errors returned = recycled.
>>>> This turned out to be my typo error. I should have write
>>>> $_.recycle().
>>>> Now its givin me the ACCESS_DENIED again.
>>>> I'll check on it some more and see what I can find.
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>>> Hi Shay,
>>>>>
>>>>> This last set of code seems to have come the closest. I no longer
>>>>> get an access denied error, but the application pools on the
>>>>> remote server don't actually recycle.
>>>>>
>>>>> I have gotten some new ideas from these scripts though and I will
>>>>> try some things.
>>>>>
>>>>> Thank You for the continued effort.
>>>>> Bill
>>>>> "Shay Levi" wrote:
>>>>>> I came up with another working version:
>>>>>>
>>>>>> $server="server"
>>>>>>
>>>>>> $co = new-object System.Management.ConnectionOptions;
>>>>>> $co.Username="domain\username";
>>>>>> $co.Password="password";
>>>>>> $co.Authentication=[System.Management.AuthenticationLevel]::Packe
>>>>>> tP
>>>>>> r
>>>>>> i
>>>>>> vacy;
>>>>>> $co.EnablePrivileges=$true;
>>>>>> $wmi = New-Object System.Management.ManagementObjectSearcher;
>>>>>> $wmi.Query="Select * From IIsApplicationPool";
>>>>>> $wmi.Scope.Path="\\$server\root\microsoftiisv2";
>>>>>> $wmi.Scope.Options=$co;
>>>>>> $wmi.Get() | foreach {
>>>>>> $_.recycle;
>>>>>> if($?){
>>>>>> $_.name + " recycled";
>>>>>> } else {
>>>>>> $_.name + " error";
>>>>>> }
>>>>>> }
>>>>>> I hope it solves your problem.
>>>>>> -----
>>>>>> Shay Levi
>>>>>> $cript Fanatic
>>>>>> http://scriptolog.blogspot.com
>>>>>>> You can even connect with alternate credentials:
>>>>>>>
>>>>>>> $wmi.Scope.Options.Username="domain\user"
>>>>>>> $wmi.Scope.Options.Password="yourPassword"
>>>>>>> -----
>>>>>>> Shay Levi
>>>>>>> $cript Fanatic
>>>>>>> http://scriptolog.blogspot.com
>>>>>>>> Bill
>>>>>>>>
>>>>>>>> Try this, I get no errors on my machines.
>>>>>>>>
>>>>>>>> $server="server"
>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>>>>> $objWMI.Scope.Options.Authentication =
>>>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy;
>>>>>>>> $objWMI.Scope.Options.EnablePrivileges=$true;
>>>>>>>> $pools = $objWMI.Get()
>>>>>>>> $pools | foreach {$_.recycle}
>>>>>>>> -----
>>>>>>>> Shay Levi
>>>>>>>> $cript Fanatic
>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>> I'll keep looking for it :-)
>>>>>>>>>
>>>>>>>>> -----
>>>>>>>>> Shay Levi
>>>>>>>>> $cript Fanatic
>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>> Hi Shay,
>>>>>>>>>>
>>>>>>>>>> Yeah, I can list the pools, but only after specifing the
>>>>>>>>>> authentication setting.
>>>>>>>>>>
>>>>>>>>>> It's almost like it does not carry to the other objects.
>>>>>>>>>>
>>>>>>>>>> I appreciate the help.
>>>>>>>>>>
>>>>>>>>>> Thanks
>>>>>>>>>> Bill
>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>> Bill
>>>>>>>>>>>
>>>>>>>>>>> Just to clarify, I'm not able to recycle remote app pools,
>>>>>>>>>>> just
>>>>>>>>>>> list
>>>>>>>>>>> their
>>>>>>>>>>> names (Access denied).
>>>>>>>>>>> Even adding full security on IIsApplicationPool (WMI
>>>>>>>>>>> Properties)
>>>>>>>>>>> didn't resolve
>>>>>>>>>>> it.
>>>>>>>>>>> I'm still on it...
>>>>>>>>>>> -----
>>>>>>>>>>> Shay Levi
>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>> Are you able to list the Application poll names?
>>>>>>>>>>>>
>>>>>>>>>>>> $server="server"
>>>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>>>>>>>>> $objWMI.Scope.Options.Authentication =
>>>>>>>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
>>>>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>>>>> $pools | foreach { $_.name }
>>>>>>>>>>>> -----
>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>> Hi Shay,
>>>>>>>>>>>>>
>>>>>>>>>>>>> I have tested this and it still does not resolve the
>>>>>>>>>>>>> issue.
>>>>>>>>>>>>>
>>>>>>>>>>>>> I guess I must be missing something. The fact that the VB
>>>>>>>>>>>>> Script works and the PowerShell does not, leads me to
>>>>>>>>>>>>> believe that I have an issue with my script and not the
>>>>>>>>>>>>> security on the target server.
>>>>>>>>>>>>>
>>>>>>>>>>>>> I may have to use PowerShell to call the actual .Net class
>>>>>>>>>>>>> instead of using the built in commands.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Funny, I would have never believed that something as
>>>>>>>>>>>>> simple as recycling a remote application pool would be so
>>>>>>>>>>>>> hard :)
>>>>>>>>>>>>>
>>>>>>>>>>>>> Thanks again for the help.
>>>>>>>>>>>>> Bill
>>>>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>>>>> Bill
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> I can't test it right now. Try this on the remote
>>>>>>>>>>>>>> server(s:)
>>>>>>>>>>>>>> 1. Start > Run > dcomcnfg > ENTER
>>>>>>>>>>>>>> 2. In the left pane (Compnent Services.msc) expand
>>>>>>>>>>>>>> "Component
>>>>>>>>>>>>>> Services" >
>>>>>>>>>>>>>> Computers
>>>>>>>>>>>>>> 3. Right click "My Computer"
>>>>>>>>>>>>>> 4. In the "Default Properties" tab tick the "Enable COM
>>>>>>>>>>>>>> Internet
>>>>>>>>>>>>>> Services
>>>>>>>>>>>>>> on this computer" checkbox
>>>>>>>>>>>>>> 5. Reboot remote server
>>>>>>>>>>>>>> 6. Test again
>>>>>>>>>>>>>> I can't recall if you need to add Access Permissions in
>>>>>>>>>>>>>> the
>>>>>>>>>>>>>> "COM
>>>>>>>>>>>>>> Security" tab.
>>>>>>>>>>>>>> -----
>>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>>> Shay,
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Thanks for the help, but I see the namespace
>>>>>>>>>>>>>>> "root\MicrosoftIISv2" operate differently when
>>>>>>>>>>>>>>> attempting to access it on a remote IIS6 computer
>>>>>>>>>>>>>>> (default W2K3 installs).
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> The code examples you pointed out all work fine when
>>>>>>>>>>>>>>> targeting the namespace locally, but I recieve access
>>>>>>>>>>>>>>> denied messages when useing the same commands remotely.
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> I have tried from multiple machines to multiple servers.
>>>>>>>>>>>>>>> The VB Script I supplied still works in all cases, which
>>>>>>>>>>>>>>> leads me to believe I am not setting the PowerShell WMI
>>>>>>>>>>>>>>> object authentication to use pktPrivacy correctly.
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Any advice would be appreciated.
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Thanks
>>>>>>>>>>>>>>> Bill
>>>>>>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>>>>>>> Pinging the remote machine before trying to recycle
>>>>>>>>>>>>>>>> pools is a recommended practice.
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Also, I wrote:
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | % {$_.name}
>>>>>>>>>>>>>>>> for those who are not familiar with gwmi and %, I
>>>>>>>>>>>>>>>> should
>>>>>>>>>>>>>>>> say
>>>>>>>>>>>>>>>> that
>>>>>>>>>>>>>>>> they are
>>>>>>>>>>>>>>>> aliases for:
>>>>>>>>>>>>>>>> gwmi = get-wmiobject
>>>>>>>>>>>>>>>> % = foreach-object
>>>>>>>>>>>>>>>> I should have written it like:
>>>>>>>>>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer |
>>>>>>>>>>>>>>>> foreach-object
>>>>>>>>>>>>>>>> {$_.name}
>>>>>>>>>>>>>>>> -----
>>>>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>>>>> If you know the application pool name (the function
>>>>>>>>>>>>>>>>> returns true/false respectively:
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> function Recycle-AppPool{
>>>>>>>>>>>>>>>>> param([string]$server,[string]$appPool)
>>>>>>>>>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPoo
>>>>>>>>>>>>>>>>> l.
>>>>>>>>>>>>>>>>> N
>>>>>>>>>>>>>>>>> a
>>>>>>>>>>>>>>>>> me
>>>>>>>>>>>>>>>>> =
>>>>>>>>>>>>>>>>> '
>>>>>>>>>>>>>>>>> W
>>>>>>>>>>>>>>>>> 3S
>>>>>>>>>>>>>>>>> V
>>>>>>>>>>>>>>>>> C/
>>>>>>>>>>>>>>>>> A
>>>>>>>>>>>>>>>>> ppPools/$appPool'").recycle();
>>>>>>>>>>>>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>>>> To get a list of application pool names:
>>>>>>>>>>>>>>>>> $computer = "your Server name/ip"
>>>>>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>>>>>>> "root\MicrosoftIISv2"
>>>>>>>>>>>>>>>>> -computer
>>>>>>>>>>>>>>>>> $computer | % {$_.name}
>>>>>>>>>>>>>>>>> -----
>>>>>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>>>>>> I'm having some trouble finding the best method for
>>>>>>>>>>>>>>>>>> recycling remote IIS6 application pools using
>>>>>>>>>>>>>>>>>> PowerShell.
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> In the past this has always been very easy using VB
>>>>>>>>>>>>>>>>>> script:
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> Function PoolRecycle(strServer)
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> On Error Resume Next
>>>>>>>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>>>>>>>> Dim objWMIService
>>>>>>>>>>>>>>>>>> Dim colItems
 

My Computer

S

Shay Levi

#20
Bill

I'm still struggling with the WMI method, I tried so many variations and
still gets access denied.
In the meantime I was able to recycle via ADSI, can you confirm?

$server="server"
$pool=[adsi]"IIS://$server/W3SVC/AppPools/DefaultAppPool"
$pool.psbase.Invoke("recycle")

I can see that the w3wp process id is changing.


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com



> IMO, its a permissions issue.
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com

>> Hi Shay,
>>
>> I'm still pluggin around, but I wanted to run a couple of things by
>> you.
>>
>> Do you think it could be related to the Timeout option? I'm not sure
>> how to translate the value, but if it was short our connection could
>> have timed out.
>>
>> Another possibility could be that the connection options are not
>> carrying over to the other objects. Maybe reset the
>> $_.psbase.options on each object??
>>
>> Thanks
>> Bill
>> "Shay Levi" wrote:

>>> I also found this:
>>>
>>> Encrypting Data When Running WMI–Based Remote Administration Scripts
>>> (IIS
>>>
>>> 6.0)
>>>
>>> http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Libra
>>> r y/IIS/aa7f3ea9-0639-475f-b6c7-883cbde6f3a0.mspx?mfr=true
>>>
>>> Authentication level seems to be in place. Anyone???
>>>
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> I monitored the remote server's WMI log file and found this
>>>> (truncated) while executing the code:
>>>>
>>>> CALL ConnectionLogin::NTLMLogin
>>>> wszNetworkResource = \\server\root\MicrosoftIISv2
>>>> pPreferredLocale =
>>>> lFlags = 0x0
>>>> DCOM connection from DOMAIN\ShayL at authentiction level Packet,
>>>> AuthnSvc
>>>> = 9, AuthzSvc = 1, Capabilities = 0
>>>> CALL CWbemNamespace::ExecQuery
>>>> BSTR QueryFormat = WQL
>>>> BSTR Query = select * from IIsApplicationPool
>>>> IEnumWbemClassObject **pEnum = 0xB9130
>>>> Access to the root\MicrosoftIISv2 namespace was denied. The
>>>> namespace is
>>>> marked with RequiresEncryption but the client connection was
>>>> attempted
>>>> with an authentication level below Pkt_Privacy.Re try the
>>>> connection
>>>> using Pkt_Privacy authentication level.
>>>> Odd... the connection AuthenticationLevel was specified to
>>>> PacketPrivacy
>>>> $co.Authentication=[System.Management.AuthenticationLevel]::PacketP
>>>> r
>>>> iv acy;
>>>> Why do I still get access denied?
>>>>
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>>> Bill
>>>>>
>>>>> You're right. The pools aren't recycling. PowerShell doesn't
>>>>> return
>>>>> any error
>>>>> on $_.recycle
>>>>> and I assumed no errors returned = recycled.
>>>>> This turned out to be my typo error. I should have write
>>>>> $_.recycle().
>>>>> Now its givin me the ACCESS_DENIED again.
>>>>> I'll check on it some more and see what I can find.
>>>>> -----
>>>>> Shay Levi
>>>>> $cript Fanatic
>>>>> http://scriptolog.blogspot.com
>>>>>> Hi Shay,
>>>>>>
>>>>>> This last set of code seems to have come the closest. I no
>>>>>> longer get an access denied error, but the application pools on
>>>>>> the remote server don't actually recycle.
>>>>>>
>>>>>> I have gotten some new ideas from these scripts though and I will
>>>>>> try some things.
>>>>>>
>>>>>> Thank You for the continued effort.
>>>>>> Bill
>>>>>> "Shay Levi" wrote:
>>>>>>> I came up with another working version:
>>>>>>>
>>>>>>> $server="server"
>>>>>>>
>>>>>>> $co = new-object System.Management.ConnectionOptions;
>>>>>>> $co.Username="domain\username";
>>>>>>> $co.Password="password";
>>>>>>> $co.Authentication=[System.Management.AuthenticationLevel]::Pack
>>>>>>> e
>>>>>>> tP
>>>>>>> r
>>>>>>> i
>>>>>>> vacy;
>>>>>>> $co.EnablePrivileges=$true;
>>>>>>> $wmi = New-Object System.Management.ManagementObjectSearcher;
>>>>>>> $wmi.Query="Select * From IIsApplicationPool";
>>>>>>> $wmi.Scope.Path="\\$server\root\microsoftiisv2";
>>>>>>> $wmi.Scope.Options=$co;
>>>>>>> $wmi.Get() | foreach {
>>>>>>> $_.recycle;
>>>>>>> if($?){
>>>>>>> $_.name + " recycled";
>>>>>>> } else {
>>>>>>> $_.name + " error";
>>>>>>> }
>>>>>>> }
>>>>>>> I hope it solves your problem.
>>>>>>> -----
>>>>>>> Shay Levi
>>>>>>> $cript Fanatic
>>>>>>> http://scriptolog.blogspot.com
>>>>>>>> You can even connect with alternate credentials:
>>>>>>>>
>>>>>>>> $wmi.Scope.Options.Username="domain\user"
>>>>>>>> $wmi.Scope.Options.Password="yourPassword"
>>>>>>>> -----
>>>>>>>> Shay Levi
>>>>>>>> $cript Fanatic
>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>> Bill
>>>>>>>>>
>>>>>>>>> Try this, I get no errors on my machines.
>>>>>>>>>
>>>>>>>>> $server="server"
>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>>>>>> $objWMI.Scope.Options.Authentication =
>>>>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy;
>>>>>>>>> $objWMI.Scope.Options.EnablePrivileges=$true;
>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>> $pools | foreach {$_.recycle}
>>>>>>>>> -----
>>>>>>>>> Shay Levi
>>>>>>>>> $cript Fanatic
>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>> I'll keep looking for it :-)
>>>>>>>>>>
>>>>>>>>>> -----
>>>>>>>>>> Shay Levi
>>>>>>>>>> $cript Fanatic
>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>> Hi Shay,
>>>>>>>>>>>
>>>>>>>>>>> Yeah, I can list the pools, but only after specifing the
>>>>>>>>>>> authentication setting.
>>>>>>>>>>>
>>>>>>>>>>> It's almost like it does not carry to the other objects.
>>>>>>>>>>>
>>>>>>>>>>> I appreciate the help.
>>>>>>>>>>>
>>>>>>>>>>> Thanks
>>>>>>>>>>> Bill
>>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>>> Bill
>>>>>>>>>>>>
>>>>>>>>>>>> Just to clarify, I'm not able to recycle remote app pools,
>>>>>>>>>>>> just
>>>>>>>>>>>> list
>>>>>>>>>>>> their
>>>>>>>>>>>> names (Access denied).
>>>>>>>>>>>> Even adding full security on IIsApplicationPool (WMI
>>>>>>>>>>>> Properties)
>>>>>>>>>>>> didn't resolve
>>>>>>>>>>>> it.
>>>>>>>>>>>> I'm still on it...
>>>>>>>>>>>> -----
>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>> Are you able to list the Application poll names?
>>>>>>>>>>>>>
>>>>>>>>>>>>> $server="server"
>>>>>>>>>>>>> $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
>>>>>>>>>>>>> $objWMI.Scope.Path = "\\$server\root\microsoftiisv2"
>>>>>>>>>>>>> $objWMI.Scope.Options.Authentication =
>>>>>>>>>>>>> [System.Management.AuthenticationLevel]::PacketPrivacy
>>>>>>>>>>>>> $pools = $objWMI.Get()
>>>>>>>>>>>>> $pools | foreach { $_.name }
>>>>>>>>>>>>> -----
>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>> Hi Shay,
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> I have tested this and it still does not resolve the
>>>>>>>>>>>>>> issue.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> I guess I must be missing something. The fact that the
>>>>>>>>>>>>>> VB Script works and the PowerShell does not, leads me to
>>>>>>>>>>>>>> believe that I have an issue with my script and not the
>>>>>>>>>>>>>> security on the target server.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> I may have to use PowerShell to call the actual .Net
>>>>>>>>>>>>>> class instead of using the built in commands.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Funny, I would have never believed that something as
>>>>>>>>>>>>>> simple as recycling a remote application pool would be so
>>>>>>>>>>>>>> hard :)
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Thanks again for the help.
>>>>>>>>>>>>>> Bill
>>>>>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>>>>>> Bill
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> I can't test it right now. Try this on the remote
>>>>>>>>>>>>>>> server(s:)
>>>>>>>>>>>>>>> 1. Start > Run > dcomcnfg > ENTER
>>>>>>>>>>>>>>> 2. In the left pane (Compnent Services.msc) expand
>>>>>>>>>>>>>>> "Component
>>>>>>>>>>>>>>> Services" >
>>>>>>>>>>>>>>> Computers
>>>>>>>>>>>>>>> 3. Right click "My Computer"
>>>>>>>>>>>>>>> 4. In the "Default Properties" tab tick the "Enable COM
>>>>>>>>>>>>>>> Internet
>>>>>>>>>>>>>>> Services
>>>>>>>>>>>>>>> on this computer" checkbox
>>>>>>>>>>>>>>> 5. Reboot remote server
>>>>>>>>>>>>>>> 6. Test again
>>>>>>>>>>>>>>> I can't recall if you need to add Access Permissions in
>>>>>>>>>>>>>>> the
>>>>>>>>>>>>>>> "COM
>>>>>>>>>>>>>>> Security" tab.
>>>>>>>>>>>>>>> -----
>>>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>>>> Shay,
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Thanks for the help, but I see the namespace
>>>>>>>>>>>>>>>> "root\MicrosoftIISv2" operate differently when
>>>>>>>>>>>>>>>> attempting to access it on a remote IIS6 computer
>>>>>>>>>>>>>>>> (default W2K3 installs).
>>>>>>>>>>>>>>>> The code examples you pointed out all work fine when
>>>>>>>>>>>>>>>> targeting the namespace locally, but I recieve access
>>>>>>>>>>>>>>>> denied messages when useing the same commands remotely.
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> I have tried from multiple machines to multiple
>>>>>>>>>>>>>>>> servers. The VB Script I supplied still works in all
>>>>>>>>>>>>>>>> cases, which leads me to believe I am not setting the
>>>>>>>>>>>>>>>> PowerShell WMI object authentication to use pktPrivacy
>>>>>>>>>>>>>>>> correctly.
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Any advice would be appreciated.
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Thanks
>>>>>>>>>>>>>>>> Bill
>>>>>>>>>>>>>>>> "Shay Levi" wrote:
>>>>>>>>>>>>>>>>> Pinging the remote machine before trying to recycle
>>>>>>>>>>>>>>>>> pools is a recommended practice.
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> Also, I wrote:
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer | %
>>>>>>>>>>>>>>>>> {$_.name}
>>>>>>>>>>>>>>>>> for those who are not familiar with gwmi and %, I
>>>>>>>>>>>>>>>>> should
>>>>>>>>>>>>>>>>> say
>>>>>>>>>>>>>>>>> that
>>>>>>>>>>>>>>>>> they are
>>>>>>>>>>>>>>>>> aliases for:
>>>>>>>>>>>>>>>>> gwmi = get-wmiobject
>>>>>>>>>>>>>>>>> % = foreach-object
>>>>>>>>>>>>>>>>> I should have written it like:
>>>>>>>>>>>>>>>>> get-wmiobject -class IIsApplicationPool -namespace
>>>>>>>>>>>>>>>>> "root\MicrosoftIISv2" -computer $computer |
>>>>>>>>>>>>>>>>> foreach-object
>>>>>>>>>>>>>>>>> {$_.name}
>>>>>>>>>>>>>>>>> -----
>>>>>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>>>>>> If you know the application pool name (the function
>>>>>>>>>>>>>>>>>> returns true/false respectively:
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> function Recycle-AppPool{
>>>>>>>>>>>>>>>>>> param([string]$server,[string]$appPool)
>>>>>>>>>>>>>>>>>> ([WMI]"\\$server\root\MicrosoftIISv2:IIsApplicationPo
>>>>>>>>>>>>>>>>>> o
>>>>>>>>>>>>>>>>>> l.
>>>>>>>>>>>>>>>>>> N
>>>>>>>>>>>>>>>>>> a
>>>>>>>>>>>>>>>>>> me
>>>>>>>>>>>>>>>>>> =
>>>>>>>>>>>>>>>>>> '
>>>>>>>>>>>>>>>>>> W
>>>>>>>>>>>>>>>>>> 3S
>>>>>>>>>>>>>>>>>> V
>>>>>>>>>>>>>>>>>> C/
>>>>>>>>>>>>>>>>>> A
>>>>>>>>>>>>>>>>>> ppPools/$appPool'").recycle();
>>>>>>>>>>>>>>>>>> if($? -ne 0}{$false} else {$true}
>>>>>>>>>>>>>>>>>> }
>>>>>>>>>>>>>>>>>> To get a list of application pool names:
>>>>>>>>>>>>>>>>>> $computer = "your Server name/ip"
>>>>>>>>>>>>>>>>>> gwmi -class IIsApplicationPool -namespace
>>>>>>>>>>>>>>>>>> "root\MicrosoftIISv2"
>>>>>>>>>>>>>>>>>> -computer
>>>>>>>>>>>>>>>>>> $computer | % {$_.name}
>>>>>>>>>>>>>>>>>> -----
>>>>>>>>>>>>>>>>>> Shay Levi
>>>>>>>>>>>>>>>>>> $cript Fanatic
>>>>>>>>>>>>>>>>>> http://scriptolog.blogspot.com
>>>>>>>>>>>>>>>>>>> I'm having some trouble finding the best method for
>>>>>>>>>>>>>>>>>>> recycling remote IIS6 application pools using
>>>>>>>>>>>>>>>>>>> PowerShell.
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> In the past this has always been very easy using VB
>>>>>>>>>>>>>>>>>>> script:
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Function PoolRecycle(strServer)
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> On Error Resume Next
>>>>>>>>>>>>>>>>>>> Err.Clear
>>>>>>>>>>>>>>>>>>> Dim objWMIService
>>>>>>>>>>>>>>>>>>> Dim colItems
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)