Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Are Powershell scripts compiled or interpreted?

Reply
 
Old 12-19-2007   #1 (permalink)
ChanKaiShi


 
 

Are Powershell scripts compiled or interpreted?

Hello,

I'm noticing significant delay between saving file and seeing changes during
script execution. Sample code is below. If I modify content within a
function, then changes are not available immediately and require several
repetitions before changes come live. Bigger files require even longer delay
before changes are see. Try for yourself and change version withing Function
Write-Host, save it and execute script on PS.

Write-Host "Version 1"
Test
Function Test()
{
Write-Host "Version5"
}

My System SpecsSystem Spec
Old 12-19-2007   #2 (permalink)
Shay Levi


 
 

Re: Are Powershell scripts compiled or interpreted?



You should call "test" after the function declaration, otherwise you'll get
an error:

Write-Host "Version 1"
# Test , this will return error. test function doesn't exist yet

Function Test() {
Write-Host "Version5"
}
Quote:

> Test
Version5


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hello,
>
> I'm noticing significant delay between saving file and seeing changes
> during script execution. Sample code is below. If I modify content
> within a function, then changes are not available immediately and
> require several repetitions before changes come live. Bigger files
> require even longer delay before changes are see. Try for yourself and
> change version withing Function Write-Host, save it and execute script
> on PS.
>
> Write-Host "Version 1"
> Test
> Function Test()
> {
> Write-Host "Version5"
> }

My System SpecsSystem Spec
Old 12-19-2007   #3 (permalink)
ChanKaiShi


 
 

Re: Are Powershell scripts compiled or interpreted?

Actually it works for me the way I posted. First execution it'll complain
about it and next execution it'll run just fine. That's why I think code is
compiled and first time you run it, it did not compile PS file yet and
complain about it. Try to run it again within same session and you'll see.

"Shay Levi" wrote:
Quote:

>
>
> You should call "test" after the function declaration, otherwise you'll get
> an error:
>
> Write-Host "Version 1"
> # Test , this will return error. test function doesn't exist yet
>
> Function Test() {
> Write-Host "Version5"
> }
>
Quote:

> > Test
>
> Version5
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > Hello,
> >
> > I'm noticing significant delay between saving file and seeing changes
> > during script execution. Sample code is below. If I modify content
> > within a function, then changes are not available immediately and
> > require several repetitions before changes come live. Bigger files
> > require even longer delay before changes are see. Try for yourself and
> > change version withing Function Write-Host, save it and execute script
> > on PS.
> >
> > Write-Host "Version 1"
> > Test
> > Function Test()
> > {
> > Write-Host "Version5"
> > }
>
>
>
My System SpecsSystem Spec
Old 12-19-2007   #4 (permalink)
Shay Levi


 
 

Re: Are Powershell scripts compiled or interpreted?

Saved as a ps1 file, restarted PowerShell and ran a few times:

### C:\Scripts\temp\tmp1.ps1 ###

Write-Host "Version 1"
Test
Function Test()
{
Write-Host "Version5"
}


########################

PS:1 >C:\Scripts\temp\tmp1.ps1
Version 1
The term 'Test' is not recognized as a cmdlet, function, operable program,
or script file. Verify the term and try again.
At C:\Scripts\temp\tmp1.ps1:2 char:5
+ Test <<<<

PS:2 >C:\Scripts\temp\tmp1.ps1
Version 1
The term 'Test' is not recognized as a cmdlet, function, operable program,
or script file. Verify the term and try again.
At C:\Scripts\temp\tmp1.ps1:2 char:5
+ Test <<<<

PS:3 >C:\Scripts\temp\tmp1.ps1
Version 1
The term 'Test' is not recognized as a cmdlet, function, operable program,
or script file. Verify the term and try again.
At C:\Scripts\temp\tmp1.ps1:2 char:5
+ Test <<<<



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Actually it works for me the way I posted. First execution it'll
> complain about it and next execution it'll run just fine. That's why I
> think code is compiled and first time you run it, it did not compile
> PS file yet and complain about it. Try to run it again within same
> session and you'll see.
>
> "Shay Levi" wrote:
>
Quote:

>> You should call "test" after the function declaration, otherwise
>> you'll get an error:
>>
>> Write-Host "Version 1"
>> # Test , this will return error. test function doesn't exist yet
>> Function Test() {
>> Write-Host "Version5"
>> }
Quote:

>>> Test
>>>
>> Version5
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hello,
>>>
>>> I'm noticing significant delay between saving file and seeing
>>> changes during script execution. Sample code is below. If I modify
>>> content within a function, then changes are not available
>>> immediately and require several repetitions before changes come
>>> live. Bigger files require even longer delay before changes are see.
>>> Try for yourself and change version withing Function Write-Host,
>>> save it and execute script on PS.
>>>
>>> Write-Host "Version 1"
>>> Test
>>> Function Test()
>>> {
>>> Write-Host "Version5"
>>> }

My System SpecsSystem Spec
Old 12-19-2007   #5 (permalink)
Mike Miller


 
 

Re: Are Powershell scripts compiled or interpreted?

That seems like a good indication to me that they aren't compiled :-) I may
be wrong though and somebody else can likely explain this better.

However, I can explain what's happening in your example. When you run a
powershell script in powershell, it's executed within the current process.
When you run the script the first time, the function is created within your
current powershell process. When you run the script a second time, it is
again within the current process which now has a function Test() declared
and it executes that previously declared function, *not* the one that is
declared after you call the function.

To help see this, try running the script, editing it to change the
write-host message, then run it again (from the same powershell process as
before).

Mike

"ChanKaiShi" <ChanKaiShi@xxxxxx> wrote in message
news:F88F1F8F-6B3F-4248-BE57-79EAD98F1FD8@xxxxxx
Quote:

> Hello,
>
> I'm noticing significant delay between saving file and seeing changes
> during
> script execution. Sample code is below. If I modify content within a
> function, then changes are not available immediately and require several
> repetitions before changes come live. Bigger files require even longer
> delay
> before changes are see. Try for yourself and change version withing
> Function
> Write-Host, save it and execute script on PS.
>
> Write-Host "Version 1"
> Test
> Function Test()
> {
> Write-Host "Version5"
> }

My System SpecsSystem Spec
Old 12-19-2007   #6 (permalink)
Oisin Grehan


 
 

Re: Are Powershell scripts compiled or interpreted?

On Dec 19, 12:13 pm, ChanKaiShi <ChanKai...@xxxxxx>
wrote:
Quote:

> Actually it works for me the way I posted. First execution it'll complain
> about it and next execution it'll run just fine. That's why I think code is
> compiled and first time you run it, it did not compile PS file yet and
> complain about it. Try to run it again within same session and you'll see.
>
>
>
> "Shay Levi" wrote:
>
Quote:

> > You should call "test" after the function declaration, otherwise you'll get
> > an error:
>
Quote:

> > Write-Host "Version 1"
> > # Test , this will return error. test function doesn't exist yet
>
Quote:

> > Function Test() {
> > Write-Host "Version5"
> > }
>
Quote:
Quote:

> > > Test
>
Quote:

> > Version5
>
Quote:

> > -----
> > Shay Levi
> > $cript Fanatic
> >http://scriptolog.blogspot.com
> > Hebrew weblog:http://blogs.microsoft.co.il/blogs/scriptfanatic
>
Quote:
Quote:

> > > Hello,
>
Quote:
Quote:

> > > I'm noticing significant delay between saving file and seeing changes
> > > during script execution. Sample code is below. If I modify content
> > > within a function, then changes are not available immediately and
> > > require several repetitions before changes come live. Bigger files
> > > require even longer delay before changes are see. Try for yourself and
> > > change version withing Function Write-Host, save it and execute script
> > > on PS.
>
Quote:
Quote:

> > > Write-Host "Version 1"
> > > Test
> > > Function Test()
> > > {
> > > Write-Host "Version5"
> > > }- Hide quoted text -
>
> - Show quoted text -
PowerShell scripts are interpreted. The experience you're getting is
due to scoping issues - you are probably dot sourcing it without
knowing, probably by using powershell plus (which dot sources its
temporary scripts when running them - not sure if that's a good idea,
tobias? karl?). Dot sourcing means that on the second run of the
script, the first definition of the function is still in memory; dot
sourcing runs a file in the current scope (typically the global
scope), not the file's nested scope.

- Oisin / x0n.
My System SpecsSystem Spec
Old 12-19-2007   #7 (permalink)
ChanKaiShi


 
 

Re: Are Powershell scripts compiled or interpreted?

Something screwy is going on. See this video capture of my screen
(http://blip.tv/file/get/Artisticchee...Failure756.wmv). Exact
behavior which I described.
?!??!!

"Shay Levi" wrote:
Quote:

> Saved as a ps1 file, restarted PowerShell and ran a few times:
>
> ### C:\Scripts\temp\tmp1.ps1 ###
>
> Write-Host "Version 1"
> Test
> Function Test()
> {
> Write-Host "Version5"
> }
>
>
> ########################
>
> PS:1 >C:\Scripts\temp\tmp1.ps1
> Version 1
> The term 'Test' is not recognized as a cmdlet, function, operable program,
> or script file. Verify the term and try again.
> At C:\Scripts\temp\tmp1.ps1:2 char:5
> + Test <<<<
>
> PS:2 >C:\Scripts\temp\tmp1.ps1
> Version 1
> The term 'Test' is not recognized as a cmdlet, function, operable program,
> or script file. Verify the term and try again.
> At C:\Scripts\temp\tmp1.ps1:2 char:5
> + Test <<<<
>
> PS:3 >C:\Scripts\temp\tmp1.ps1
> Version 1
> The term 'Test' is not recognized as a cmdlet, function, operable program,
> or script file. Verify the term and try again.
> At C:\Scripts\temp\tmp1.ps1:2 char:5
> + Test <<<<
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > Actually it works for me the way I posted. First execution it'll
> > complain about it and next execution it'll run just fine. That's why I
> > think code is compiled and first time you run it, it did not compile
> > PS file yet and complain about it. Try to run it again within same
> > session and you'll see.
> >
> > "Shay Levi" wrote:
> >
Quote:

> >> You should call "test" after the function declaration, otherwise
> >> you'll get an error:
> >>
> >> Write-Host "Version 1"
> >> # Test , this will return error. test function doesn't exist yet
> >> Function Test() {
> >> Write-Host "Version5"
> >> }
> >>> Test
> >>>
> >> Version5
> >>
> >> -----
> >> Shay Levi
> >> $cript Fanatic
> >> http://scriptolog.blogspot.com
> >> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
> >>> Hello,
> >>>
> >>> I'm noticing significant delay between saving file and seeing
> >>> changes during script execution. Sample code is below. If I modify
> >>> content within a function, then changes are not available
> >>> immediately and require several repetitions before changes come
> >>> live. Bigger files require even longer delay before changes are see.
> >>> Try for yourself and change version withing Function Write-Host,
> >>> save it and execute script on PS.
> >>>
> >>> Write-Host "Version 1"
> >>> Test
> >>> Function Test()
> >>> {
> >>> Write-Host "Version5"
> >>> }
>
>
>
My System SpecsSystem Spec
Old 12-19-2007   #8 (permalink)
ChanKaiShi


 
 

Re: Are Powershell scripts compiled or interpreted?

Yes I have powershell installed but I execute sample script inside class PS
1.0. See my video here
(http://blip.tv/file/get/Artisticchee...Failure756.wmv).
So you saying that it's result of having PowerShellPlus installed on my
machine even though I'm not using it's GUI at this time?
How can I prevent this from happening short of uninstalling PS+?


"Oisin Grehan" wrote:
Quote:

> On Dec 19, 12:13 pm, ChanKaiShi <ChanKai...@xxxxxx>
> wrote:
Quote:

> > Actually it works for me the way I posted. First execution it'll complain
> > about it and next execution it'll run just fine. That's why I think code is
> > compiled and first time you run it, it did not compile PS file yet and
> > complain about it. Try to run it again within same session and you'll see.
> >
> >
> >
> > "Shay Levi" wrote:
> >
Quote:

> > > You should call "test" after the function declaration, otherwise you'll get
> > > an error:
> >
Quote:

> > > Write-Host "Version 1"
> > > # Test , this will return error. test function doesn't exist yet
> >
Quote:

> > > Function Test() {
> > > Write-Host "Version5"
> > > }
> >
Quote:

> > > > Test
> >
Quote:

> > > Version5
> >
Quote:

> > > -----
> > > Shay Levi
> > > $cript Fanatic
> > >http://scriptolog.blogspot.com
> > > Hebrew weblog:http://blogs.microsoft.co.il/blogs/scriptfanatic
> >
Quote:

> > > > Hello,
> >
Quote:

> > > > I'm noticing significant delay between saving file and seeing changes
> > > > during script execution. Sample code is below. If I modify content
> > > > within a function, then changes are not available immediately and
> > > > require several repetitions before changes come live. Bigger files
> > > > require even longer delay before changes are see. Try for yourself and
> > > > change version withing Function Write-Host, save it and execute script
> > > > on PS.
> >
Quote:

> > > > Write-Host "Version 1"
> > > > Test
> > > > Function Test()
> > > > {
> > > > Write-Host "Version5"
> > > > }- Hide quoted text -
> >
> > - Show quoted text -
>
> PowerShell scripts are interpreted. The experience you're getting is
> due to scoping issues - you are probably dot sourcing it without
> knowing, probably by using powershell plus (which dot sources its
> temporary scripts when running them - not sure if that's a good idea,
> tobias? karl?). Dot sourcing means that on the second run of the
> script, the first definition of the function is still in memory; dot
> sourcing runs a file in the current scope (typically the global
> scope), not the file's nested scope.
>
> - Oisin / x0n.
>
My System SpecsSystem Spec
Old 12-20-2007   #9 (permalink)
Steven Hystad


 
 

Re: Are Powershell scripts compiled or interpreted?

The scope issue is due to the fact that you are dot sourcing the script
yourself.
Use an ampersand (&) rather than a period (.) to generate a separate scope
for the script.
Then make sure to declare all functions before using them.

"ChanKaiShi" <ChanKaiShi@xxxxxx> wrote in message
news:3D5C2455-71FA-4EED-8F51-91D25A2C4EA6@xxxxxx
Quote:

> Yes I have powershell installed but I execute sample script inside class
> PS
> 1.0. See my video here
> (http://blip.tv/file/get/Artisticchee...Failure756.wmv).
> So you saying that it's result of having PowerShellPlus installed on my
> machine even though I'm not using it's GUI at this time?
> How can I prevent this from happening short of uninstalling PS+?
>
>
> "Oisin Grehan" wrote:
>
Quote:

>> On Dec 19, 12:13 pm, ChanKaiShi <ChanKai...@xxxxxx>
>> wrote:
Quote:

>> > Actually it works for me the way I posted. First execution it'll
>> > complain
>> > about it and next execution it'll run just fine. That's why I think
>> > code is
>> > compiled and first time you run it, it did not compile PS file yet and
>> > complain about it. Try to run it again within same session and you'll
>> > see.
>> >
>> >
>> >
>> > "Shay Levi" wrote:
>> >
>> > > You should call "test" after the function declaration, otherwise
>> > > you'll get
>> > > an error:
>> >
>> > > Write-Host "Version 1"
>> > > # Test , this will return error. test function doesn't exist yet
>> >
>> > > Function Test() {
>> > > Write-Host "Version5"
>> > > }
>> >
>> > > > Test
>> >
>> > > Version5
>> >
>> > > -----
>> > > Shay Levi
>> > > $cript Fanatic
>> > >http://scriptolog.blogspot.com
>> > > Hebrew weblog:http://blogs.microsoft.co.il/blogs/scriptfanatic
>> >
>> > > > Hello,
>> >
>> > > > I'm noticing significant delay between saving file and seeing
>> > > > changes
>> > > > during script execution. Sample code is below. If I modify content
>> > > > within a function, then changes are not available immediately and
>> > > > require several repetitions before changes come live. Bigger files
>> > > > require even longer delay before changes are see. Try for yourself
>> > > > and
>> > > > change version withing Function Write-Host, save it and execute
>> > > > script
>> > > > on PS.
>> >
>> > > > Write-Host "Version 1"
>> > > > Test
>> > > > Function Test()
>> > > > {
>> > > > Write-Host "Version5"
>> > > > }- Hide quoted text -
>> >
>> > - Show quoted text -
>>
>> PowerShell scripts are interpreted. The experience you're getting is
>> due to scoping issues - you are probably dot sourcing it without
>> knowing, probably by using powershell plus (which dot sources its
>> temporary scripts when running them - not sure if that's a good idea,
>> tobias? karl?). Dot sourcing means that on the second run of the
>> script, the first definition of the function is still in memory; dot
>> sourcing runs a file in the current scope (typically the global
>> scope), not the file's nested scope.
>>
>> - Oisin / x0n.
>>
My System SpecsSystem Spec
Old 12-27-2007   #10 (permalink)
Karl Prosser[MVP]


 
 

Re: Are Powershell scripts compiled or interpreted?

No its not because of having PS+ installed. its just that PS+ editor has
different options for running a script in different scopes, and by
default it loads it into the global scope.

but b.t.w how are you running this.. typically when you apply a function
you want it to be in the global scope. which means, just like variable,
its not there until you've specified it once and then it hangs around
for the extend of the session that the console is open.. i.e open
powershell.exe

type

$a
then press enter

you'll get nothing back as the variable $a contains nothing.

now type
$a = 1
$a

then you'll see that $a is now one

then type

$a = "hello"
$a

and you'll see it is now hello. The environment is live and variables
change according to what the code sets it..

functions are the same.. you can dynamically change them

run
mytest

then you'll get an error because its currently not been applied to the
environment

then try
function mytest { 1 }

then try to run mytest again
and you'll get a 1 returned

then change mytest live to
function mytest { 1..10 }
and then when you run it again now you'll run the new version..


so what was happening before was

the first time you ran your code.. no function was applied (or the
previous version was).. and you ran the call to the code before you
actually applied it

then the next time you ran it in the same environment , you run the
function against the version you had previously applied, and then after
that, change what the definition is.

b.t.w how did you run the script.. did you save it as an external PS1
file and run it.. did you dotsource it in with . ? or did you just copy
paste it into the console?

-Karl

http://www.powershell.com



ChanKaiShi wrote:
Quote:

> Yes I have powershell installed but I execute sample script inside class PS
> 1.0. See my video here
> (http://blip.tv/file/get/Artisticchee...Failure756.wmv).
> So you saying that it's result of having PowerShellPlus installed on my
> machine even though I'm not using it's GUI at this time?
> How can I prevent this from happening short of uninstalling PS+?
>
>
> "Oisin Grehan" wrote:
>
Quote:

>> On Dec 19, 12:13 pm, ChanKaiShi <ChanKai...@xxxxxx>
>> wrote:
Quote:

>>> Actually it works for me the way I posted. First execution it'll complain
>>> about it and next execution it'll run just fine. That's why I think code is
>>> compiled and first time you run it, it did not compile PS file yet and
>>> complain about it. Try to run it again within same session and you'll see.
>>>
>>>
>>>
>>> "Shay Levi" wrote:
>>>
>>>> You should call "test" after the function declaration, otherwise you'll get
>>>> an error:
>>>> Write-Host "Version 1"
>>>> # Test , this will return error. test function doesn't exist yet
>>>> Function Test() {
>>>> Write-Host "Version5"
>>>> }
>>>> > Test
>>>> Version5
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>> Hebrew weblog:http://blogs.microsoft.co.il/blogs/scriptfanatic
>>>>> Hello,
>>>>> I'm noticing significant delay between saving file and seeing changes
>>>>> during script execution. Sample code is below. If I modify content
>>>>> within a function, then changes are not available immediately and
>>>>> require several repetitions before changes come live. Bigger files
>>>>> require even longer delay before changes are see. Try for yourself and
>>>>> change version withing Function Write-Host, save it and execute script
>>>>> on PS.
>>>>> Write-Host "Version 1"
>>>>> Test
>>>>> Function Test()
>>>>> {
>>>>> Write-Host "Version5"
>>>>> }- Hide quoted text -
>>> - Show quoted text -
>> PowerShell scripts are interpreted. The experience you're getting is
>> due to scoping issues - you are probably dot sourcing it without
>> knowing, probably by using powershell plus (which dot sources its
>> temporary scripts when running them - not sure if that's a good idea,
>> tobias? karl?). Dot sourcing means that on the second run of the
>> script, the first definition of the function is still in memory; dot
>> sourcing runs a file in the current scope (typically the global
>> scope), not the file's nested scope.
>>
>> - Oisin / x0n.
>>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
run PowerShell scripts from ASP or asp.net PowerShell
Powershell scripts not working with Vista powershell PowerShell
Can't run powershell scripts PowerShell
Powershell and Logon Scripts PowerShell
Powershell for test scripts PowerShell


Vista Forums 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 Ltd

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