Posted in Virtual Machine Manager, Windows Powershell | No Comment | 2,603 views | 19/02/2014 17:05
You can grant user roles into Virtual Machines for AppController with following script:
1
2
3
4
5
6
7
8
9
| $VMs = Get-VM
foreach ($VM in $VMs)
{
$UserRole = $VM.UserRole.Name
$UserRole = Get-SCUserRole -Name "$UserRole"
$UserRoleID = $UserRole.ID.Guid
$UserName = $VM.Owner
Grant-SCResource -Resource $VM -UserName $UserName -UserRoleID @("$UserRoleID")
} |
$VMs = Get-VM
foreach ($VM in $VMs)
{
$UserRole = $VM.UserRole.Name
$UserRole = Get-SCUserRole -Name "$UserRole"
$UserRoleID = $UserRole.ID.Guid
$UserName = $VM.Owner
Grant-SCResource -Resource $VM -UserName $UserName -UserRoleID @("$UserRoleID")
}
That will apply VM’s owner and UserRole as a granted user role.
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 5,886 views | 17/02/2014 14:48
You may see duplicate VMs in SCVMM 2012 R2 after a host crash in a cluster.
In that use you can use following script:
1
| Get-VM "DuplicateVM" | Where Cloud -eq $Null | Remove-VM -force |
Get-VM "DuplicateVM" | Where Cloud -eq $Null | Remove-VM -force
That will remove that VM from SCVMM database only. VM will be online on host after operation.
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 4,439 views | 15/02/2014 12:41
You can use this script to move your VMs into another Cloud (Cloud Migration) on SCVMM 2012 R2.
This script only works on SCVMM 2012 R2 due to vHBA control.
You can remove vHBA control to make it work on SCVMM 2012 SP1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
| # Parameters
$DestinationCloud = "New Cloud"
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
# Add VMs into Array
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
$AddArray = $VMArray.Add("VM03")
$AddArray = $VMArray.Add("VM04")
# Get Destination Cloud
$Cloud = Get-SCCloud -Name $DestinationCloud
# Cloud Host Group Path
$HostGroupPath = (($Cloud.HostGroup)[0]).Path + "*"
foreach ($VMName in $VMArray)
{
# Get VM
$VM = Get-SCVirtualMachine -Name $VMName
# Output
Write-Host VM Name: $VMName
Write-Host " "
if ($VM.HasPassthroughDisk -eq $False -and $VM.HasVirtualFibreChannelAdapters -eq $False)
{
# Current Cloud
$CurrentCloud = Get-SCCloud -Name $VM.Cloud.Name
# Current Hyper-V Host
$CurrentVMHost = Get-SCVMHost -ComputerName $VM.VMHost.Name
# Create Job Guid
$JobGuid = [System.Guid]::NewGuid().toString()
# Remove from Cloud
$SetCloud = $VM | Set-SCVirtualMachine -RemoveFromCloud
# Get Best Available Hyper-V Host
$VMHostName = ((Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
# Output
Write-Host Target Host: $VMHostName
# Get Best Available CSV
$VolumeName = ((Get-SCStorageVolume | Where {$_.VMHost -eq $VMHostName -and $_.IsClusterSharedVolume -eq $True} | Select Name,FreeSpace | Sort FreeSpace -Descending)[0]).Name
# Output
Write-Host Target Volume: $VolumeName
Write-Host " "
# Get Hyper-V Host Information
$VMHost = Get-SCVMHost -ComputerName $VMHostName
[int64]$VMHostAvailableMemory = [int64]$VMHost.AvailableMemory + 10240 # Leave 10 GB Available Memory
# Get CSV Information
$Volume = Get-SCStorageVolume -Name $VolumeName -VMHost $VMHostName
[int64]$VolumeFreeSpace = [int64]$Volume.FreeSpace + 107374182400 # Leave 100 GB Free Space
# Control Free Memory
if ($VM.Memory -lt $VMHostAvailableMemory -and $VM.TotalSize -lt $VolumeFreeSpace)
{
# Get Virtual Network Adapters
$VirtualNetworkAdapters = $VM | Get-SCVirtualNetworkAdapter
foreach ($VirtualNetworkAdapter in $VirtualNetworkAdapters)
{
# Clear VM Network
$VMNetwork = $Null;
# Get VM Network
$VMNetwork = Get-SCVMNetwork | Where {$_.Name -eq $VirtualNetworkAdapter.VMNetwork.Name}
if (!$VMNetwork)
{
$VMNetwork = Get-SCVMNetwork | Where {$_.VMSubnet.SubnetVLANs.VLanID -eq $VirtualNetworkAdapter.VLanID}
}
# Destination Virtual Network
$VirtualNetwork = ((Get-VM -Cloud $Cloud | Where {$_.VirtualNetworkAdapters.VMNetwork.Name -eq $VirtualNetworkAdapter.VMNetwork.Name -and $_.VirtualNetworkAdapters.VirtualNetwork})[0]).VirtualNetworkAdapters.VirtualNetwork
# Set VM Network Adapter
$SetSCVirtualNetworkAdapter = Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $VirtualNetworkAdapter -VirtualNetwork $VirtualNetwork -VMNetwork $VMNetwork -JobGroup $JobGuid
}
# Move VM
$MoveSCVirtualMachine = $VM | Move-SCVirtualMachine -VMHost $VMHostName -HighlyAvailable $True -UseLAN -UseDiffDiskOptimization -JobGroup $JobGuid -Path $VolumeName
Write-Host "Migration process is finished."
Write-Host "Please check job results to ensure that if operation is successful.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $Cloud
# Refresh VM
$RefreshVM = $VM | Refresh-VM
}
else
{
Write-Host "Not enough resources to move VM.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $CurrentCloud
}
}
else
{
Write-Host "VM has Pass-through disks or vHBA.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
}
} |
# Parameters
$DestinationCloud = "New Cloud"
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
# Add VMs into Array
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
$AddArray = $VMArray.Add("VM03")
$AddArray = $VMArray.Add("VM04")
# Get Destination Cloud
$Cloud = Get-SCCloud -Name $DestinationCloud
# Cloud Host Group Path
$HostGroupPath = (($Cloud.HostGroup)[0]).Path + "*"
foreach ($VMName in $VMArray)
{
# Get VM
$VM = Get-SCVirtualMachine -Name $VMName
# Output
Write-Host VM Name: $VMName
Write-Host " "
if ($VM.HasPassthroughDisk -eq $False -and $VM.HasVirtualFibreChannelAdapters -eq $False)
{
# Current Cloud
$CurrentCloud = Get-SCCloud -Name $VM.Cloud.Name
# Current Hyper-V Host
$CurrentVMHost = Get-SCVMHost -ComputerName $VM.VMHost.Name
# Create Job Guid
$JobGuid = [System.Guid]::NewGuid().toString()
# Remove from Cloud
$SetCloud = $VM | Set-SCVirtualMachine -RemoveFromCloud
# Get Best Available Hyper-V Host
$VMHostName = ((Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
# Output
Write-Host Target Host: $VMHostName
# Get Best Available CSV
$VolumeName = ((Get-SCStorageVolume | Where {$_.VMHost -eq $VMHostName -and $_.IsClusterSharedVolume -eq $True} | Select Name,FreeSpace | Sort FreeSpace -Descending)[0]).Name
# Output
Write-Host Target Volume: $VolumeName
Write-Host " "
# Get Hyper-V Host Information
$VMHost = Get-SCVMHost -ComputerName $VMHostName
[int64]$VMHostAvailableMemory = [int64]$VMHost.AvailableMemory + 10240 # Leave 10 GB Available Memory
# Get CSV Information
$Volume = Get-SCStorageVolume -Name $VolumeName -VMHost $VMHostName
[int64]$VolumeFreeSpace = [int64]$Volume.FreeSpace + 107374182400 # Leave 100 GB Free Space
# Control Free Memory
if ($VM.Memory -lt $VMHostAvailableMemory -and $VM.TotalSize -lt $VolumeFreeSpace)
{
# Get Virtual Network Adapters
$VirtualNetworkAdapters = $VM | Get-SCVirtualNetworkAdapter
foreach ($VirtualNetworkAdapter in $VirtualNetworkAdapters)
{
# Clear VM Network
$VMNetwork = $Null;
# Get VM Network
$VMNetwork = Get-SCVMNetwork | Where {$_.Name -eq $VirtualNetworkAdapter.VMNetwork.Name}
if (!$VMNetwork)
{
$VMNetwork = Get-SCVMNetwork | Where {$_.VMSubnet.SubnetVLANs.VLanID -eq $VirtualNetworkAdapter.VLanID}
}
# Destination Virtual Network
$VirtualNetwork = ((Get-VM -Cloud $Cloud | Where {$_.VirtualNetworkAdapters.VMNetwork.Name -eq $VirtualNetworkAdapter.VMNetwork.Name -and $_.VirtualNetworkAdapters.VirtualNetwork})[0]).VirtualNetworkAdapters.VirtualNetwork
# Set VM Network Adapter
$SetSCVirtualNetworkAdapter = Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $VirtualNetworkAdapter -VirtualNetwork $VirtualNetwork -VMNetwork $VMNetwork -JobGroup $JobGuid
}
# Move VM
$MoveSCVirtualMachine = $VM | Move-SCVirtualMachine -VMHost $VMHostName -HighlyAvailable $True -UseLAN -UseDiffDiskOptimization -JobGroup $JobGuid -Path $VolumeName
Write-Host "Migration process is finished."
Write-Host "Please check job results to ensure that if operation is successful.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $Cloud
# Refresh VM
$RefreshVM = $VM | Refresh-VM
}
else
{
Write-Host "Not enough resources to move VM.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $CurrentCloud
}
}
else
{
Write-Host "VM has Pass-through disks or vHBA.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
}
}
After migrations, please check SCVMM job results to see if migrations are successful.
Posted in Windows Powershell, Windows Server | No Comment | 998 views | 14/02/2014 18:37
You can use following script to see if remote host has cluster service:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| $Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
try
{
$State = (Get-WmiObject -ComputerName $Server -Class Win32_service | Where Name -like *clu*).State
}
catch
{
}
if ($State)
{
Write-Warning $Server
Add-Content -Value $Server -Path C:\ClusterResults.txt
}
else
{
Write-Host $Server
}
} |
$Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
try
{
$State = (Get-WmiObject -ComputerName $Server -Class Win32_service | Where Name -like *clu*).State
}
catch
{
}
if ($State)
{
Write-Warning $Server
Add-Content -Value $Server -Path C:\ClusterResults.txt
}
else
{
Write-Host $Server
}
}
Script will output all results into C:\ClusterResults.txt.
Posted in Windows Powershell, Windows Server | No Comment | 1,254 views | 14/02/2014 17:39
You can get QLogic Driver Versions of HP Servers with following script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| $Clusters = Get-Content C:\Clusters.txt
foreach ($Cluster in $Clusters)
{
$Servers = (Get-Cluster $Cluster | Get-ClusterNode).Name
foreach ($Server in $Servers)
{
try
{
$ProductVersion = $Null;
$ProductVersion = (Get-Item \\$Server\C$\Windows\System32\Drivers\ql2300.sys).VersionInfo.ProductVersion
}
catch
{
}
$Value = $Server + ";" + $ProductVersion
Add-Content -Value $Value -Path C:\Results.txt
}
} |
$Clusters = Get-Content C:\Clusters.txt
foreach ($Cluster in $Clusters)
{
$Servers = (Get-Cluster $Cluster | Get-ClusterNode).Name
foreach ($Server in $Servers)
{
try
{
$ProductVersion = $Null;
$ProductVersion = (Get-Item \\$Server\C$\Windows\System32\Drivers\ql2300.sys).VersionInfo.ProductVersion
}
catch
{
}
$Value = $Server + ";" + $ProductVersion
Add-Content -Value $Value -Path C:\Results.txt
}
}
Script will output all results into C:\Results.txt.
Posted in Windows Powershell, Windows Server | No Comment | 1,208 views | 14/02/2014 10:10
You can type your servers into C:\Servers.txt and check WBEM status.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| $Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
try
{
$TestWBEM = (Get-Item "\\$Server\C$\Program Files\HPWBEM\Tools\HPWbemTestEvent.exe" -ErrorAction SilentlyContinue).Exists
}
catch
{
}
if ($TestWBEM -eq $True) { $TestWBEM = "True"; } else { $TestWBEM = "False"; }
$Value = $Server + ";" + $TestWBEM
Add-Content -Value $Value -Path C:\Results.txt
} |
$Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
try
{
$TestWBEM = (Get-Item "\\$Server\C$\Program Files\HPWBEM\Tools\HPWbemTestEvent.exe" -ErrorAction SilentlyContinue).Exists
}
catch
{
}
if ($TestWBEM -eq $True) { $TestWBEM = "True"; } else { $TestWBEM = "False"; }
$Value = $Server + ";" + $TestWBEM
Add-Content -Value $Value -Path C:\Results.txt
}
That will export all data into C:\Results.txt.
Posted in Virtual Machine Manager | No Comment | 1,125 views | 13/02/2014 17:10
You can add DVD devices into SCVMM 2012 R2 templates with this script:
1
2
3
4
5
6
7
8
| $Templates = Get-SCVMTemplate
foreach ($Template in $Templates)
{
$JobId = [System.Guid]::NewGuid().toString()
New-SCVirtualDVDDrive -JobGroup $JobId -Bus 0 -LUN 1
$Template | Set-SCVMTemplate -JobGroup $JobId
} |
$Templates = Get-SCVMTemplate
foreach ($Template in $Templates)
{
$JobId = [System.Guid]::NewGuid().toString()
New-SCVirtualDVDDrive -JobGroup $JobId -Bus 0 -LUN 1
$Template | Set-SCVMTemplate -JobGroup $JobId
}
All DVD devices will be on “Bus 0, Lun 1”. You can change it for different location.
|