Recommendations for powershell profile

As i showed in my post about creation of powershell profile you can add into profile your own frequently used functions.

I recommend to name them with permanent prefix. For example i create all my functions as moguy-cdp, moguy-esxi and so on.

In this case you don’t need to remember the name of your function to run, just start typing as moguy… and autocompletion will show you all your functions.

for example:

function moguy-vms

{

Get-VMHost | Get-VM | Select-Object Name, PowerState, NumCpu, MemoryMB, VMhost, @{N="Datastore";E={Get-Datastore -vm $_}}, UsedSpaceGB, ProvisionedSpaceGB, @{Name='ToolsVersion';Expression={$_.Guest.ToolsVersion}}, @{Name=’VMHostVersion’;Expression={$_.VMHost.Version}},Version, @{N="IP Address";E={@($_.guest.IPAddress[0])}},@{Name=’Cluster’;Expression={$_.VMHost.Parent}}, @{N="PortGroup";E={Get-VirtualPortGroup -VM $_}}, @{N="owner1";E={$_.customfields.item("owner1")}} | Export-Csv 
Read the rest

How configure ssh_config for windows openssh client, for example for PubkeyAcceptedKeyTypes=+ssh-dss

Since win10 you have openssh ssh.exe in

C:\Windows\System32\OpenSSH

So to use passwordless ssh connection you maybe need to provide ssh.exe “PubkeyAcceptedKeyTypes=+ssh-dss”

For this you can create ssh_config file anywhere (for example in c:\users\yourname\.ssh near your id_dsa private key file) with only one line:

PubkeyAcceptedKeyTypes=+ssh-dss

so now you can:

ssh yourname@1.1.1.1 -F "c:\users\yourname\.ssh\ssh_config"

where 1.1.1.1 for example your ssh server, san_switch, HPE virtual connect manager, nimble storage and so on

ps
the same is true for openssh server installed from home site on win2016 server
only ssh.exe file will be in
“C:\Program Files\OpenSSH-Win64” (by default installation)… Read the rest

Use powershell module to automate Nimble storage administration

Install Nimble module for powershell

Install-Module -Name HPENimblePowerShellToolkit -RequiredVersion 3.0.0

Use below script to connect to nimble SAN storage, and create LUN, Access InitiatorGroup and assign the LUN access group

import-module HPENimblePowerShellToolkit
#this section for inputs, what should be changed
$arrayname = "nimble-group02.itforce.local"
$nm_uid = "admin"
$nm_password = ConvertTo-SecureString -String "[your-password]" -AsPlainText -Force
$nm_cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $nm_uid,$nm_password
Connect-NSGroup -group $arrayname -credential $nm_cred -IgnoreServerCertificate

# The following commands are used if creating an FC connection
$servername="cbuat"
$wwn01="50:01:43:80:26:66:b2:e4"
$wwn02="50:01:43:80:26:66:b2:e6"
$volumename="cbuat-lun001"

$port0=$servername+"_p1"
$port1=$servername+"_p2"
$description="Initiator Group for ”+$servername
$performancepolicyid=Get-NSPerformancePolicy -name default | select -ExpandProperty id

New-NSInitiatorGroup –name $servername –access_protocol fc –description 
Read the rest

How to check that ntp server is configured properly for all esxi

#To get time and ntp info for  all esxi :

Get-VMHost | Sort Name | Select Name,  @{N="NTPServer";E={$_ |Get-VMHostNtpServer}},   Timezone,  @{N="CurrentTime";E={(Get-View $_.ExtensionData.ConfigManager.DateTimeSystem) | Foreach {$_.QueryDateTime().ToLocalTime()}}},  @{N="ServiceRunning";E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq "ntpd"}).Running}} ` | Format-Table -AutoSize

or 

Get-VMHost | Get-VMHostService | Where-Object {$_.key -eq "ntpd"} | select vmhost, label, Key, Policy, Running, Required |format-table -autosize

# Remove wrong NTP Server and Add correct NTP server

Get-VMHost | Remove-VMHostNtpServer -NtpServer ntp.mn Confirm:$false
Get-VMHost | Add-VMHostNtpServer -NtpServer 0.asia.pool.ntp.org

# Start NTP Service and set to Automatic

Get-VMHost | Get-VMHostService | Where-Object {$_.key -eq "ntpd" } | Start-VMHostService
Get-VMHost | Get-VMHostService | Where-Object {$_.key 
Read the rest

How to get detailed info for all esxi into csv or Excel file

The advantage of below powerCLI is that oneliner shows as well SerialNumber (for example Dell/HP tags – very useful to find your esxi server in Datacenter physically )

Get-VMHost |Select-Object Name,NetworkInfo,Manufacturer,Model,ProcessorType, ConnectionState, PowerState, NumCpu, MemoryTotalGB, Version, Build, MaxEVCMode, @{N="SerialNumber";E={(Get-VMHostHardware -vmhost $_).SerialNumber}} | Export-Csv -notypeinformation -Path c:\temp\all-vmhosts.csv 

or more detailed report:

.'C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'
connect-viserver vcsa.itforce.local

function Get-VMHostWSManInstance {
        param (
        [Parameter(Mandatory=$TRUE,HelpMessage="VMHosts to probe")]
        [VMware.VimAutomation.Client20.VMHostImpl[]]
        $VMHosts,
 
        [Parameter(Mandatory=$TRUE,HelpMessage="Class Name")]
        [string]
        $class,
 
        [switch]
        $ignoreCertFailures,
 
        [System.Management.Automation.PSCredential]
        $credential=$null
        )
 
        $omcBase = "http://schema.omc-project.org/wbem/wscim/1/cim-schema/2/"
        $dmtfBase = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/"
        $vmwareBase = "http://schemas.vmware.com/wbem/wscim/1/cim-schema/2/"
 
        if ($ignoreCertFailures) {
                $option = New-WSManSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
        } else {
                $option = New-WSManSessionOption
        }
        foreach 
Read the rest

How to get detailed info about all VMs

Get-VMHost | Get-VM | Select-Object  Name, PowerState, NumCpu, MemoryMB, VMhost, @{N="Datastore";E={Get-Datastore -vm $_}}, UsedSpaceGB, ProvisionedSpaceGB, @{Name='ToolsVersion';Expression={$_.Guest.ToolsVersion}}, @{Name=’VMHostVersion’;Expression={$_.VMHost.Version}},Version, @{N="IP Address";E={@($_.guest.IPAddress[0])}}  |  Export-Csv -notypeinformation -path c:\temp\vms.csv

Read the rest

How to get CDP information for all esxi network adapters

It could be very handy to know to what switch, what switch port your esxi network adapters are connected.

There is no standard method in PowerCLI with name “Get-VMHostNetworkAdapterCDP”.
To make it available you need in Powershell ISE
import all powerCLI modules and press enter
or use below script (replace vcsa by your own server fqdn):

 

.'C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'

connect-viserver vcsa.itforce.local
function Get-VMHostNetworkAdapterCDP { <# .SYNOPSIS Function to retrieve the Network Adapter CDP info of a vSphere host. .DESCRIPTION Function to retrieve the Network Adapter CDP info of a vSphere host. .PARAMETER VMHost A vSphere ESXi Host object .INPUTS
Read the rest

How to import powerCLI modules into standard Powershell ISE

Sometimes it’s useful to have opportunity to run and debug powerCLI scripts/functions from standard Powershell ISE

  1. install on AdminPC powerCLI
  2. open Powershell ISE
  3. copy and paste into command line below:
    Import-Module VMware.DeployAutomation
    Import-Module VMware.ImageBuilder
    Import-Module VMware.VimAutomation.Cis.Core
    Import-Module VMware.VimAutomation.Common
    Import-Module VMware.VimAutomation.Core
    Import-Module VMware.VimAutomation.HA
    Import-Module VMware.VimAutomation.License
    Import-Module VMware.VimAutomation.Sdk
    Import-Module VMware.VimAutomation.Storage
    Import-Module VMware.VimAutomation.vds
    
    

if you need to have powerCLI modules in Powershell ISE always, then create under your user account powershell profile:

    1. new-item -path $profile -itemtype file -force
    2. powershell_ise $profile
    3. and add into your profile above commands, or just one line
      .’C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1′
    4. save your powershell profile.
Read the rest

How to know what VMs have snapshot.

If you use Veeam Backup and Replication or Veritas Netbackup non-authorized snapshot can prevent backup/replica tasks (long term snapshots as backups is one of the most common mistakes, as cause of poor performance and as Vmware datastore low free space reason and so on ). So it’s important to know quickly what VMs have snapshot and delete/merge them.

Set-ExecutionPolicy RemoteSigned
connect-viserver vcenter.org.local
get-vm | get-snapshot | fl Description, Created, VM

if you want to delete/merge existing snapshots for all VMs:

Get-VM | Get-Snapshot | Remove-Snapshot -confirm:$false
Read the rest