![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Invoking a certain section of a powershell file. Hi, Forgive my incompetance, but it is possible to invoke a certain section of a powershell file? There is a certain section that needs to be invoked upon reboot but i am not sure about how to get it to do that without rerunning the entire file. Jason |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Invoking a certain section of a powershell file. I would 1) make the section a function 2) Add a switch flag as a parameter 3) first thing to check is that switch.. if it is true call function and exit. Brandon Shell --------------- Blog: http://www.bsonposh.com/ PSH Scripts Project: www.codeplex.com/psobject g> Hi, g> g> Forgive my incompetance, but it is possible to invoke a certain g> section of a powershell file? There is a certain section that needs g> to be invoked upon reboot but i am not sure about how to get it to do g> that without rerunning the entire file. g> g> Jason g> |
My System Specs![]() |
| | #3 (permalink) | ||||||||||||
| Guest | Re: Invoking a certain section of a powershell file. Could you give me a little more detail on those three steps? I'm somewhat confused on how to begin. Sorry ![]() "Brandon Shell [MVP]" wrote:
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #4 (permalink) | ||||||||||||
| Guest | Re: Invoking a certain section of a powershell file. greatbarrier86 wrote:
--------test.ps1--------- param($status) function all{ write-host "all function run" } function boot{ write-host "boot function run" } # If boot passed as argument run the boot function. if ($status -eq "boot"){ boot exit } # We either exit above, or run this last function. all ------------------------- PSH> ./test.ps1 all function run PSH> ./test.ps1 boot boot function run PSH> Marco -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com | ||||||||||||
My System Specs![]() | |||||||||||||
| | #5 (permalink) |
| Guest | Re: Invoking a certain section of a powershell file. You can invoke each line in the section of the script by slicing the array Get-Content returns. In the sample script the section begins on line 5 and ends on line 7. # the slice would begin on element 4 and end on element 6: @(get-content .\script.ps1)[4..6] | invoke-expression -< script.ps1 >- [datetime]::minValue $firstInput = read-host 'firstInput' $firstInput.toLower() # begin section "Current date & time:`n$(get-date -u %c)" $sectionInput = read-host 'sectionInput' $sectionInput.toUpper() # end section [datetime]::maxValue $lastInput = read-host 'lastInput' $lastInput.toLower() -< end >- -- Kiron |
My System Specs![]() |
| | #6 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Invoking a certain section of a powershell file. So, the following would be correct? param($status) function SQL { c:\Setup\MSEE\SQLEXPR.EXE reg import C:\Setup\MSEE\MseePort.reg Stop-Service SQLBrowser Set-Service SQLBrowser -startupType disabled } if ($status -eq "SQL") { SQL exit } This seems correct. At least from your example. "Marco Shaw [MVP]" wrote:
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||
| Guest | Re: Invoking a certain section of a powershell file. Kiron, Is there an advantage of that over using a function? J "Kiron" wrote:
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #8 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Invoking a certain section of a powershell file. Hmm...that does not appear to work. When i run it inside of a larger ps1 file, it errors out saying that Param is not a cmdlet...etc "greatbarrier86" wrote:
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #9 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Invoking a certain section of a powershell file. Something like this param($normalParam1,$normalparam2,[switch]$Section) function a{} function b{} function c{} function Section{ put section code here } if($Section){Section;exit} code... code... code... Brandon Shell --------------- Blog: http://www.bsonposh.com/ PSH Scripts Project: www.codeplex.com/psobject g> Could you give me a little more detail on those three steps? I'm g> somewhat confused on how to begin. Sorry ![]() g> g> "Brandon Shell [MVP]" wrote: g>
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #10 (permalink) |
| Guest | Re: Invoking a certain section of a powershell file. If you don't want to alter the script, yes, it's a way to extract and execute statements from it. Don't get me wrong, I'm all for calling functions from scripts, check out this post: news:F6624437-79A7-46F5-AC45-D9A80FE0597D@xxxxxx -- Kiron |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Right-button on folder invoking PowerShell | Hans Dingemans | PowerShell | 10 | 10-02-2007 03:39 PM |
| Invoking PowerShell functions with parameters from .NET: issues. | Roman Kuzmin | PowerShell | 0 | 04-23-2007 07:35 AM |
| Invoking PowerShell from WScript via COM | Joris van Lier | PowerShell | 1 | 03-28-2007 09:53 AM |
| Invoking Powershell from C# | Janssen | PowerShell | 3 | 02-16-2007 06:42 AM |
| invoking .net classes in powershell | Jim B | PowerShell | 4 | 01-05-2007 06:38 PM |