.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

PowerShell ครั้งที่ 17 Options · View
paedotnet
Posted: Monday, February 18, 2008 9:03:52 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 344
Location: bkk

Building code at runtime

How to use  Invoke-Expression
Invoke-Expression is a way to execute an arbitrary string as a piece of code
example:


Invoke-Expression  '$a = 10 + 20 ; $a'
 

Output:
30

if you use this expression


Invoke-Expression '$a++'
$a
 

output:
31

ExecutionContext Variable
$ExecutionContext is the way to get at variable facilities provided by the powershell engine
example:


$ExecutionContext
 

output:
you can get output about Host,invokeprovider,sessionstate and invokecommand

$ExecutionContext.InvokeCommand | Get-Member

output:
 
 
How to use ExpandString
expandstring is  method  that lets you perform the same kind of variable interpolation that the powershell runtime does in scripts
example:
$test = 100
$s = 'this is $test'


you got : this is $test
if you want to get value of variable you can use this command
$ExecutionContext.InvokeCommand.ExpandString($s)
output:
this is 100

InvokeScript
invokescript is method . this method does the same thing that the invoke-expression
example:


$ExecutionContext.InvokeCommand.InvokeScript("100+200")


output:
300



[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by