VMware Cloud Community
baber
Expert
Expert

how can run a script in powercli

Dear all

Hi

i have installed power cli 5.5 and when run it type Connect-VIServer

it get server ip and user pass and connect to server when run get-vm command it show my vm info but now i want show date of each vm has been created i see this script

http://www.vmdev.info/?p=311

i put this script in file with name  1.ps  but when run that file don't show any output what is problem ?

BR

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
21 Replies
LucD
Leadership
Leadership

The code is only the function, you will still have to call it.

Add the line at the end of the .ps1 file.


Get-VMCreationTimes


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
baber
Expert
Expert

thanks

now i use attached scriipt but don't show any output or error or result just show

PowerCLI C:\> .\1.ps

PowerCLI C:\>

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
LucD
Leadership
Leadership

Works for me.

Are you connected to a vCenter (with a Connect-VIServer)?

Display the content of $global:defaultviservers.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
baber
Expert
Expert

first connect to vcenter with this command:

connect-viserver

now when run below command show :

PowerCLI C:\> $global:defaultviservers

Name                           Port  User

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

10.11.21.192                   443   VSPHERE.LOCAL\Administrator

10.11.21.191                   443   root

but now when run 1.ps show this :

PowerCLI C:\> .\1.ps

PowerCLI C:\>

i am so confused the script that i have attached was ok ?

BR

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
LucD
Leadership
Leadership

You seem to be connected to two vSphere servers (a vCenter and an ESXi node by the looks of it).

Yes, you script is ok.

It returns results for me when I run it my lab.

It could be that you are not collecting Tasks and Events in the vCenter.

Try the following script, it will report the VMs that were created in the last month.

$vms = Get-VM

$start = (Get-Date).AddMonths(-1)

Get-VIEvent -Entity $vms -Start $start -MaxSamples ([int]::MaxValue) |

where{$_ -is [VMware.Vim.VmCreatedEvent]} | %{

    $obj = [ordered]@{

        Name = $_.Vm.Name

        Created = $_.CreatedTime

        CreatedBy = $_.UserName

        IPAddress = $vm.Guest.IPAddress -join '|'

    }

    New-Object psobject -Property $obj

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
baber
Expert
Expert

now when run below command show :

PowerCLI C:\> $global:defaultviservers

Name                           Port  User

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

10.11.21.192                   443   VSPHERE.LOCAL\Administrator

10.11.21.191                   443   root

and when put this script in a .ps file with name 1.ps

$vms = Get-VM

$start = (Get-Date).AddMonths(-1)

Get-VIEvent -Entity $vms -Start $start -MaxSamples ([int]::MaxValue) |

where{$_ -is [VMware.Vim.VmCreatedEvent]} | %{

    $obj = [ordered]@{

        Name = $_.Vm.Name

        Created = $_.CreatedTime

        CreatedBy = $_.UserName

        IPAddress = $vm.Guest.IPAddress -join '|'

    }

    New-Object psobject -Property $obj

}

when run just open 1.ps file in a txt file

what do i have to do ?

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
LucD
Leadership
Leadership

Check the PowerShell execution policy that is set.
Do a Get-ExecutionPolicy, eventually change to RemoteSigned with the Set-ExecutionPolicy cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
baber
Expert
Expert

so thanks

i have typed this command in windows powershell  :

Set-ExecutionPolicy

after show

ExecutionPolicy:

type :

RemoteSigned

then select Y

now when run script that not work and just open script in txt file

this is my output :

PowerCLI C:\> $global:defaultviservers

Name                           Port  User

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

10.11.21.192                   443   VSPHERE.LOCAL\Administrator

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
LucD
Leadership
Leadership

Can you show the output of the Get-ExecutionPolicy cmdlet?

Also have a look at the Beginners Guide to Managing VMware using PowerShell from XtraVirt.

It contains a good intro to running PowerCLI scripts.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
baber
Expert
Expert

this is output :

PS C:\Users\Administrator> Get-ExecutionPolicy cmdlet

Get-ExecutionPolicy : Cannot bind parameter 'Scope'. Cannot convert value "cmdlet" to type

"Microsoft.PowerShell.ExecutionPolicyScope". Error: "Unable to match the identifier name cmdlet to a valid enumerator

name.  Specify one of the following enumerator names and try again: Process, CurrentUser, LocalMachine, UserPolicy,

MachinePolicy"

At line:1 char:21

+ Get-ExecutionPolicy cmdlet

+                     ~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-ExecutionPolicy], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetExecutionPolicyCommand

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
LucD
Leadership
Leadership

That should be only this

Get-ExecutionPolicy


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
baber
Expert
Expert

output is :

RemoteSigned

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
LucD
Leadership
Leadership

Can you show me a screenshot of how you start the .ps1 script?
Did you check that link I send earlier?
It contains in detail how to set up the environment to run PowerCLI scripts


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
baber
Expert
Expert

i have attached powercli command and pic from script when type 1.ps that script will be open

what is my problem?

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
LucD
Leadership
Leadership

Sorry, can't see any attachments in your latest reply.


What is the current content of the file 1.ps1?
And can you add a screenshot of the prompt from where you run the 1.ps1 script, and what is returns?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
baber
Expert
Expert

excuseme attached that

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
LucD
Leadership
Leadership

Thanks for that.
What is returned when hit <Enter> after you entered ./1.ps1?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
baber
Expert
Expert

when type 1.ps file and press enter

1.ps file will be open such as pevious attached

Please mark helpful or correct if my answer resolved your issue.
0 Kudos
LucD
Leadership
Leadership

Just noticed, can you change the filetype from .ps to .ps1


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos