Hi Guys,
Firstly let me for-warn you that my PowerCLI is weak, generally I rely on PowerGUI and the VMware Community PowerPack for a BAU reporting and it has sufficed to date. The one report I need to run however simply does nothing and sits there
I tried running the VMware Community PowerPack report named "VMs Created over the last x days" however it doesn't seem to do anything or report any errors wither. So I thought I would resort to plan B and manually run the following script I found online ...
Using PowerCLI to report number of VMware VMs created | blockandfile.wordpress.com
As an added bonus I felt it would be beneficial to learn to invest some effort in learning some basic new skills. So I pasted the script into PowerCLI and I get nothing I was expecting to get prompted for credentials at least (see attached) . I do get a load of red text stating various terms are not recognized
Any ideas'?
It looks like the execution policy for PowerShell is not set correctly on your system.
Open a PowerShell or PowerCLI prompt, and execute Get-ExecutionPolicy.
What does it return ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Its states "Restricted" mate
silly question does it also matter if I run the 32bit or 64bit version?
Try setting the execution policy to RemoteSigned.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Did you unblock the downloaded script ?
See Scripting Wife Learns About Unblocking Files in PowerShell
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The text on that blog post is messed up, try removing lines 1 till 22, there should be a comment block.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
"Did you unblock the downloaded script ?"
I simply pasted the text directly into 32Bit PowerCLI so not sure it would be applicable would it? Still no banana
Can you attach the script you are trying to run ?
The code on that site you mentioned is messed up.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I pasted exactly the below into PowerCLI mate and hit return... (I also tried adding the vCenter IP manually into this script however it still didn't work) sorry if I have screwed up and caused confusion somehow
# Script: vmcreationreport.ps1
# Author: David Maldonado
# Date: 04/14/2011
# Examples:
# .\vmcreationreport.ps1
# .\vmcreationreport.ps1 vcenter01 7
vmcreationreport.ps1
Description
-----------
Script will prompt
for
vCenter server name and number of days, and then proceed.
.EXAMPLE
C:\PS> vmcreationreport.ps1 vcenter01 7
Description
-----------
Script will proceed directly with parameters entered, using vCenter server
"vcenter01"
and a
report window of previous
"7"
days.
#>
# Params
param
([
parameter
(
Mandatory
=
$true
)]
$vcenterServer
,[
parameter
(
Mandatory
=
$true
)]
$noDays
)
# Functions
function
reportVMCreation {
# Summary
Write-Host
"This script will report all the VMs created in $vcenterServer in the last $noDays days."
# Connect to VCenter Server
Connect-VIServer
$vcenterServer
# Get list of VMs
$VMs
=
Get-VM
| Sort Name
$VM
=
$VMs
| Select
-First
1
$Results
= @()
# Initialize counter
$counter
= 0
# Loop through VMs
foreach
(
$VM
in
$VMs
)
{
# Increase counter
$counter
++
# Display progress
Write-Progress
-Activity
"Getting VM Events"
-Status
"Processing VM $VM"
-PercentComplete
(100*(
$counter
/
$VMs
.count))
# Get Creation Events that meet date param
$Event
=
$VM
|
Get-VIEvent
-maxsamples
1000000
-Start
(
Get-Date
).AddDays(-
$noDays
)
-Types
Info |
where {
$_
.Gettype().Name
-eq
"VmCreatedEvent"
-or
$_
.Gettype().Name
-eq
"VmBeingClonedEvent"
-or
$_
.Gettype().Name
-eq
"VmBeingDeployedEvent"
}
# Measure results
$eventCount
=
$Event
|
Measure-Object
if
(
$eventCount
.count
-gt
0){
# Output results
$Result
=
new-object
PSObject
$Result
|
add-member
-membertype
NoteProperty
-name
"Name"
-value
$VM
$Result
|
add-member
-membertype
NoteProperty
-name
"Created Time"
-value
$Event
.CreatedTime
$Result
|
add-member
-membertype
NoteProperty
-name
"Created By"
-value
$Event
.UserName
$Result
|
add-member
-membertype
NoteProperty
-name
"Notes"
-value
$Event
.FullformattedMessage
$Results
+=
$Result
}
}
# Aggregate results to single .csv file
$Results
|
Select-Object
* |
Export-Csv
-notypeinformation
$(
"c:\temp\"
+
$vcenterServer
+
"_VM_Creation_Report.csv"
)
# Open .csv file
Start-Process
Excel
-ArgumentList
$(
"c:\temp\"
+
$vcenterServer
+
"_VM_Creation_Report.csv"
)
}
# Begin
reportVMCreation
# End
Try the attached script, I fixed the block comment.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thankyou for your help mate I created a new script note in PowerGUI using your attached content. When I ran it I was prompted for a vCenter server and days value. I entered them and starting to get optimistic as it was executing and taking a while longer however the messages pane shows the following error now
There were some broken lines, I attach a new version.
Btw, which PowerShell version are you using ?
Display the content of $PSVersionTable at the prompt.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I wish I understood this script better I feel pretty useless
You forget to provide the 2nd parameter that specifies how many days back the report should look.
If you call the script from the PowerCLI prompt, you can do
.\script.ps1 vcenter 30
The script will look back 30 days on the vCenter called vcenter.
If you want to run the script from the PowerGui editor, add the Input script parameters command to the toolbar
You can then do
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference