Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PoSH Challenge Part 4

I did another couple of Levels in the  PoSH Challenge based on the python challenge and did a lot of fun things with PowerShell on the way,  and some nice posts will follow in this series ;-)

In this part I first will go on first with Level 5 from last post , if you have the attachment from last post PoSH Challenge Part 3  that showed how work with IronPython, you can load it like this :,

$list = Import-Clixml banner.xml

PS C:\Users\mvanorsouw> $list

95

14
#
5

70
#
5

1

15
#

...

Now what have we got there will be a difference between using the "Live" Python object we get when we paste in the code from last post PoSH Challenge Part 3 ( *note* watch out for spaces behind @` when pasting from the browser , these will result in errors):

 

 

PoSH> $wc = New-Object System.Net.WebClient                                                                             
PoSH> $r = $wc.DownloadString("http://www.pythonchallenge.com/pc/def/banner.p")                                         
PoSH>                                                                                                                   
PoSH> # Use IronPython to load Python 2.5 to translate pickle string                                                    
PoSH> [System.Reflection.Assembly]::LoadFile( 'C:\IronPython-1.1\IronPython.dll')                                       
                                                                                                                        
GAC    Version        Location                                                                                          
---    -------        --------                                                                                          
False  v2.0.50727     C:\IronPython-1.1\IronPython.dll                                                                  
                                                                                                                        
                                                                                                                        
PoSH>                                                                                                                   
PoSH> $ipe = new-object IronPython.Hosting.PythonEngine                                                                 
PoSH>                                                                                                                   
PoSH> $IPCode = @'                                                                                                      
>>                                                                                                                      
>> import clr                                                                                                           
>> import System                                                                                                        
>> import sys                                                                                                           
>>                                                                                                                      
>> sys.path.append(r'C:\Python25\Lib')                                                                                  
>> import pickle                                                                                                        
>> t = pickle.loads(p)                                                                                                  
>>                                                                                                                      
>> '@                                                                                                                   
>>                                                                                                                      
PoSH> $ipe.Globals.Add("p",$r)                                                                                          
PoSH> $ipe.Execute($IPCode)                                                                                             
PoSH>                                                                                                                   
PoSH> $list = $ipe.Globals['t'] |% {$_}                                                                                 
PoSH> $list                                                                                                             
                                                                                                                        
95                                                                                                                      
                                                                                                                        
14                                                                                                                      
#                                                                                                                       
5                                                                                                                       
                                                                                                                        
70                                                                                                                      
#                                                                                                                       
5                                                                                                                  
...
                                                                                                                
PoSH> $list | Get-Member                                                                                                
                                                                                                                        
                                                                                                                        
   TypeName: IronPython.Runtime.Tuple                                                                                   
                                                                                                                        
Name               MemberType            Definition                                                                     
----               ----------            ----------                                                                     
AddSequence        Method                System.Object AddSequence(Object other)                                        
CompareTo          Method                System.Int32 CompareTo(Object other)                                           
ContainsValue      Method                System.Boolean ContainsValue(Object item)                                      
CopyTo             Method                System.Void CopyTo(Array array, Int32 index)                                   
Equals             Method                System.Boolean Equals(Object obj)                                              
GetDynamicType     Method                IronPython.Runtime.Types.DynamicType GetDynamicType()                          
GetEnumerator      Method                System.Collections.IEnumerator GetEnumerator()                                 
GetHashCode        Method                System.Int32 GetHashCode()                                                     
GetLength          Method                System.Int32 GetLength()                                                       
GetNewArgs         Method                System.Object GetNewArgs()                                                     
GetSlice           Method                System.Object GetSlice(Int32 start, Int32 stop)                                
GetType            Method                System.Type GetType()                                                          
get_Count          Method                System.Int32 get_Count()                                                       
get_IsSynchronized Method                System.Boolean get_IsSynchronized()                                            
get_Item           Method                System.Object get_Item(Int32 index), System.Object get_Item(Slice slice)       
get_SyncRoot       Method                System.Object get_SyncRoot()                                                   
MultiplySequence   Method                System.Object MultiplySequence(Object count)                                   
ReverseMultiply    Method                System.Object ReverseMultiply(Object count)                                    
ToString           Method                System.String ToString()                                                       
__iadd__           Method                System.Object __iadd__(Object other)                                           
__imul__           Method                System.Object __imul__(Object count)                                           
Item               ParameterizedProperty System.Object Item(Int32 index) {get;}, System.Object Item(Slice slice) {get;} 
Count              Property              System.Int32 Count {get;}                                                      
IsSynchronized     Property              System.Boolean IsSynchronized {get;}                                           
SyncRoot           Property              System.Object SyncRoot {get;}                                                  
                                                                                                                        
                                                                                                                        
PoSH> get-buffer | out-file event5.html                                                                                 
 

  You see that with the method we used in last post we get the "Real" IronPython object back to work with

When we look at the imported banner.xml file , we see that we only get back the data (but usable without IronPython  ;-) ) :

PoSH> $list | Export-Clixml banner.xml                                                                                  
PoSH> $list = Import-Clixml banner.xml                                                                                  
PoSH> $list | gm                                                                                                        
                                                                                                                        
                                                                                                                        
   TypeName: Deserialized.IronPython.Runtime.Tuple                                                                      
                                                                                                                        
Name           MemberType Definition                                                                                    
----           ---------- ----------                                                                                    
Count          Property   System.Int32 {get;set;}                                                                       
IsSynchronized Property   System.Boolean {get;set;}                                                                     
SyncRoot       Property   System.Management.Automation.PSObject {get;set;}                                              
                                                                                                                        
                                                                                                                        
PoSH> $list[0] | gm                                                                                                     
                                                                                                                        
                                                                                                                        
   TypeName: System.String                                                                                              
                                                                                                                        
Name             MemberType            Definition                                                                       
----             ----------            ----------                                                                       
Clone            Method                System.Object Clone()                                                            
CompareTo        Method                System.Int32 CompareTo(Object value), System.Int32 CompareTo(String strB)        
Contains         Method                System.Boolean Contains(String value)                                            
...

TrimEnd          Method                System.String TrimEnd(Params Char[] trimChars)                                   
TrimStart        Method                System.String TrimStart(Params Char[] trimChars)                                 
Chars            ParameterizedProperty System.Char Chars(Int32 index) {get;}                                            
Length           Property              System.Int32 Length {get;}                                                       
                                                                                                                        
                                                                                                                        
   TypeName: System.Int32                                                                                               
                                                                                                                        
Name        MemberType Definition                                                                                       
----        ---------- ----------                                                                                       
CompareTo   Method     System.Int32 CompareTo(Int32 value), System.Int32 CompareTo(Object value)                        
Equals      Method     System.Boolean Equals(Object obj), System.Boolean Equals(Int32 obj)                              
GetHashCode Method     System.Int32 GetHashCode()                                                                       
GetType     Method     System.Type GetType()                                                                            
GetTypeCode Method     System.TypeCode GetTypeCode()                                                                    
ToString    Method     System.String ToString(), System.String ToString(IFormatProvider provider), System.String ToS... 
                                                                                                                        
                                                                                                                        
PoSH> get-buffer | out-file event5.html                                                                                 

 

but for the solution of this Level this will make no difference, with both versions we got Int-String  pairs back, and can use them like this :

"$($list |% {$_[0] * $_[1]})"

Configure console window and buffer width to 95 for best results

 

That's it for this level in next post I will be back with a wrapper for 7zip ;-)

I did a lot of interesting things in this challenge but more about that in later posts ;-)

Greetings /\/\o\/\/

Published Monday, January 14, 2008 4:23 PM by MoW

Comments

No Comments
Anonymous comments are disabled