PowerCLI

 View Only
  • 1.  None of the powershell scripts will run

    Posted Dec 30, 2010 08:34 PM

    Hi, I am trying to run a powershell scripts but the problem is none of them will run. When I start the powershell, I get the following error message

    Add-PSSnapin : Cannot add Windows PowerShell snap-in VMware.VimAutomation.Core
    because it is already added. Verify the name of the snap-in and try again.
    At C:\WINDOWS\system32\WindowsPowerShell\v1.0\profile.ps1:1 char:13
    + Add-PSSnapin <<<<  VMware.VimAutomation.Core
        + CategoryInfo          : InvalidArgument: (VMware.VimAutomation.Core:Stri
       ng) [Add-PSSnapin], PSArgumentException
        + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
       dPSSnapinCommand

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    After that, I run this script

    # Report VMs created per month.
    # Before you begin run Alan's "Who Created that VM?" script.
    # http://www.virtu-al.net/2010/02/23/who-created-that-vm/
    function Get-VMCreationReport {
        Get-VM | Group {
            if ($_.CustomFields["CreatedOn"] -as [DateTime] -ne $null) {
                "{0:Y}" -f [DateTime]$_.CustomFields["CreatedOn"]
            } else {
                "Unknown"
            }
        }
    }

    # To create a CSV of this stuff try:
    # Get-VMCreationReport | select Count, Name, { $_.Group -as [String] } |
    #    export-csv c:\report.csv -NoTypeInformation

    ----------------------------------------------------------------------------------------------------------------------------------------------------

    The above mentioned script can be found at - http://blogs.vmware.com/vipowershell/2010/03/find-out-how-many-vms-youre-creating-a-month-with-this-simple-script.html

    Once I run that script here nothing happens I ran few other script also and I am getting the same error message

    Security Warning
    Run only scripts that you trust. While scripts from the Internet can be useful, this script can potentially harm your
    computer. Do you want to run C:\Documents and Settings\vcpguy\Desktop\question\Get-VMCreationReport.ps1?
    [D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D"): r
    [vSphere PowerCLI] C:\Documents and Settings\vcpguy\Desktop\question>

    Any idea what is going on here ?



  • 2.  RE: None of the powershell scripts will run

    Posted Dec 30, 2010 09:13 PM

    That first error shouldn't be a problem.

    That's because there is a Add-PSSnapin cmdlet in your script or in one of the profile files that tries to load the PowerCLI snapin.

    And that snapin is already loaded. Do you run the script from the PowerCLI prompt ?

    The shortcut that starts the PowerCLI prompt already loads the snapin.

    The 2nd error you are seeing is because you downloaded that script from the Internet and your security zones definitions tell PS to prompt you when it has to execute a script from the Internet zone.

    Is simple stuff working ?

    Can you do a Connect-VIServer to your vCenter or ESX(i) server ?

    Once connected can you do for example a Get-VMHost ?



  • 3.  RE: None of the powershell scripts will run

    Broadcom Employee
    Posted Dec 30, 2010 10:07 PM

    The 2nd error message can be avoided by creating a new file.

    Choose any text or script editor you like and copy&paste the code from the script that's downloaded from the Internet.

    Save the file as a .ps1 file and you're good to go.

    BUT,

    Looking at the script you posted as an example,

    It's obvious that nothing is happening.

    The script contains a function definition, but the function is never called, because there's a hash(#) in front of the last 2 lines. The hash symbol marks a line as comment.

    Remove the hashes and make sure that both lines are consolidated into one line.

    Arnim