Thursday, January 26, 2012

List all Zones on all DNS Servers

cls
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
$Date = Get-Date
$Filename = "C:\" + "DNSZones" + "_" + $Date.Day + "-" + $Date.Month + "-" + $Date.Year + ".txt"
$computer = 'DNSServer1','DNSServer2','DNSServer3'
foreach ($comp in $computer)
{
$zones = Get-WmiObject -Class MicrosoftDNS_Zone -ComputerName $comp -Namespace root\microsoftDNS
foreach ($zone in $zones)
{
if ($zone.ZoneType -eq '2'-or $zone.ZoneType -eq '1')
{
$tab = '     '
$output = $comp + $tab + $zone.Name
$output | out-file $Filename -Append
}
}
}

Use Powershell to change DNS entries on multiple machines

cls # Clear Screen
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue # remove any lingering variables
$content = get-content c:\scripts\powershell\DNS\fixdnsstpete.txt
foreach ($comp in $content)
{
if (Test-Connection -ComputerName $comp -count 1 -Quiet)
  {
  $comp
  $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -comp $comp | where{$_.IPEnabled -eq “TRUE”}
  Foreach($NIC in $NICs)      {
      $DNSServers = ('170.12.8.249','170.12.10.173','170.12.144.17','170.12.144.227')
      $NIC.SetDNSServerSearchOrder($DNSServers)
      }
  }
}

Add a vLAN to a vmWare host

cls # Clear Screen
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue # remove any lingering variables
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Connect-VIServer -Server visrv1
foreach ($ESXhost in get-cluster CLUSTERNAME | get-vmhost)
{
  get-virtualswitch -vmhost $ESXhost -name VSWITCH1 | new-virtualportgroup -name vLAN_201 -vlanid 201
}

Get Misnamed Folders in vmWare

cls
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
$VISRVS = 'visrv1','visrv2','visrv3'
foreach ($VISRV in $VISRVS)
{
$VIServer = Connect-VIServer $VISRV -ErrorAction SilentlyContinue
foreach ($VM in (Get-View -ViewType VirtualMachine ))
 {
  $Details = "" |Select-Object VM,Path
  $Folder = ((($VM.Summary.Config.VmPathName).Split(']')[1]).Split('/'))[0].TrimStart(' ')
  $Path = ($VM.Summary.Config.VmPathName).Split('/')[0]
  if ($VM.Name -ne $Folder)
  {
   $Details.VM = $VM.Name
   $Details.Path = $Path
   $output = $Details.VM + ';' + $Details.Path
   $output
   $output | Out-File c:\misnamed.csv -Append
  }
 }
Disconnect-VIServer $VISRV -Confirm:$false
}

Power off all servers in a resource pool

cls
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue # remove any lingering variables
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

Connect-VIServer visrv1
#Get All VMs in the specific cluster Resource Pool
$TESTVMS = Get-ResourcePool "RESOURCEPOOLNAME" | Get-VM
#For each of the VMs in the Resource Pool 
Foreach ($VM in ($TESTVMS))

 # Power Down the Test VM Guests 
 $VM | Shutdown-VMGuest -Confirm:$false
 } 
# St the amount of time to wait before assuming the guests are stuck  
$waittime = 200 #Seconds 
$Time = (Get-Date).TimeofDay 
do

#Wait for the VMs to be shutdown cleanly 
sleep 1.0 
$timeleft = $waittime - ($Newtime.seconds) 
$numvms = (Get-ResourcePool "SS_topaz" | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count 
Write "Waiting for startup of $numvms VMs or $timeleft seconds"
$Newtime = (Get-Date).TimeofDay - $Time
}
until ((@(Get-ResourcePool "RESOURCEPOOLNAME" | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count) -eq 0 -or ($Newtime).Seconds -ge $waittime) 
Write-Host "RESOURCEPOOLNAME Test VM Power On Complete"
Disconnect-VIServer visrv1 -Confirm:$false

Get vm tools version

cls
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue # remove any lingering variables
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

$vcs = @()
$vcs += connect-viserver visrv1 -User USERNAME -Password PASSWORD
$vcs += connect-viserver visrv2 -User USERNAME -Password PASSWORD
$vcs += connect-viserver visrv3 -User USERNAME -Password PASSWORD

get-vm  | % { get-view $_.ID } | select Name, @{ Name="ToolsVersion"; Expression={$_.config.tools.toolsVersion}}
Disconnect-VIServer $vcs

Is a VM thin Provisioned

cls
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue # remove any lingering variables
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue$VISRVS = 'visrv1','visrv2','visrv3'
foreach ($VISRV in $VISRVS)
{
$VIServer = Connect-VIServer $VISRV -User USERNAME -Password PASSWORD
get-view -ViewType VirtualMachine  | % {
#Write-Host $_.Name
    foreach($dev in $_.config.hardware.Device){
        if($dev.GetType().Name -eq "VirtualDisk"){
            if($dev.Backing.ThinProvisioned){
   Write-Host $VISRV
   Write-Host $_.Name
   Write-Host $dev.DeviceInfo.Label = "Is Thin Provisioned" -ForegroundColor Red }
            }
     }
 } Disconnect-VIServer
}