Friday, October 27, 2017

Register powershell snapin manually

I wanted to use the VMWare.vimautomation.core psssnapin in my scom discovery script.
But the snapin would not show up.
Earlier i had installed Powercli 6.0 and it does not have this snapin
So i uninstalled it and installed Powercli 5.5. But when the vcenter powershell was started it still showed 6.0 and the snapins would not show up.


 








 These are the steps I followed to manually register the snapin

As an example I will use the VMWare.VimAutomation.Core powershell snapin for .net 2.0.
Add the following registry key to your server.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\VMware.VimAutomation.Core]
"PowerShellVersion"="2.0"
"Vendor"="VMware Inc."
"Description"="This Windows PowerShell snap-in contains Windows PowerShell cmdlets for managing vSphere."
"Version"="5.5.0.0"
"ApplicationBase"="C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI"
"AssemblyName"="VMware.VimAutomation.ViCore.Cmdlets, Version=5.5.0.0, Culture=neutral, PublicKeyToken=null"
"ModuleName"="C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI\\VMware.VimAutomation.ViCore.Cmdlets.dll"
"CustomPSSnapInType"="VMware.VimAutomation.ViCore.Cmdlets.Vim4PSSnapIn"

Copy the assembly files in modulename for powercli 5.0 to location

C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI

Open powershell and run command Add-PSSnapin Vmware.vimautomation.Core




Thursday, August 3, 2017

Regular expression checker powershell

Simple powershell script to check your regular expression strings.
The reference for creating a regular expression is here

http://www.regular-expressions.info/quickstart.html


PS C:\WINDOWS\system32> $String = 'This is my string'
$expression = [regex]"my"
$expression.Match($String)

Groups   : {my}
Success  : True
Captures : {my}
Index    : 8
Length   : 2
Value    : my


PS C:\WINDOWS\system32> $String = 'this is my string'
$expression = [regex]"[A-Za-z]"
$expression.Match($String)


Groups   : {t}
Success  : True
Captures : {t}
Index    : 0
Length   : 1
Value    : t



PS C:\WINDOWS\system32> $String = 'this is my string'
$expression = [regex]"[A-Z]"
$expression.Match($String)



Groups   : {}
Success  : False
Captures : {}
Index    : 0
Length   : 0
Value    : 

Wednesday, July 12, 2017

Increase VMware Vcenter server disk space using powershell

Increasing virtual disks in VMware is pretty easy and straightforward.
Mapping them to the right disk in your computer is hard. They have not direct correlation seen in the Vmware ui.
So its pretty much guesswork after checking the available diskspace in computer and matching it with the diskspace seen in VMWare.

This script matches the disks in VMware with the one in computer.
Increases the space
Extends all the partitions in your computer which have free diskspace.You shouldn't have diskpace lying around unused anyway ;)

Things you need.
Account which has rights on the VMware server. Account with rights on your computer.
VMware assemblies or powercli installed.

Download link here
https://my.vmware.com/group/vmware/details?downloadGroup=VSP510-PCLI-510&productId=285


 If your script fails with and exception for SoapInterceptor
find the dll VMware.VimAutomation.Logging.SoapInterceptor.dll in your powercli install directory.
and add this line to your script after placing it in the right location.

[System.Reflection.Assembly]::LoadFrom("c:\temp\VMware.VimAutomation.Logging.SoapInterceptor.dll")



############# Variable declaration and initialization##################################################
$ComputerName = "Put your VMware computername here"
$username = "domain\username"
$password = ConvertTo-SecureString -AsPlainText -Force "password"
$Credentials = New-Object System.Management.Automation.PSCredential ($username, $password)
$LocalDriveObjects =  @()
$capacityIncrease = 2
$Volume = "D:"

############################################Assemblies#############################################################
add-pssnapin VMware.VimAutomation.Core
add-pssnapin VMware.VimAutomation.Vds

##########################################################################################################


Function CreateEventLog()
{
$ApplicationEventLogSources = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application"

if(!(($ApplicationEventLogSources | Select-Object "PSChildName") -match "DiskIncreaseScript"))
{
 New-EventLog -LogName Application -Source DiskIncreaseScript -ComputerName "localhost"

}

}


Function WriteEvent($message)
  {
      Write-EventLog -logname Application -computername "localhost" -Source DiskIncreaseScript -Eventid 65503 -Message $message
  }

Function Increase-Disk($HardDisk)
         {
            
            $CurrentCapacity = $HardDisk.CapacityGB
            $Capacity = $CurrentCapacity + $capacityIncrease
            WriteEvent "Current capacity on $Computername : $CurrentCapacity. This will be increased to $Capacity"
            Set-HardDisk -HardDisk $HardDisk -CapacityGB $Capacity -GuestCredential $Credentials -Confirm:$false
         }

Function Increase-volume($Computer)
         {
          
           WriteEvent "Increasing volumes on $Computer"
          
           Invoke-Command -ComputerName $Computer -scriptblock { 'list disk' | diskpart | ? {
  $_ -match 'disk (\d+)\s+online\s+\d+ .?b\s+\d+ [gm]b'
} | % {
  $disk = $matches[1]
  "select disk $disk", "list partition" | diskpart | ? {
    $_ -match 'partition (\d+)'
  } | % { $matches[1] } | % {
    "select disk $disk", "select partition $_", "extend" | diskpart | Out-Null
  }
}
}


          }


Function Get-LocalDisk($Computer)
{
$PDiskDrives = Get-WmiObject -Class Win32_DiskDrive -ComputerName $Computer

 foreach($PdiskDrive in $PDiskDrives)
  {

   $partitionquery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($PdiskDrive.DeviceID)'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
   $Partitions = Get-WmiObject -Query $partitionquery -ComputerName $Computer
   #$Partitions
    foreach($Partition in $Partitions)
     {
     $Logicaldrivesquery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} WHERE AssocClass = Win32_LogicalDiskToPartition"
     $LogicalDrives =  Get-WmiObject -Query $Logicaldrivesquery -ComputerName $Computer
 
    
      foreach($LogicalDrive in $LogicalDrives)
        {
          
       
        $obj = New-Object -Type PSCustomObject -Property @{
        Disk        = $PdiskDrive.DeviceID
        DiskSize    = $PdiskDrive.Size
        DiskModel   = $PdiskDrive.Model
        Partition   = $partition.Name
        RawSize     = $partition.Size
        DriveLetter = $LogicalDrive.DeviceID
        VolumeName  = $LogicalDrive.VolumeName
        Size        = $LogicalDrive.Size
        FreeSpace   = $LogicalDrive.FreeSpace
        SerialNumber = $PdiskDrive.SerialNumber
                                                           }

        $LocalDriveObjects = $LocalDriveObjects + $obj
        }
     }
}
        
return $LocalDriveObjects
}

##################### Main Script######################################################
Connect-VIServer "VcenterServerName" -Credential $Credentials
$LocalDisks = Get-LocalDisk $Computername
$vmHardDisks = Get-VM -Name $ComputerName | Get-HardDisk 
$vmDatacenterView = Get-VM -Name $ComputerName | Get-Datacenter | Get-View 
$virtualDiskManager = Get-View -Id VirtualDiskManager-virtualDiskManager

CreateEventLog

foreach($DObject in $LocalDisks)
 {
  if($DObject.DriveLetter -eq $Volume)
     {
      WriteEvent  "Checking driveletter $($DObject.Driveletter) on $ComputerName"
      foreach($vmHardDisk in $vmHardDisks)
              {
              $vmHardDiskUuid = $virtualDiskManager.queryvirtualdiskuuid($vmHardDisk.Filename, $vmDatacenterView.MoRef) | foreach {$_.replace(' ','').replace('-','')}
             
              if($DObject.SerialNumber -eq $vmHardDiskUuid)
                {
                 WriteEvent "Found a match $vmHardDiskUuid $($DObject.SerialNumber) $($vmHardDisk.Name) on $Computername"
                 WriteEvent "Disk Object: $DObject"
                 Increase-Disk $vmHardDisk
                 Start-Sleep -Seconds 10
                
                
                }
              }
     }
 }

Start-Sleep -Seconds 120

Increase-volume $Computername



Friday, June 30, 2017

SQL server Reporting Services configuration manager does not open

This can happen due a corrupted wmi repository. If that is the case in your installation.

Find the file sqlmgmproviderxpsp2up.mof in your SQL installation directory on your server.

 In my case it was located in

C:\Program Files (x86)\Microsoft SQL Server\110\Shared

Then open command prompt as admin and navigate to the directory.

Then execute

mofcomp.exe sqlmgmproviderxpsp2up.mof

This will repair your repository  and you should be able to open the console.



Friday, June 23, 2017

Extract contents from msp file

Use the msix utility to extract the contents of your msp file.

https://onedrive.live.com/?id=9415F61CBB1A8030%211613&cid=9415F61CBB1A8030


C:\Parag\Tools\Temp>MsiX.exe SCSM2012R2_CU_KB3129780_AMD64_7.5.3079.607.msp
Target_MSIToFixed_MSI
#Target_MSIToFixed_MSI
PCW_CAB_SMHotfix

Rename the cab file by adding the extension .cab to it.

Now you can browse the file in Windows Explorer and extract the file you need.

Find the directory for your installation and search for this file.

Microsoft.EnterpriseManagement.Packaging.dll

Copy the location of that file in my case its

C:\Program Files\Microsoft System Center 2012 R2\Operations Manager\Setup

Thursday, June 22, 2017

VMWare monitoring using SCOM

The management pack for monitoring VMware is here.

https://github.com/Mitch-Luedy/Community.VMware

It's a very good management pack.and also has clear instructions for installation.


But after installation you may find that the required objects are not being discovered.
This is largely due to the fact that the powershell scripts which are running the discoveries have this

Try {
    Add-PSSnapin VMware.VimAutomation.Core
} Catch {
    Start-Sleep -Seconds 10
    Try {
        Add-PSSnapin VMware.VimAutomation.Core
    } Catch {
        DefaultErrorLogging
        Exit
    }
}

If the mp was unsealed then maybe I could have updated the code and added import-module instead of this one. But to fix it  try this.

1. Install the VMware powercli version from this link.

https://my.vmware.com/web/vmware/details?downloadGroup=PCLI550&productId=352

VMware-PowerCLI-5.1.0-793510.exe

After installation run the commands in powershell and if you do not get any errors then you are good to go.

Wednesday, June 7, 2017

How to make a parameter mandatory in powershell.

How to make a parameter mandatory in powershell.

Enter this at the start of your script.

param(
            [Parameter(Mandatory=$true)]
            [string]$Server
           )

A very good explanation and additional ways to do it is here.

https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/22/use-powershell-to-make-mandatory-parameters/

powershell powergui download link

Powergui is a very good powershell editor. I particularly like the speed of execution compared to windows powershell ise.



Please refer to this link for powergui and additinal downloads.

https://dmitrysotnikov.wordpress.com/2015/01/30/download-links-for-powergui-and-qad-cmdlets/

In case the page does not open the direct download link is posted below.

http://community-downloads.quest.com/powergui/Release/3.8/PowerGUI.3.8.0.129.msi