Monday, October 30, 2017

Basic Capacity Information (PowerCLI)

The PowerCLI cmdlets can be used to give a quick overview of the number of hosts and VMs that exist within the Virtual Center; it could be useful to record these figures on a monthly basis so that the growth of the environment can be tracked over time. How about finding the average number of VMs per host in a cluster? Again, a useful figure to regularly record so as to observe what consolidation ratios are being achieved and to plan for future capacity needs, and a figure which PowerCLI can help you find.
In the code below, we retrieve the ESX / ESXi hosts & VMs and observe their count; note the use of the $DefaultVIServer variable, which is updated each time a connection is made to a vCenter.
For the average number of VMs in each cluster, we find the total number of ESX / ESXi hosts and VMs and perform some basic maths to arrive at a figure. Note the use of some basic .NET [math]::round code and the number of decimal places (1) required to format the output as desired:

# Total number of hosts
$TotalVMHosts = (Get-VMHost).Count
Write-Host "There are $TotalVMHosts Hosts in $DefaultVIServer"
 
# Total number of guests
$TotalVMs = (Get-VM).Count
Write-Host "There are $TotalVMs Virtual Machines in $DefaultVIServer"
 
# Average VMs Per Cluster
$Clusters = Get-Cluster
foreach ($Cluster in $Clusters){
 
    $VMHosts = Get-Cluster $Cluster | Get-VMHost
    $VMs = Get-Cluster $Cluster | Get-VM
 
    $AverageVMsPerCluster = [math]::round(($VMs.count / $VMHosts.count), 1)
    Write-Host "$Cluster has $AverageVMsPerCluster guests per VMware host"
 

Saturday, October 28, 2017

PowerShell Arrays

TOPIC
    about_Arrays

SHORT DESCRIPTION
    Describes arrays, which are data structures designed to store
    collections of items.

LONG DESCRIPTION
    An array is a data structure that is designed to store a collection
    of items. The items can be the same type or different types.

    Beginning in Windows PowerShell 3.0, a collection of zero or one
    object has some properties of arrays.


 CREATING AND INITIALIZING AN ARRAY
    To create and initialize an array, assign multiple values to a variable.
    The values stored in the array are delimited with a comma and separated
    from the variable name by the assignment operator (=).

    For example, to create an array named $A that contains the seven
    numeric (int) values of 22, 5, 10, 8, 12, 9, and 80, type:

        $A = 22,5,10,8,12,9,80

    You can also create and initialize an array by using the range
    operator (..). For example, to create and initialize an array named
    "$B" that contains the values 5 through 8, type:

        $B = 5..8

    As a result, $B contains four values: 5, 6, 7, and 8.

    When no data type is specified, Windows PowerShell creates each array as
    an object array (type: System.Object[]). To determine the data type of an array,
    use the GetType() method. For example, to determine the data type of the
    $a array, type:

        $a.GetType()

    To create a strongly typed array, that is, an array that can contain only
    values of a particular type, cast the variable as an array type, such
    as string[], long[], or int32[]. To cast an array, precede the variable
    name with an array type enclosed in brackets. For example, to create a
    32-bit integer array named $ia containing four integers (1500, 2230, 3350,
    and 4000), type:

        [int32[]]$ia = 1500,2230,3350,4000

    As a result, the $ia array can contain only integers.

    You can create arrays that are cast to any supported type in the
    Microsoft .NET Framework. For example, the objects that Get-Process
    retrieves to represent processes are of the System.Diagnostics.Process
    type. To create a strongly typed array of process objects, enter the
    following command:

        [Diagnostics.Process[]]$zz = Get-Process

 THE ARRAY SUB-EXPRESSION OPERATOR
    The array sub-expression operator creates an array, even if it
    contains zero or one object.

    The syntax of the array operator is as follows:
        @( ... )

    You can use the array operator to create an array of zero or
    one object.

        PS C:\>$a = @("One")
        PS C:\>$a.Count
        1

        PS C:\>$b = @()
        PS C:\>$b.Count
        0

    The array operator is particularly useful in scripts when
    you are getting objects, but do not know how many objects
    you will get.

        PS C:\> $p = @(Get-Process Notepad)


    For more information about the array sub-expression operator,
    see about_Operators.


  READING AN ARRAY
    You can refer to an array by using its variable name. To display all
    the elements in the array, type the array name. For example:

$a

    You can refer to the elements in an array by using an index, beginning
    at position 0. Enclose the index number in brackets. For example,
    to display the first element in the $a array, type:

        $a[0]

    To display the third element in the $a array, type:

        $a[2]

    Negative numbers count from the end of the array. For example, "-1"
    refers to the last element of the array. To display the last three elements
    of the array, type:

        $a[-3..-1]

    However, be cautious when using this notation.

        $a[0..-2]

    This command does not refer to all the elements of the array, except for
    the last one. It refers to the first, last, and second-to-last elements
    in the array.
 
    You can use the range operator to display a subset of all the values in an
    array. For example, to display the data elements at index position 1
    through 3, type:

        $a[1..3]

    You can use the plus operator (+) to combine a range with a list of
    elements in an array. For example, to display the elements at index
    positions 0, 2, and 4 through 6, type:

        $a[0,2+4..6]

    To determine how many items are in an array, use the Length property
    or its Count alias.

        $a.Count


    You can also use looping constructs, such as ForEach, For, and While loops,
    to refer to the elements in an array. For example, to use a ForEach loop
    to display the elements in the $a array, type:

        foreach ($element in $a) {$element}

    The Foreach loop iterates through the array and returns each value in
    the array until reaching the end of the array.

    The For loop is useful when you are incrementing counters while examining
    the elements in an array. For example, to use a For loop to  return every
    other value in an array, type:

        for ($i = 0; $i -le ($a.length - 1); $i += 2) {$a[$i]}

    You can use a While loop to display the elements in an array until a
    defined condition is no longer true. For example, to display the elements
    in the $a array while the array index is less than 4, type:

        $i=0
        while($i -lt 4) {$a[$i]; $i++}



 GET THE MEMBERS OF AN ARRAY

    To get the properties and methods of an array, such as the Length
    property and the SetValue method, use the InputObject parameter of the
    Get-Member cmdlet.

    When you pipe an array to Get-Member, Windows PowerShell sends the items
    one at a time and Get-Member returns the type of each item
    in the array (ignoring duplicates).

    When you use the InputObject parameter, Get-Member returns the
    members of the array.

    For example, the following command gets the members of the array in the
    $a variable.

        Get-Member -InputObject $a

    You can also get the members of an array by typing a comma (,) before
    the value that is piped to the Get-Member cmdlet. The comma makes the
    array the second item in an array of arrays. Windows PowerShell pipes
    the arrays one at a time and Get-Member returns the members of the array.

        ,$a | Get-Member

        ,(1,2,3) | Get-Member


 MANIPULATING AN ARRAY
    You can change the elements in an array, add an element to an array, and
    combine the values from two arrays into a third array.

    To change the value of a particular element in an array, specify the
    array name and the index of the element that you want to change, and then
    use the assignment operator (=) to specify a new value for the element. For
    example, to change the value of the second item in the $a array (index
    position 1) to 10, type:

        $a[1] = 10

    You can also use the SetValue method of an array to change a value. The
    following example changes the second value (index position 1) of the $a
    array to 500:

        $a.SetValue(500,1)

    You can use the += operator to add an element to an array. When you use
    it, Windows PowerShell actually creates a new array with the values of the
    original array and the added value. For example, to add an element with a
    value of 200 to the array in the $a variable, type:

        $a += 200

    It is not easy to delete elements from an array, but you can create a new
    array that contains only selected elements of an existing array. For
    example, to create the $t array with all the elements in the $a array
    except for the value at index position 2, type:

        $t = $a[0,1 + 3..($a.length - 1)]

    To combine two arrays into a single array, use the plus operator (+). The
    following example creates two arrays, combines them, and then displays
    the resulting combined array.

        $x = 1,3
        $y = 5,9
        $z = $x + $y

    As a result, the $z array contains 1, 3, 5, and 9.


    To delete an array, assign a value of $null to the array. The following
    command deletes the array in the $a variable.

        $a = $null

    You can also use the Remove-Item cmdlet, but assigning a value of $null
    is faster, especially for large arrays.



 ARRAYS OF ZERO OR ONE
    Beginning in Windows PowerShell 3.0, a collection of zero or one object
    has the Count and Length property. Also, you can index into an array of
    one object. This feature helps you to avoid scripting errors that occur
    when a command that expects a collection gets fewer than two items.

    The following examples demonstrate this feature.   
   
    #Zero objects
        $a = $null
        $a.Count
        0
        $a.Length
        0

    #One object
        $a = 4
        $a.Count
        1
        $a.Length
        1
        $a[0]
        4
        $a[-1]
        4


SEE ALSO
    about_Assignment_Operators
    about_Hash_Tables
    about_Operators
    about_For
    about_Foreach
    about_While












Monday, October 16, 2017

VMware PowerCLI CLI install

PS> Save-Module -Name VMware.PowerCLI -Path <path>


PS> Install-Module -Name VMware.PowerCLI



LINK


*******************
Install-Module Vmware.Powercli -Force -confirm:false


Thursday, October 12, 2017

Set MEM Reservations

Add-PsSnapin VMware.VimAutomation.Core


$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.memoryReservationLockedToMax = $true

foreach ($vm in (Get-Cluster "VDI-Non-P-Desktops" | Get-VM))
{
  $VM.ExtensionData.ReconfigVM_Task($spec)
}

Wednesday, October 11, 2017

PowerCLI @ Set time

#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))
}

Tuesday, October 10, 2017

Creating a Microsoft Certificate Authority Template for SSL certificate creation in vSphere

Creating a Microsoft Certificate Authority Template for SSL certificate creation in vSphere 5.x

(2062108)

Configuration SSL Cert


Purpose
This article provides information on manually configuring a new Certificate Authority (CA) template based on the Web Server template located in the Certificate Authority Root or Subordinate server for use with SSL certificate implementation in VMware vSphere 5.x.

By default, the Certificate Authority role in Windows Server 2008 and later do not include Data Encipherment, Nonrepudiation, or Client Authentication on the Web Server template. vSphere 5.0 requires the use of Nonrepudiation and Client Authentication on the generated CA certificates; vSphere 5.1 and 5.5 require Data Encipherment on the generated CA certificates.
Resolution
Creating a new default template
To create a new default template:
Connect to the Root CA server or Subordinate CA server through RDP.

Note: Connect to the CA server in which you are intending to perform your certificate generation.

Click Start > Run, type certtmpl.msc, and click OK. The Certificate Template Console opens.
In the middle pane, under Template Display Name, locate Web Server.
Right-click Web Server and click Duplicate Template.
In the Duplicate Template window, select Windows Server 2003 Enterprise for backward compatibility.

Note: If you have an encryption level higher than SHA1, select Windows Server 2008 Enterprise.

Click the General tab.
In the Template display name field, enter VMware Certificate as the name of the new template.
Click the Extensions tab.
Select Key Usage and click Edit.
Select the Signature is proof of origin (nonrepudiation) option.
Select the Allow encryption of user data option.
Click OK.
Select Application Policies and click Edit.
Click Add.
Select Client Authentication.

Note: You may need to provide a name to proceed to step 16.

Click OK.
Click OK again.
Click the Subject Name tab.
Ensure that the Supply in the request option is selected.
Click OK to save the template.
Adding a new template to certificate templates
To add a new template to certificate templates:
Connect to the Root CA server or Subordinate CA server through RDP.

Note: Connect to the CA server in which you are intending to perform your certificate generation.

Click Start > Run, type certsrv.msc, and click OK. The Certificate Server console opens.
In the left pane, if collapsed, expand the node by clicking the [+] icon.
Right-click Certificate Templates and click New > Certificate Template to Issue.
Locate VMware Certificate under the Name column.
Click OK.

A new template option is now created in your Active Directory Certificate Services node. This new template can be used in the place of Web Server for the vSphere 5.x CA certificate

InPrivate Filtering feature on at all times by default

tweak the registry to keep the InPrivate Filtering feature on at all times by default.

Open Registry Editor, navigate to HKCU\Software\Microsoft\Internet Explorer\Safety\PrivacIE,

add the DWORD value StartMode (if it doesn’t already exist), and set its value to 1.

Internet Explorer will now retain and apply whatever decisions you made in the InPrivate Filtering Settings dialog box.

Wednesday, October 4, 2017

Unmap on Physical LUN




TABLE OF CONTENTS
Overview
In order to perform space reclamation on the Clustered Shared Volume (CSV) a drive letter to mount point mapping is required. Sdelete can be run on CSVs using the command subst to assign a virtual drive to the CSV mount point and then run sdelete on the virtual drive to perform space reclamation. This article details all of the steps required to perform this task.
Steps
Step 1 -- Find out which volume is the CSV
Open up a Windows PowerShell session
Run "List Volume" | diskpart
Note down the CSVFS Volume path, in the example below, the path is: "C:\ClusterStorage\Volume1\"
Example
01
PS C:\> "List Volume" | diskpart
02
Microsoft DiskPart version 6.3.9600
03
Copyright (C) 1999-2013 Microsoft Corporation.
04
On computer: HYPERV-NODE1
05
DISKPART>
06
 Volume     ### Ltr Label       Fs    Type       Size    Status    Info
07
 ---------- --- --- ----------- ----- ---------- ------- --------- --------
08
 Volume      0   E                    CD-ROM         0 B No Media
09
 Volume      1      System Rese NTFS  Partition   350 MB Healthy   System
10
 Volume      2   C              NTFS  Partition   238 GB Healthy   Boot
11
 Volume      3      SQL2014 Sys NTFS  Partition   199 GB Healthy
12
   C:\Pure Storage\SQL2014-Sys\
13
 Volume      4      SQL2014 Tem NTFS  Partition  2047 GB Healthy
14
   C:\Pure Storage\SQL2014-Tempdb\
15
 Volume      5   F  CommVault L NTFS  Partition  1023 GB Healthy
16
 Volume      6      CSV         CSVFS Partition    15 TB Healthy
17
   C:\ClusterStorage\Volume1\
18
 Volume      7   D                    Removable      0 B No Media
19
 Volume      8      SQL2014 Dat NTFS  Partition  5119 GB Healthy
20
   C:\Pure Storage\SQL2014-Data\
21
22
DISKPART>
23
PS C:\>
If you have multiple Clustered Shared Volumes across a large number of Windows Server Failover Clusters the below PowerShell can be used to determine the CSVs on each of the individual clusters from one centralized PowerShell script.
01
Import-Module FailoverClusters
02
$Volumes = @()
03
$ClusterName = "<CLUSTER_NAME>"
04
05
$CSV = Get-ClusterSharedVolume -Cluster $ClusterName
06
ForEach ($CSV in $CSVS) {
07
  $CSVInfo = $CSV | Select -Property Name -ExpandProperty SharedVolumeInfo
08
  ForEach ( $CSVInfo in $CSVInfos ) {
09
    $Volumes = New-Object PSObject -Property @{
10
       Path = $CSVInfo.FriendlyVolumeName
11
    }
12
    $Volumes.Path
13
  }
14
}
Step 2 -- Find out which drive letters are currently in use on the system.
List drive letters currently in use: Get-PSDrive -PSProvider FileSystem
1
PS B:\> Get-PSDrive -PSProvider FileSystem
2
Name        Used (GB)       Free (GB)    Provider   Root
3
----        ---------       ---------    --------   ----
4
C               39.81          198.32    FileSystem C:\
5
D                                        FileSystem D:\
6
E                                        FileSystem E:\
7
F               27.78          996.09    FileSystem F:\
8
P                                        FileSystem P:\
9
X                                        FileSystem X:\
Use the subst command and assign a virtual drive letter to the CSV volume. In my example below, I will use drive B: and map the CSV volume to "C:\ClusterStorage\Volume1\"
view sourceprint
01
S G:\> subst B: C:\ClusterStorage\Volume1
02
PS G:\> B:
03
PS B:\> ls
04
Directory: B:\
05
Mode      LastWriteTime           Length Name
06
----      -------------       ---------- ----
07
d----   7/12/2015 6:15 PM                PowerShellSDK_1-0-15-0
08
d----   7/12/2015 6:15 PM                PowerShellToolkit-2-8-0-430
09
d----   7/12/2015 6:16 PM                PowerShellToolkit-3-0-0-0
10
d----  7/20/2015 11:46 PM                PythonToolkit-1-4-0
11
d----   9/28/2015 6:35 PM                SCOM
12
d---- 12/30/2015 11:14 PM                vdbench-1
13
d----   7/12/2015 6:04 PM                vdbench-2
14
d----   7/12/2015 6:04 PM                vdbench-3
15
-a---  12/18/2015 1:29 AM     2718328422 PowerShellSDK_1-0-15-0.VHDX
16
                                       4
17
-a---  12/18/2015 1:30 AM     1084227584 PowerShellToolkit-2-8-0-430.VHDX
18
                                       0
19
-a---    9/9/2015 6:36 PM     9802088448 PowerShellToolkit-3-0-0-0.VHDX
20
-a---  11/23/2015 3:58 AM     1114426572 PythonToolkit-1-4-0.VHDX
21
                                       8
22
-a---    1/9/2013 2:26 PM         155736 sdelete.exe
23
-a---  12/18/2015 2:48 AM     1007052390 vdbench-1.VHDX
24
                                       4
25
-a---  12/18/2015 2:48 AM     1037251379 vdbench-2.VHDX
26
                                       2
27
-a---  12/18/2015 2:48 AM     1090938470 vdbench-3.VHDX
28
                                       4
 Run sdelete to free space on the CSV.
You must have the sdelete.exe file in command path or the file sdelete.exe must exist on the current path. If you do not have the sdelete program, you will have to go download from Microsoft TechNet.
Go to the virtual drive, in my example above, I have drive B: mapped to the CSV, run: B:
Run .\sdelete -z -s 
Note Running the above parameters will zero free space and apply to sub-directories.
Note If there are CSV deleted files or folders in the Trash Bin that is not emptied, you will need to empty the trash bin before running sdelete to reclaim the space.
1
PS B:\> .\sdelete -z -s
2
SDelete - Secure Delete v1.61
3
Copyright (C) 1999-2012 Mark Russinovich
4
Sysinternals - www.sysinternals.com
5
SDelete is set for 1 pass.
6
Zeroing free space on B:\: 0%

References
Reference syntax for “subst”
01
PS C:\Users\Administrator.CSGLAB> subst /?
02
Associates a path with a drive letter.
03
04
 SUBST [drive1: [drive2:]path]
05
 SUBST drive1: /D
06
    drive1:        Specifies a virtual drive to which you want to assign a path.
07
   [drive2:]path  Specifies a physical drive and path you want to assign to
08
                  a virtual drive.
09
   /D             Deletes a substituted (virtual) drive.
10
 
11
  Type SUBST with no parameters to display a list of current virtual drives.

Reference syntax for “sdelete”
01
PS B:\> .\sdelete.exe /h
02
 SDelete - Secure Delete v1.61
03
Copyright (C) 1999-2012 Mark Russinovich
04
Sysinternals - www.sysinternals.com
05
06
 usage: B:\sdelete.exe [-p passes] [-s] [-q] <file or directory> ...
07
       B:\sdelete.exe [-p passes] [-z|-c] [drive letter] ...
08
   -a         Remove Read-Only attribute
09
   -c         Clean free space
10
   -p passes  Specifies number of overwrite passes (default is 1)
11
   -q         Don't print errors (Quiet)
12
   -s or -r   Recurse subdirectories
13
   -z         Zero free space (good for virtual disk optimization)
Back to top

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...