Hi Luc ,
could you suggest on the following requirments.
1:scheduling execution of following html report(discussed with you few days back)
2:oncereport is generated automatically sending to email address.
$vc = read-host "specify vcenter name"
$cred = Get-Credential
$date=get-date
Connect-VIServer -server $vc -Credential $cred|out-null
$path = 'C:\Users\in0079d6\Desktop\Technicolor_script'
$head = @'
<style>
body { background-color:#dddddd;
font-family:Tahoma;
font-size:12pt; }
td, th { border:1px solid black;
border-collapse:collapse; }
th { color:white;
background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
table { margin-left:50px; }
</style>
'@
$clusters = Import-Csv -path "C:\users\in0079d6\Desktop\Technicolor_script\EU_tc_CLUSTER.csv"
$fragments = @()
foreach ($line in $clusters) {
$cluster = Get-Cluster -name $line.clustername
$vms = Get-VM -location $cluster
$esx = Get-VMHost -Location $cluster
$fragments += Get-Cluster -name $cluster |
Select name |
ConvertTo-Html -Property name -Fragment -PreContent '<h2>clustername </h2>' |
Out-String
$ds_all = Get-Datastore -RelatedObject $cluster
$syslog_server = "10.x.x.x"
$fragments += Get-Datastore -RelatedObject $cluster |
Where-Object {($_.FreeSpaceGB) / ($_.CapacityGB) -le 0.15} |
Select Name |
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>DATASTORE WITH LESS THAN 15 PERCENT SPACE </h2>' |
Out-String
$fragments += $vms | Get-Snapshot |
Select Name, @{N = 'VM'; E = {$_.VM.Name}}, Created |
ConvertTo-Html -Property Name, VM, Created -Fragment -PreContent '<h2>SNAPSHOT_INFO</h2>' |
Out-String
$fragments += $vms | Where-Object {$_.PowerState -eq "poweredoff"}|
select name|
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>POWEREDOFF_VMS</h2>' |
Out-String
$fragments += $esx |
select name, @{N = 'vmkernel'; E = {$_ | Get-VMHostNetworkAdapter -VMKernel | Where-Object {$_.vmotionenabled -eq "true"}}}|
ConvertTo-Html -Property name, vmkernel -Fragment -PreContent '<h2>VMKERNEL_PORT_VMOTION</h2>' |
Out-String
$fragments += $esx |
select name, @{N = 'syslogserver'; E = {Get-VMHostSysLogServer -VMHost $_}}|
ConvertTo-Html -Property name, syslogserver -Fragment -PreContent '<h2>SYSLOGSERVER<h2>' |
Out-String
$fragments += $vms | Where-Object {$_.Guest.GuestFamily -eq 'windowsGuest' -and $_.ExtensionData.guest.toolsversionstatus -eq 'guesttoolsneedupgrade'}|
select name|
ConvertTo-Html -Property Name -Fragment -PreContent '<h2>WINDOWS_VM_TOOLS_NEED_UPGRADE</h2>' |
Out-String
$fragments += $esx | Get-VMHostService|? {$_.key -eq 'ntpd'}|
select vmhost, key, running|
ConvertTo-Html -Property vmhost, key, running -Fragment -precontent '<h2>NTP<h2>'|
Out-String
$fragments += $esx |
select name, build, version, model|
ConvertTo-Html -Property name, build, version, model -Fragment -PreContent '<h2>VERSION<h2>'|
Out-String
$fragments += $cluster|
select drsenabled, haenabled, HAAdmissionControlEnabled|
ConvertTo-Html -Property drsenabled, haenabled, HAAdmissionControlEnabled -Fragment -PreContent '<h2>CLUSTERPROPERTIES<h2>'|
Out-String
$fragments += Get-DatastoreCluster -Location(Get-Datacenter -Cluster $cluster)|
select name|ConvertTo-Html -Property name -Fragment -PreContent '<h2>DATASTORECLUSTER<h2>'|
Out-String
}
ConvertTo-HTML -head $head -PostContent $fragments |
Out-String | Out-File -FilePath "$path\feb19.html"
What can you use for the scheduling part?
The Windows Task Scheduler?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i am using windows task scheduler only .is there any powershellcommand to launch this and create basic task .
There are cmdlets to create a Scheduled Task.
Basically something like this.
It runs C:\Scripts\myscript.ps1 every day at 05:00
$pswd = 'VMware1!'
$sAction = @{
Execute = 'Powershell.exe'
Argument = '-NoProfile -WindowStyle Hidden -File C:\Scripts\myscript.ps1'
}
$action = New-ScheduledTaskAction @sAction
$sTrigger = @{
Daily = $true
At = "05:00"
}
$trigger = New-ScheduledTaskTrigger @sTrigger
$sTask = @{
TaskName = 'My Task'
Action = $action
Trigger = $trigger
User = $user
Password = $pswd
}
Register-ScheduledTask @sTask
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thnaksLuc iam going to check this .however currently powercli commands are available on powershell ise not on powershell.
will it make any changes on
Execute = 'Powershell.exe'
How do you mean?
When you installed PowerCLI (6.51 or higher) from the PowerSHell Gallery, it will be installed (depending on the Scope), in one of the directories listed in $env:PSModulePath.
When you start PowerShell this variable and module auto loading will make sure you have access to the PowerCLI modules.
Independent of you run PS from the command prompt, the ISE or VSC.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i belive its a primitive way of installtion on this machine not from powershell gallery .however i am in process of planning to upgrade to powershell core6.0 from gallery.
so in this case we need to do what imentioned in my last post.
Just take note that you will have to use pwsh.exe instead of powershell.exe for a cheduled task when you moved to PowerShell v6
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thnaksLuc.
Hi Luc ,
can you please suggest simplest way of converting password to encrypted .
You can do
$securePswd = ConvertTo-SecureString -AsPlainText -String $unsecurePswd -Force
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Guru ji.
this way also exposes password in script so this is not i wanted .
i am checking something like this to avoid any hardcoding of password in scripts.
https://www.altaro.com/msp-dojo/encrypt-password-powershell/
for some resons New-ScheduledTaskAction and New-ScheduledTaskTrigger are not availble.
These cmdlets are available in the ScheduledTasks module.
It was introduced in Windows 8 and Server 2012 with PS v3.
If you're using an older environment, you could use the Schedule.Service COM object.
See for example PowerShell – Managing Scheduled Tasks
On the credentials, you could also use the New-VICredentialStoreItem
Just make sure the scheduled task runs under the same account and on the same computer where you created the CredentialStoreItem.
Which is in fact the same limitation the solution you pointed to has.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i will check this on 2012 .
however for the credential following worked .however iamusing my domain pssword so it needs to be run once i reset my domin passord .
$path1 = 'C:\Users\user1\Desktop\folder1'
(get-credential).password | ConvertFrom-SecureString | set-content "$path\password.txt"
#it will store pssword in password.txt
$password = Get-Content "$path1\password.txt" | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential("domain\user1",$password)
connect-viserver -server vcentername -Credential $credential
You could run your scheduled tasks under a kind of service account.
One where the password doesn't change, and that is restricted to "logon as a batch job".
Ultimately, the best solution, security wise, is to use a Credential Store.
Like for example KeePass with the PoShKeePass module.
There are other Credential Store solutions with PowerShell support out there.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference