header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Register scripts file
Last Post 09 May 2011 06:22 AM by Joffre Mota. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Joffre MotaUser is Offline
New Member
New Member
Posts:20
Avatar

--
09 May 2011 05:23 AM
    Why do I have to register my scripts file everytime when I try to use PowerShell?

    - I open the PowerShell
    - I try the command "Get-VM"
    And it brings back the following message:
    Windows PowerShell Copyright (C) 2006 Microsoft Corporation. All rights reserved.
    PS C:\Users\Administrator> Get-VM
    The term 'Get-VM' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again. At line:1 char:6 + Get-VM

    The underlined text, on PowerShell, is RED.

    If I use this
    . C:\hyperv\hyperv.ps1 before I try the "Get-VM" it works perfectly!



    Sorry about my english.
    JonathanUser is Offline
    Basic Member
    Basic Member
    Posts:109
    Avatar

    --
    09 May 2011 05:34 AM
    Hi Joffre,

    If I am reading your post correctly, it appears that you have a "." and a space in front of the C:\hyperv... line. If that is correct, it seems that the "Get-VM" is a function that is part of the script that you are "dot sourcing." By dot-sourcing the script, everything inside becomes resident in your console, so variables, functions, etc stay in memory.

    If this function is something that you use quite regularly, there are a couple of options. You can copy the function to your profile so that it loads in each of your sessions (be careful, there are four different profile types for different users/shells). You can also copy that to a specific script file (you will need to remove the function line and {}s to use it as a script file) and then you can call it from the directory that it is in.

    But the reason I stated above seems to be the reason why you can't execute the command without running the other line first.

    Let me know if you have further questions or if what I said is not clear.

    Regards,

    Jonathan Tyler
    http://powershellreflections.wordpress.com
    Jonathan Tyler
    http://powershellreflections.wordpress.com
    Follow Me On Twitter
    halr9000User is Offline
    PowerShell MVP, Site Admin
    Advanced Member
    Advanced Member
    Posts:565
    Avatar

    --
    09 May 2011 05:48 AM
    The Get-VM cmdlet does not come with PowerShell. It's only shipped as a part of a "snap-in" that is an extension of PowerShell's functionality. This snap-in ships with System Center Virtual Machine Manager. If you use the SCVMM console shortcut in your start menu, or open a console from within the SCVMM GUI, then you will find that the Get-VM cmdlet works automatically. This is because that console shortcut loads a console settings file (.PSC1 file extension) which includes instructions to load the SCVMM snap-in at startup. If you run PowerShell using the regular shortcut from your start menu, or type "powershell.exe" at the run line, then you won't have the snap-in executed, and thus, no Get-VM.

    For more help, try reading the help for the Get-PSSnapin and Add-PSSnapin cmdlets.

    get-pssnapin: http://technet.microsoft.com/en-us/...15338.aspx
    add-pssnapin: http://technet.microsoft.com/en-us/library/dd347601.aspx
    Community Director, PowerShellCommunity.org
    Co-host, PowerScripting Podcast
    Author, TechProsaic
    Joffre MotaUser is Offline
    New Member
    New Member
    Posts:20
    Avatar

    --
    09 May 2011 06:15 AM
    Posted By Jonathan on 09 May 2011 06:34 AM
    Hi Joffre,

    If I am reading your post correctly, it appears that you have a "." and a space in front of the C:\hyperv... line. If that is correct, it seems that the "Get-VM" is a function that is part of the script that you are "dot sourcing." By dot-sourcing the script, everything inside becomes resident in your console, so variables, functions, etc stay in memory.

    If this function is something that you use quite regularly, there are a couple of options. You can copy the function to your profile so that it loads in each of your sessions (be careful, there are four different profile types for different users/shells). You can also copy that to a specific script file (you will need to remove the function line and {}s to use it as a script file) and then you can call it from the directory that it is in.

    But the reason I stated above seems to be the reason why you can't execute the command without running the other line first.

    Let me know if you have further questions or if what I said is not clear.

    Regards,

    Jonathan Tyler
    http://powershellreflections.wordpress.com


    Yes, Get-VM is a function on hyperv.ps1 file. How do I copy the functions for my profile?


    Just for your knowledge:

    My ps1 file has hundreds of lines, and a lot of functions. I think its the PowerShell Hyper-V's default ps1 file (if it exists).
    Before the first function, the code below is written:
    Param ([switch]$quiet)

    #Global variables: #

    $VMState= @{"Running"=2 ; "Stopped"=3 ; "Paused"=32768 ; "Suspended"=32769 ; "Starting"=32770 ; "Snapshotting"=32771 ; "Saving"=32773 ; "Stopping"=32774 }
    $ReturnCode= @{"0"="OK" ; "4096"="Job Started" ; "32768"="Failed"; "32769"="Access Denied" ; "32770"="Not Supported"; "32771"="Unknown" ; "32772"="Timeout" ; "32773"="Invalid parameter" ; "32774"="System is in use" ; "32775"="Invalid state for this operation" ; "32776"="Incorrect data type" ; "32777"="System is not available" ; "32778"="Out of memory" }
    $BootMedia= @{"Floppy"=0 ; "CD"=1 ; "IDE"=2 ; "NET"=3 }
    $StartupAction =@{"None"=0 ; "RestartOnly"=1 ; "AlwaysStartup"=2}
    $ShutDownAction=@{"TurnOff"=0 ; "SaveState"=1 ; "ShutDown"=2}
    $Recoveryaction=@{"None"=0 ; "Restart"=1 ; "RevertToSnapShot"=2}
    $DiskType= @{"Fixed"=2; "Dynamic"=3; "Differencing"=4; "PhysicalDrive"=5}

    There are a lot of lines started by # that I didn't paste here, cause I think those are comments.




    I am totally newbie on PowerShell, please, be patient!
    And again, sorry about my terrible english!
    Joffre MotaUser is Offline
    New Member
    New Member
    Posts:20
    Avatar

    --
    09 May 2011 06:22 AM
    Posted By halr9000 on 09 May 2011 06:48 AM
    The Get-VM cmdlet does not come with PowerShell. It's only shipped as a part of a "snap-in" that is an extension of PowerShell's functionality. This snap-in ships with System Center Virtual Machine Manager. If you use the SCVMM console shortcut in your start menu, or open a console from within the SCVMM GUI, then you will find that the Get-VM cmdlet works automatically. This is because that console shortcut loads a console settings file (.PSC1 file extension) which includes instructions to load the SCVMM snap-in at startup. If you run PowerShell using the regular shortcut from your start menu, or type "powershell.exe" at the run line, then you won't have the snap-in executed, and thus, no Get-VM.

    For more help, try reading the help for the Get-PSSnapin and Add-PSSnapin cmdlets.

    get-pssnapin: http://technet.microsoft.com/en-us/...15338.aspx
    add-pssnapin: http://technet.microsoft.com/en-us/library/dd347601.aspx

    I see...

    Is it possible to register my ps1 file to have Get-VM and other functions always available when I start my session on PowerShell?


    Where do I use Get-PSSnapin -registered command?
    - If the answer is "
    On PowerShell ", it doesn't return any value. Even if I register my ps1 file before.

    If i try the just the command
    Get-PSSnapin , the result is:

    PS C:\Users\Administrator> Get-PSSnapIn

    Name : Microsoft.PowerShell.Core PS
    Version : 1.0
    Description : This Windows PowerShell snap-in contains Windows PowerShell manag ement cmdlets used to manage components of Windows PowerShell.

    Name : Microsoft.PowerShell.Host PS
    Version : 1.0
    Description : This Windows PowerShell snap-in contains cmdlets used by the Wind ows PowerShell host.

    Name : Microsoft.PowerShell.Management PS
    Version : 1.0
    Description : This Windows PowerShell snap-in contains management cmdlets used to manage Windows components.

    Name : Microsoft.PowerShell.Security PS
    Version : 1.0
    Description : This Windows PowerShell snap-in contains cmdlets to manage Window s PowerShell security.

    Name : Microsoft.PowerShell.Utility PS

    Version : 1.0
    Description : This Windows PowerShell snap-in contains utility Cmdlets used to manipulate data.

    PS C:\Users\Administrator>

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer