Dynamically Create Script Blocks

It is sometimes necessary to dynamically create script blocks. Here is an example how and why:

function Get-RemoteLog($name) {
$code = "Get-EventLog -LogName $name -Newest 5 -EntryType Error "
$sc = $executioncontext.InvokeCommand.NewScriptBlock($code)

Invoke-Command -ComputerName myServer -ScriptBlock $sc
}
Get-RemoteLog "System"

The function Get-Remotelog accepts a log name and then retrieves the last five error records from that log. Since the name of the log submitted in $name needs to be sent to the remote system, the function first has to resolve this variable locally before it creates the script block and sends it to the remote machine. If you had submitted the code directly, the remote machine would try and resolve the $name variable, which would not exist on the remote system.

Twitter This Tip! ReTweet this Tip!


Posted Dec 03 2009, 08:00 AM by ps1

Comments

Twitter Trackbacks for Dynamically Create Script Blocks - Power Tips - PowerShell.com [powershell.com] on Topsy.com wrote Twitter Trackbacks for Dynamically Create Script Blocks - Power Tips - PowerShell.com [powershell.com] on Topsy.com
on 12-03-2009 10:15 AM

Pingback from  Twitter Trackbacks for                 Dynamically Create Script Blocks - Power Tips - PowerShell.com         [powershell.com]        on Topsy.com

Aleksandar wrote re: Dynamically Create Script Blocks
on 12-03-2009 1:15 PM

For this particular case we can make it simpler by using ArgumentList parameter.

function Get-RemoteLog ($name) {

   Invoke-Command -ComputerName myServer -ScriptBlock {param ($logname) Get-EventLog -LogName $logname -Newest 5 -EntryType Error} -ArgumentList $name

}

Get-RemoteLog System

Here is an explanation from Invoke-Command help topic:

PS> get-help invoke-command -parameter argumentlist

-ArgumentList <Object[]>

   Supplies the values of local variables in the command. The variables in the command are replaced by these values be

   fore the command is run on the remote computer. Enter the values in a comma-separated list. Values are associated w

   ith variables in the order that they are listed. The alias for ArgumentList is "Args".

   The values in ArgumentList can be actual values, such as "1024", or they can be references to local variables, such

    as "$max".

   To use local variables in a command, use the following command format:

   {param($<name1>[, $<name2>]...) <command-with-local-variables>} -ArgumentList <value | $local-variable>

   The "param" keyword lists the local variables that are used in the command. The ArgumentList parameter supplies the

    values of the variables, in the order that they are listed.

-aleksandar

powershellers.blogspot.com

Dynamic Code in Powershell « Tome's Land of IT wrote Dynamic Code in Powershell &laquo; Tome&#039;s Land of IT
on 02-10-2010 5:22 PM

Pingback from  Dynamic Code in Powershell « Tome's Land of IT

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.