Tuesday, January 16, 2018

PowerCLI Tips

Set-PowerCLIConfiguration -InvalidCertificationAction IgnoreGet

Visual Studio Code
http://www.lucd.info/2016/04/23/visual-studio-code-powercli/

Load PowerCLi in PowerShell ISE
# Load Windows PowerShell cmdlets for managing vSphere


#Add VMware Module for newer OS
Imprt-Module VMware.PowerCLI -Scope CurrentUser
Import-Module Vmware.VimAutomation.Core
Import-Module Vmware.CLI

Suppress Deprecated Warning 
Set-PowerCLIConfiguration -DisplayDeprecatedWarnings $false -Scope User

Filters with PowerCLi
Get-VM | Where-Object {$_.NumCPU -gt 1} 

#Output via grid
Get-VM | out-GridView

#For each example
#Set Time for ESX Host
$hosts =get-vmhost 
foreach($i in $hosts){
$t = Get-Date
$dst = Get-VMHost $i | %{ Get-View $_.ExtensionData.ConfigManager.DateTimeSystem } 
$dst.UpdateDateTime((Get-Date($t.ToUniversalTime()) -format u))
}

#Property Names
Get-Process | Sort-Object -property Name


Getting the commands available in PowerCLI
Nouns and Verbs

Example of all the commands:
Get-VIcommand

Get-VICommand notice the heading command type - name - definition

Name is the commandlet (lots)

Get-Vicommand | Measure-Object = show you the total number of commandlets in PowerCLI

****You can sort by Groups to view them verb
the verb is the verb before the dash -  wait-|task  = verb is "wait"

Could sort by count = Get-Vicommand | Group-Object verb | Sort count

or even group by noun
Get-Vicommand | Group-Object noun | Sort count | format-wide -Column 3


Get-VIcommands = for powercli
Get-command = for powershell

More Examples -
 Get-command *vmscript

Built in HELP

help new-vm

TrainSignal
The pipeline
examine object
filter objects
export data

Pipeline down the pipeline
Get-Object - Process Object - Output Object

#Filter More than 2 criteria at the same time
Get-Process | Where-Object {($_.PriorityClass -eq “Normal”) -AND ($_.HandleCount -lt 300)}

#Export results to CVS



New-Item c:\temp -type directory
Get-Process | select name,handlecount | Export-Csv -delimiter "`t" -path "C:\temp\AllProcess.csv" -NoTypeInformation
notepad "C:\temp\AllProcess.csv"
Import-Csv -delimiter "`t" -path "C:\temp\AllProcess.csv"



$files = Get-ChildItem -Path C:\Folder -Filter *.txt -Recurse

$files.where({ ([datetime]$_.LastWriteTime).Date -eq '7/16/2016' })

1
2




No comments:

Post a Comment

Vmware NSX SSL creation 

Using OpenSSL for NSX Manager SSL import: Creates CSR and 4096 bit KEY Creating NSX 6.4.2 SSL    openssl req -out nsxcert.csr -newkey rsa:40...