Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Invoking a certain section of a powershell file.

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-07-2007   #1 (permalink)
greatbarrier86
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 SpecsSystem Spec
Old 11-07-2007   #2 (permalink)
Brandon Shell [MVP]
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 SpecsSystem Spec
Old 11-07-2007   #3 (permalink)
greatbarrier86
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:
Quote:

> 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 SpecsSystem Spec
Old 11-07-2007   #4 (permalink)
Marco Shaw [MVP]
Guest


 

Re: Invoking a certain section of a powershell file.

greatbarrier86 wrote:
Quote:

> 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
Here's a simple example:

--------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 SpecsSystem Spec
Old 11-07-2007   #5 (permalink)
Kiron
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 SpecsSystem Spec
Old 11-07-2007   #6 (permalink)
greatbarrier86
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:
Quote:

> greatbarrier86 wrote:
Quote:

> > 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
>
> Here's a simple example:
>
> --------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 SpecsSystem Spec
Old 11-07-2007   #7 (permalink)
greatbarrier86
Guest


 

Re: Invoking a certain section of a powershell file.

Kiron,

Is there an advantage of that over using a function?

J

"Kiron" wrote:
Quote:

> 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 SpecsSystem Spec
Old 11-07-2007   #8 (permalink)
greatbarrier86
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:
Quote:

> 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:
>
Quote:

> > greatbarrier86 wrote:
Quote:

> > > 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
> >
> > Here's a simple example:
> >
> > --------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 SpecsSystem Spec
Old 11-07-2007   #9 (permalink)
Brandon Shell [MVP]
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>
Quote:
Quote:

>> 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 SpecsSystem Spec
Old 11-07-2007   #10 (permalink)
Kiron
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 SpecsSystem Spec
Closed Thread

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


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51