RDM – find assisgned and make sure assigned as Perennially Reserved

<#
.SYNOPSIS
Alan Harrington, copy and excute sections at a time to make sure runs okay
.DESCRIPTION
collects all rdms that are ASSIGNED to a vm and makes them as Perenially Reserved, if the rdm is not configured on the vm then it won’t set it.
.PARAMETER <paramName>
<Description of script parameter>
.EXAMPLE
<An example of using the script>
#>
$cluster = “vnesxcomp099_rdms”

#Get-cluster $cluster | get-vm | sort name | Get-HardDisk -DiskType “RawPhysical”,”RawVirtual” | Select Parent,ScsiCanonicalName
$rdmsattachedinclu = Get-cluster $cluster | get-vm | sort name | Get-HardDisk -DiskType “RawPhysical”,”RawVirtual” | Select Parent,ScsiCanonicalName
$rdmsscsinaa = $rdmsattachedinclu | select ScsiCanonicalName

#full list, sorted, unique
[array]$temp = $null
foreach ($rdmsscsi in $rdmsscsinaa){
$temp2 = $rdmsscsi.ScsiCanonicalName
$temp = $temp + $temp2}
foreach ($i in $temp) {
[string]$i = $i
$temp[$temp.IndexOf($i)] = $i.SubString(4)}
$rdmlist = $temp | sort | select -Unique
$rdmlist = $rdmlist | ForEach-Object {“naa.$_”}
#now that list is converted back to naa. time to set it

$vmhs = get-cluster $cluster | get-vmhost | sort name
$vmhsesxcli = $vmhs | get-esxcli
foreach($esxcli in $vmhsesxcli) {
# And for each RDM Disk
foreach($RDM in $rdmlist) {
# Set the configuration to “PereniallyReserved”.
# setconfig method: void setconfig(boolean detached, string device, boolean perenniallyreserved)
$esxcli.storage.core.device.setconfig($false,$RDM,$true)
$esxcli.storage.core.device.list($rdm)
}
}

checking a cluster to see about MM (scsi bus sharing)

$clname = “HOSA-P-FARM06-ProSuites”
$clustertocheck = Get-Cluster $clname
Write-Host “Checking $clname” -ForegroundColor cyan
$vmhs = $clustertocheck | Get-VMHost | sort name
foreach ($vmh in $vmhs){
$scsishared = $null
$vms = $vmh | Get-VM | sort name
#$hostver = $vmh | get-View -Property Name,Config.Product | Format-Table Name, @{L=’Version’;E={$_.Config.Product.FullName}}
$scsishared = $vms |Get-ScsiController | Where-Object {$_.BusSharingMode -eq ‘Physical’ -or $_.BusSharingMode -eq ‘Virtual’}
if (!$scsishared){
if ($vmh.ConnectionState -eq “Maintenance”){write-host “$vmh is in MM already, host is $vmh.Version”}
if ($vmh.ConnectionState -ne “Maintenance”){Write-host “$vmh is okay, no shared SCSIBUS vms” -foregroundcolor yellow}
}
if ($scsishared) { Write-Host “$vmh is NOT okay, SCSIBUS SHARED vms” -foregroundcolor red }
}

Migrating VDS port group vms to vss port groups

$face = Get-VirtualPortGroup  | sort name

$face | where {$_.vlanid -eq “344”}

 

 

$vdpg = “dvsportgroup”

Get-VDPortgroup  $vdpg | get-vm

$vmnics = Get-VDPortgroup  $vdpg | get-vm | Get-NetworkAdapter | where {$_.NetworkName -eq $vdpg}

$vmnics | Set-NetworkAdapter -NetworkName “vssporrtgroup” -WhatIf

openssl.cfg for vcenter 5.0 (not 5.1)

for god sakes please use ver .9.8, if you use 1.0, the pfx wont work correctly and web services will be broke and you will be pulling your hair out.

Alan

add sans to openssl.cfg
# Extensions to add to a certificate request

basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName= @alt_names

[req]
req_extensions = v3_req
And in the v3_req section:

[ v3_req ]
subjectAltName = @alt_names

[alt_names]
DNS.1 = in2vc01
DNS.2 = in2vc01.corp.allscripts.com
IP.1 = 10.143.100.25

openssl.exe genrsa 1024 > rui.key

mine had to be 2048

openssl.exe genrsa 2048 > rui.key

openssl req -config openssl.cfg -new -key rui.key > rui.csr
openssl req -text -noout -in rui.csr
to verify

submit as 64 to cert, approve

openssl pkcs12 -export -in rui.crt -inkey rui.key -name in2vc01.corp.allscripts.com -passout pass:testpassword -out rui.pfx

[powershell]
# Get the hostsystem object for every host currently disconnected.
$VMhosts = Get-View -ViewType ‘Hostsystem’ `
-Property ‘name’ `
-Filter @{"Runtime.ConnectionState"="disconnected"}

Foreach ($VMhost in $VMhosts)
{
# Create a reconnect spec
$HostConnectSpec = New-Object VMware.Vim.HostConnectSpec
$HostConnectSpec.hostName = $VMhost.name
$HostConnectSpec.userName = ‘root’
$HostConnectSpec.password = ‘password’

# Reconnect the host
$taskMoRef = $VMhost.ReconnectHost_Task($HostConnectSpec,$null)

# optional, but i like to return a task object, that way I can
# easily integrate this into a pipeline later if need be.
Get-VIObjectByVIView -MORef $taskMoRef
}
[/powershell]

find vms with resource settings and set ram

[powershell]
$vms = Get-VM
foreach ($vm in $vms){
$vmre = $vm | Get-VMResourceConfiguration
if ($vmre.MemLimitMB -ne ‘-1’){
$rp = $vm | Get-VMResourceConfiguration
$rpvmmb = $rp.MemLimitMB
write-host $vm ", mem granted= " $vm.memorymb ", mem resources = " $rpvmmb
}}
[/powershell]

cooler one to only show when they don’t match!

[powershell]
$vms = get-vm
foreach ($vm in $vms){
$vmre = $vm | Get-VMResourceConfiguration
if ($vmre.MemLimitMB -ne ‘-1’){
$rp = $vm | Get-VMResourceConfiguration
$rpvmmb = $rp.MemLimitMB
if ($vm.memorymb -ne $rpvmmb){
write-host $vm ", mem granted= " $vm.memorymb ", mem resources = " $rpvmmb
}}}
[/powershell]

VMlogix is a piece of shit.

vmlogix is a piece of shit, it crashes, explodes, and just does a bad job at life.

here is a script to detect with it explodes (and it will) by using the cpu values, if the rp is less then 9Ghz, it reboots it.

i have this a task to check every 30 mins for now, will probably change it to 15 when i feel like it

[powershell]#script to reboot labman if cpu useage is lower then expected.
#maybe.
connect-viserver atl-mis-vc01.eclipsys.lan
$uptimeweee = (get-stat -entity (get-vm atl-vmlm01) -stat sys.uptime.latest -maxsample 1 -realtime).Value
if ($uptimeweee -ge 1800){
$cpuuse = (Get-Stat -entity (Get-ResourcePool "LabManager-RP") -Stat cpu.usagemhz.average -MaxSamples 75 -realtime | Measure-Object Value -Average)
$cpuuse = $cpuuse.Average
Write-Host $cpuuse
if ($cpuuse -le 9500){
get-vapp LabManager | stop-vapp -Confirm:$false -ErrorAction:SilentlyContinue
sleep 30
get-vapp LabManager | start-vapp -Confirm:$false -ErrorAction:SilentlyContinue
}}
else {Write-Host "Not up long enough!"}
Disconnect-VIServer -Confirm:$false[/powershell]

Let’s build some vm’s

[powershell]

Connect-VIServer vcenter
$sr = Get-VM zTemplate2008R2SP1
$datastore = Get-Datastore ssd-nodeup
$num = 10
$vhost = get-vmhost esx-03*
while ($num -le 20){
$num2 = ($num-12)
Stop-VM 2008r2sp1-$num2 -Confirm:$false
$vm2 = New-VM -Name "2008r2sp1-$num" -VM $sr -Datastore $datastore -VMHost $vhost
Start-VM 2008r2sp1-$num
$num = ($num+1)
}
[/powershell]

Stage 4, no power :(

[powershell]
add-pssnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Connect-VIServer vcenter.alanrocks.com
#stage 4, we are fucked, and probably wont get power back. kill them all.
$hoeson = (get-vmhost | where {$_.Powerstate -eq "PoweredOn"})
$hoeson | Stop-VMHost -Confirm:$false -Force:$true
[/powershell]

Stage 3, Getting low on power….

Hell, where even going to stop a host now…
[powershell]
add-pssnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Connect-VIServer vcenter.alanrocks.com
#stage 3, now we are running leaning, and also fuck a host for good measure
$vmsilike = "dc1","vcenter","exchange2010","sql"
$vmson = (get-vm | where {$_.powerstate -eq "PoweredOn"})
$dievms = ($vmson -like $vmsilike)
foreach ($vmso in $vmson){if ($vmsilike -notcontains $vmso.Name){
# debug Write-Host $vmso.name "shut this bitch down"}}
$fvm = Get-VM $vmso
#findtoolsver
$toolver = (get-vm $fvm |% {get-View $_.id} | select @{Name="toolsver"; expression={$_.config.tools.toolsversion}})
if ($toolver.toolsver -gt 10000){$fvm | Stop-VM -Confirm:$false}
if ($toolver.toolsver -le 10000){$fvm | Shutdown-VMGuest -Confirm:$false}
}}
$ho = get-vmhost esx-02*
$clu = get-cluster -vmhost $ho
$task = $ho | Set-VMHost -Evacuate:$true -State "Maintenance" -RunAsync
Get-DrsRecommendation -Cluster $clu | where {$_.Reason -eq "Host is entering maintenance mode"} | Apply-DrsRecommendation
$host = Wait-Task $updateHostTask
$ho | Stop-VMHost -Confirm:$false -Force:$true
[/powershell]

Stage 2 Shutdown vm’s we sorta care about

[powershell]
add-pssnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Connect-VIServer vcenter.alanrocks.com
#stage 2, shutdown or stop vm’s not on this list
$vmsilike = "dc1","vcenter","dc2","files","exchange2010","sql","wordpress-win"
$vmson = (get-vm | where {$_.powerstate -eq "PoweredOn"})
$dievms = ($vmson -like $vmsilike)
foreach ($vmso in $vmson){if ($vmsilike -notcontains $vmso.Name){
# debug Write-Host $vmso.name "shut this bitch down"}}
$fvm = Get-VM $vmso
#findtoolsver
$toolver = (get-vm $fvm |% {get-View $_.id} | select @{Name="toolsver"; expression={$_.config.tools.toolsversion}})
if ($toolver.toolsver -gt 10000){$fvm | Stop-VM -Confirm:$false}
if ($toolver.toolsver -le 10000){$fvm | Shutdown-VMGuest -Confirm:$false}
}}
[/powershell]