Hi guys,
Im new in the power cli world !!
Hi guys,
Im new in the powercli world and i need help and read/practice a lot.
I have a problem when a create a task sheduler in W2K8R2 i got an error and the folowwing param is not
recognized by the script when i add the cli add-PSSnapin VMware.VimAutomation.Core
:
param (
[String[]]$vCenters
)
When i remove the script recognize the param !
----------------------------------------------------
add-PSSnapin VMware.VimAutomation.Core
param (
[String[]]$vCenters
)
$fecha = get-date -uFormat "%d%m%y"
I check few posts with people that have the same issue but i dont find the solution.
I need to but a wildcard ?
Many Thanks
Carlos
You can add a datetime stamp to the filename. Like this
$alarmas | out-file "C:\ReportesvCENTER\Vcenter_report-$(Get-Date -f yyyyMMdd-hhmm)-$fecha.txt
You can avoid the PSSnapin error by using the Try-Catch construction I gave earlier.
Did you actually try that ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you include the error message you get ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LuCD
Thanks for your quicly reply and sorry for my delay i was outside of the office .
Here the outoputs :
Ouput without Add-PSSnapin VMware.VimAutomation.Core
PowerCLI C:\PS-Scripts> .\Get-alarms-Pav.ps1 Carlos
Date: 02/02/2016
Time: 22:27
Getting the alarms from vCenters.
Getting alarms from .
PowerCLI C:\PS-Scripts>
NOTE :The output are send it a txt file
Ouput with Add-PSSnapin VMware.VimAutomation.Core :
PowerCLI C:\PS-Scripts> .\Get-alarms-Pav.ps1 Carlos
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:\PS-Scripts\Get-alarms-Pav.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
The term 'param' is not recognized as the name of a cmdlet, function, script fi
le, or operable program. Check the spelling of the name, or if a path was inclu
ded, verify that the path is correct and try again.
At C:\PS-Scripts\Get-alarms-Pav.ps1:2 char:6
+ param <<<< (
+ CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFou
ndException
+ FullyQualifiedErrorId : CommandNotFoundException
Now the output is send but without the VC field empty . Now you see that Add-PSSnapin is alredy added
but this afternoon i dont receive the same message but i see the same errror when i add-PSSnain at the top
of the script the param is not recognized. Also think this is the reason that i cannot excute vmware powercli
from task scheduler because i red an post from Alan that Add-PSSnapin VMware.VimAutomation.Core is mandatory
VC : Carlos
EntityType : DistributedVirtualSwitch
Alarm : VSphere Distributed Switch
Entity : n1kv-VSM
Status : red
Time : 14/12/2015 15:23:35
Acknowledged : False
AckBy :
AckTime :
I also attach for your the script.
Thanks
Carlos
Like the error message states, the PSSnapin already seems to be added.
This is done automatically when you start PowerCLI with the shortcut that was installed together with PowerCLI.
One way to avoid the error, is to test on the presence of the PSSnapin.
You can use a Try{} Catch{} construct for that. Put something like this at the top of your script.
Try
{
Get-PSSnapin -Name VMware* -ErrorAction Stop | Out-Null
}
Catch
{
Add-PSSnapin -Name VMware*
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Still with the same issue, with your suggestion i fot the same errror. Maybe is a bug ?
PowerCLI C:\PS-Scripts> .\For-Lucd.ps1 Carlos
The term 'param' is not recognized as the name of a cmdlet, function, script fi
le, or operable program. Check the spelling of the name, or if a path was inclu
ded, verify that the path is correct and try again.
At C:\PS-Scripts\Get-alarms-Pav.ps1:12 char:6
+ param <<<< (
+ CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFou
ndException
+ FullyQualifiedErrorId : CommandNotFoundException
Date: 03/02/2016
Time: 15:22
Getting the alarms from vCenters.
Getting alarms from .
PowerCLI C:\PS-Scripts>
Best regards
But that is another error, now it states that the cmdlet param is not found.
Can you attach the script you are using as a file to this thread, I suspect there might a problem in the .ps1 file
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
A bit confused now.
The messages seem to indicate that you start with: .\For-Lucd.ps1 Carlos
But the error message states Get-alarms-Pav.ps1:12, but on line 12 I don't see a param statement I'm afraid
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Lucd,
I change the original path to Luc \For-Lucd.ps1 Carlos 🙂 , i forget to rename the name of the script.
sorry
regardss
But still there is no param on line 12.
Perhaps you could include a screenshot of the run of the script you attached earlier ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Lucd,
Here the script :
param (
[String[]]$vCenters
)
$fecha = get-date -Format D
$a = Get-Date
"Date: " + $a.ToShortDateString()
"Time: " + $a.ToShortTimeString()
Function Get-TriggeredAlarms {
param (
$vCenter = $(throw "A vCenter must be specified."),
[System.Management.Automation.PSCredential]$credential
)
if ($credential) {
$vc = Connect-VIServer $vCenter -Credential $credential
}
else {
$vc = Connect-VIServer x.x.x.x-Force -User DOMAIN\carlos -Password pepe
}
if (!$vc) {
Write-Host "Failure connecting to the vCenter $vCenter."
exit
}
$rootFolder = Get-Folder -Server $vc "Datacenters"
foreach ($ta in $rootFolder.ExtensionData.TriggeredAlarmState) {
$alarm = "" | Select-Object VC, EntityType, Alarm, Entity, Status, Time, Acknowledged, AckBy, AckTime
$alarm.VC = $vCenter
$alarm.Alarm = (Get-View -Server $vc $ta.Alarm).Info.Name
$entity = Get-View -Server $vc $ta.Entity
$alarm.Entity = (Get-View -Server $vc $ta.Entity).Name
$alarm.EntityType = (Get-View -Server $vc $ta.Entity).GetType().Name
$alarm.Status = $ta.OverallStatus
$alarm.Time = $ta.Time
$alarm.Acknowledged = $ta.Acknowledged
$alarm.AckBy = $ta.AcknowledgedByUser
$alarm.AckTime = $ta.AcknowledgedTime
$alarm
}
Disconnect-VIServer -Server * -Force -Confirm:$False
}
Write-output ("Getting the alarms from {0} vCenters." -f $vCenters.Length)
$alarms = @()
foreach ($vCenter in $vCenters) {
Write-Host "Getting alarms from $vCenter."
$alarms += Get-TriggeredAlarms $vCenter
}
$alarmas | out-file C:\ReportesvCENTER\Vcenter_report-$fecha.txt
$ParentbackupLocation = "C:\ReportesvCENTER\"
$ResourceFileLocation = $ParentBackupLocation + "Resources\"
$BackupLocation = $ResourceFileLocation + "{0:yyyy-MM-dd-tt}" -f (get-date)
$BackupLocationtest = Test-Path -path $BackupLocation
If ($BackupLocationtest -eq $false) {mkdir $BackupLocation | out-null}
$Dateformat = get-date -f MMddyyyy
$filelocation=$BackupLocation + "\Vcenter_Report-" + $Dateformat + ".txt"
The last part :
From $alarmas | out-file C:\ReportesvCENTER\Vcenter_report-$fecha.txt . >>>>>>>>>>>>>>>>>>< Do you know how can a send a new output to the folder e.g 20160203? Either the re-writhe the file Vcenter_report-$fecha.txt.
Many thanks
Carlos
I can't reproduce the error you are getting with that script I'm afraid.
Can you send a sceenshot of when you run that script ?
On the output, do I understand correctly that you want a folder per day ?
For example: C:\ReportesvCENTER\20160203\Vcenter_Report.txt
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can add a datetime stamp to the filename. Like this
$alarmas | out-file "C:\ReportesvCENTER\Vcenter_report-$(Get-Date -f yyyyMMdd-hhmm)-$fecha.txt
You can avoid the PSSnapin error by using the Try-Catch construction I gave earlier.
Did you actually try that ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi lucD,
Yes a try this, i reply you yesterday the logs. "param is not recognise .
You upload my script in your lab and work as expected?
With that sentence :
$alarmas | out-file "C:\ReportesvCENTER\Vcenter_report-$(Get-Date -f yyyyMMdd-hhmm)-$fecha.txt
I can erase :
$ParentLocation | out-file C:\ReportesvCENTER\Vcenter_report-$fecha.txt
$ParentbackupLocation = "C:\ReportesvCENTER\"
$ResourceFileLocation = $ParentBackupLocation + "Resources\"
$BackupLocation = $ResourceFileLocation + "{0:yyyy-MM-dd-tt}" -f (get-date)
$BackupLocationtest = Test-Path -path $BackupLocation
If ($BackupLocationtest -eq $false) {mkdir $BackupLocation | out-null}
$Dateformat = get-date -f MMddyyyy
$filelocation=$BackupLocation + "\Vcenter_Report-" + $Dateformat + ".txt"
Thanks
Carlos
Yes, but that "param" error is in no way related to the Try-Catch code.
Do you still have the script where you tried that Try-Catch ?
I would like to see it, if possible.
Does it still give this "param" error ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Done !! :smileygrin:
Im happy!
five stars!
Hi LucD,
Hope that you are fine!
You know a way for send e.g all the file that contains February-2016 to a folder february 2016? or easily send old files to a Folder"history"=
Best regards
Carlos
You can add attachments to an email with the Send-MailMessage cmdlet
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference