Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use passthru to make sure the cmdlet is finished

I'm confused how -Passthru works.

From my understanding, the -Passthru parameter is used for passing the result of a cmdlet to the pipeline even the cmdlet itself doesn't produce any results.

I tried to write these code first: (I'm trying to recycle the IIS Application Pool without using Restart-WebAppPool cmdlet)

Stop-WebAppPool  -Name Foo
Start-WebAppPool -Name Foo

As I expected, it takes time to stop a IIS ApplicationPool, and since PowerShell does not wait for cmdlets to finish their job, the Stop-WebAppPool stops the application pool and returns. Because the application pool is not ready to get started, the Start-WebAppPool is called and error is thrown.

So I changed my code into these:

Stop-WebAppPool  -Name Foo
Start-Sleep -Seconds SomeValueNeedToGetMeasuredAndChanged
Start-WebAppPool -Name Foo

It works but not very elegent and flexible.

Then I add -Passthru to both of the cmdlets, it becomes:

Stop-WebAppPool  -Name Foo -Passthru
Start-WebAppPool -Name Foo -Passthru

Everything goes fine and no error is thrown.

In my case, I suppose:

The Stop/Start-WebAppPool cmdlet does not produce any results until the Application Pool really get stopped/started. Since -Passthru requires something to pass to the pipeline, PowerShell waits for the cmdlet produces its result, in this case, application pool get stopped/started, to execute the next one.

I can't find any useful manuals on either -Passthru or Stop-WebAppPool, so is it alright to do so in this case?

like image 537
wontasia Avatar asked Apr 14 '16 06:04

wontasia


People also ask

What does passthru do in PowerShell?

Well, ML, as you can see, using the passthru parameter forces Windows PowerShell to go ahead and pass newly created or modified objects instead of hiding them. By knowing when to use and not to use the parameter, great flexibility is gained. That is all there is to using the passthru parameter.

What is passthru command?

The passthru() function is similar to the exec() function in that it executes a command . This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser.

When would you use the Passthru parameter?

With the Passthru parameter, PowerShell returns the output in the console. For example, below notepad.exe process with ID 12344 will be stopped and the same will be displayed in the console with the Passthru parameter.

How do you wait for a process to finish in PowerShell?

The Wait-Process cmdlet waits for one or more running processes to be stopped before accepting input. In the PowerShell console, this cmdlet suppresses the command prompt until the processes are stopped. You can specify a process by process name or process ID (PID), or pipe a process object to Wait-Process .


1 Answers

-Passthru is used to pass current object (i.e. AppPool) further down the pipeline. I don't think that it's guaranteed, that Start/Stop-WebAppPool cmdlets will wait for current operation to complete before passing down AppPool object. It works now, but it could break later.

Solution? Use Restart-WebAppPool. Or if you really need to use Start/Stop cmdlets, do it like this:

$AppPool = 'Foo'
Stop-WebAppPool -Name $AppPool 
while((Get-WebAppPoolState -Name $AppPool).Value -ne 'Stopped'){
    Start-Sleep 1
}
Start-WebAppPool -Name $AppPool 
like image 96
beatcracker Avatar answered Oct 22 '22 22:10

beatcracker