Powershell Beginner

Author Message
TomRiddle

  • Total Posts : 620
  • Scores: 12
  • Reward points : 0
  • Joined: 2/7/2008
  • Location: Australia
  • Status: offline
Powershell Beginner Sunday, May 23, 2010 11:27 PM (permalink)
0
I am now on a mission to learn powershell and am working through the powershell2 for dummies book.
I have got to a spot that has stumped me. I put this into the powershell_ise as a profile startup script
 
function My-Custom-Function
{
Write-Host "Running my custom function!"
}
$psISE.CustomMenu.Submenus.Add("Run Custom Function",{My-Custom-Function},"Shift+Ctrl+f")
 
 
And get the following error
 
You cannot call a method on a null-valued expression.
At line:5 char:31
+ $psISE.CustomMenu.Submenus.Add <<<< ("Run Custom Function",{My-Custom-Function},"Shift+Ctrl+f")
    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

 
I have googled trhe error and just can't work out what the expression is that is null?
Any hints anyone? the code is copy and pasted straight from book.
 
BTW i have inbstalled PowerShell2 onto XP SP3 and get this.
 
$psVersionTable
Name                           Value                                                                                                                                      
----                           -----                                                                                                                                      
CLRVersion                     2.0.50727.3603                                                                                                                             
BuildVersion                   6.0.6002.18111                                                                                                                             
PSVersion                      2.0                                                                                                                                        
WSManStackVersion              2.0                                                                                                                                        
PSCompatibleVersions           {1.0, 2.0}                                                                                                                                 
SerializationVersion           1.1.0.1                                                                                                                                    
PSRemotingProtocolVersion      2.1
 

 
But it has installed into this folder
C:\WINDOWS\SYSTEM32\WindowsPowerShell\v1.0\
 
-join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
 
#1
    ebgreen

    • Total Posts : 8227
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    Re:Powershell Beginner Monday, May 24, 2010 1:54 AM (permalink)
    0
    Well, the SubMenus collection is most likely what is null. I would try:
    $psISE.CustomMenu.Submenus.Items.Add("Run Custom Function",{My-Custom-Function},"Shift+Ctrl+f")

    But that is just a guess. As for the install directory, that is how it does it. Confusing I know.
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      TomRiddle

      • Total Posts : 620
      • Scores: 12
      • Reward points : 0
      • Joined: 2/7/2008
      • Location: Australia
      • Status: offline
      Re:Powershell Beginner Tuesday, May 25, 2010 12:38 AM (permalink)
      0
      Thanks Ebgreen, I tried that and got 'close, but no cigar'

      Further investigation and I come across this page which explains it.
      [link=http://powershell.com/cs/blogs/aleksandar/archive/2009/05/04/what-happened-to-a-custommenu-property.aspx]http://powershell.com/cs/...stommenu-property.aspx[/link]

      This is the code I ended up with.

      function My-Custom-Function
      {
      Write-Host “Running my custom function!”
      }
      $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(“Run Custom Function”,{My-Custom-Function},”Shift+Ctrl+f”)


      -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
       
      #3
        ebgreen

        • Total Posts : 8227
        • Scores: 98
        • Reward points : 0
        • Joined: 7/12/2005
        • Status: offline
        Re:Powershell Beginner Tuesday, May 25, 2010 2:07 AM (permalink)
        0
        Glad you figured it out. I never use the ISE.
        "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
        Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
        http://www.visualbasicscript.com/m_47117/tm.htm
         
        #4
          TomRiddle

          • Total Posts : 620
          • Scores: 12
          • Reward points : 0
          • Joined: 2/7/2008
          • Location: Australia
          • Status: offline
          Re:Powershell Beginner Tuesday, May 25, 2010 10:53 AM (permalink)
          0
          if anything the ISE seems handy as an editor and for testing code snipits, i.e. you can copy and paste using CTL+C and CTL+V whereas powershell you have to type it or use edit from the console application bar icon menu
          -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
           
          #5
            ebgreen

            • Total Posts : 8227
            • Scores: 98
            • Reward points : 0
            • Joined: 7/12/2005
            • Status: offline
            Re:Powershell Beginner Wednesday, May 26, 2010 1:01 AM (permalink)
            0
            Right click the powershell icon and make sure Quick Edit is turned on. Then you can right click to copy or paste.
            "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
            Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
            http://www.visualbasicscript.com/m_47117/tm.htm
             
            #6
              TomRiddle

              • Total Posts : 620
              • Scores: 12
              • Reward points : 0
              • Joined: 2/7/2008
              • Location: Australia
              • Status: offline
              Re:Powershell Beginner Wednesday, May 26, 2010 10:18 PM (permalink)
              0
              Thanks again ebgreen, that works a treat.

              -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
               
              #7

                Online Bookmarks Sharing: Share/Bookmark

                Jump to:

                Current active users

                There are 0 members and 1 guests.

                Icon Legend and Permission

                • New Messages
                • No New Messages
                • Hot Topic w/ New Messages
                • Hot Topic w/o New Messages
                • Locked w/ New Messages
                • Locked w/o New Messages
                • Read Message
                • Post New Thread
                • Reply to message
                • Post New Poll
                • Submit Vote
                • Post reward post
                • Delete my own posts
                • Delete my own threads
                • Rate post

                2000-2012 ASPPlayground.NET Forum Version 3.9