Posted in
Virtual Machine Manager,
Windows Powershell,
Windows Server |
No Comment | 1,154 views | 05/06/2014 15:55
If you need to get Windows OS and Version information of a list of servers, then you can use following script.
1
2
3
4
5
6
7
8
9
10
| $Servers = Get-Content Servers.txt
foreach ($Server in $Servers)
{
$OSInfo = $Null;
$CompInfo = $Null;
$OSInfo = Get-WmiObject -ComputerName $Server -Class "Win32_OperatingSystem"
$CompInfo = Get-WmiObject -ComputerName $Server -Class "Win32_ComputerSystem"
$Value = $Server + ";" + $OSInfo.Caption + ";" + $OSInfo.CSDVersion + ";" + $OSInfo.BuildNumber + ";" + $OSInfo.Version + ";" + $CompInfo.Manufacturer + ";" + $CompInfo.Model
Add-Content -Value $Value -Path ServerOSInfo.txt
} |
$Servers = Get-Content Servers.txt
foreach ($Server in $Servers)
{
$OSInfo = $Null;
$CompInfo = $Null;
$OSInfo = Get-WmiObject -ComputerName $Server -Class "Win32_OperatingSystem"
$CompInfo = Get-WmiObject -ComputerName $Server -Class "Win32_ComputerSystem"
$Value = $Server + ";" + $OSInfo.Caption + ";" + $OSInfo.CSDVersion + ";" + $OSInfo.BuildNumber + ";" + $OSInfo.Version + ";" + $CompInfo.Manufacturer + ";" + $CompInfo.Model
Add-Content -Value $Value -Path ServerOSInfo.txt
}
That will output all data into ServerOSInfo.txt file.
Posted in
Virtual Machine Manager,
Windows Powershell,
Windows Server |
No Comment | 897 views | 05/06/2014 07:30
You can migrate selected Virtual Machines in a Hyper-V server with following script.
That will migrate all virtual machines into target volume.
If target volume is not specified, it will look for best available CSV volume, and migrate virtual machine into that.
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
| # Target Volume
$TargetVolume = "Volume11"
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
# Get All Virtual Machines
$AllVMs = Get-VM
foreach ($VM in $AllVMs)
{
# Get VM and Cluster Information
$VMName = $VM.Name
if ($VMArray.Contains($VMName) -eq $True)
{
Write-Host " "
Write-Host Working on $VMName ..
if (!$TargetVolume)
{
# Get Volume and Memory Information
$Volume = ((Get-ClusterSharedVolume | Select -ExpandProperty SharedVolumeInfo | Select @{label="Name";expression={(($_.FriendlyVolumeName).Split("\"))[-1]}},@{label="FreeSpace";expression={($_ | Select -Expand Partition).FreeSpace}} | Sort FreeSpace -Descending)[0]).Name
}
else
{
$Volume = $TargetVolume
}
# Move Virtual Machine
Move-VMStorage -VMName $VMName -DestinationStoragePath C:\ClusterStorage\$Volume\$VMName
Write-Host Done.
}
else
{
Write-Host " "
Write-Host Skipping $VMName ..
}
} |
# Target Volume
$TargetVolume = "Volume11"
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
# Get All Virtual Machines
$AllVMs = Get-VM
foreach ($VM in $AllVMs)
{
# Get VM and Cluster Information
$VMName = $VM.Name
if ($VMArray.Contains($VMName) -eq $True)
{
Write-Host " "
Write-Host Working on $VMName ..
if (!$TargetVolume)
{
# Get Volume and Memory Information
$Volume = ((Get-ClusterSharedVolume | Select -ExpandProperty SharedVolumeInfo | Select @{label="Name";expression={(($_.FriendlyVolumeName).Split("\"))[-1]}},@{label="FreeSpace";expression={($_ | Select -Expand Partition).FreeSpace}} | Sort FreeSpace -Descending)[0]).Name
}
else
{
$Volume = $TargetVolume
}
# Move Virtual Machine
Move-VMStorage -VMName $VMName -DestinationStoragePath C:\ClusterStorage\$Volume\$VMName
Write-Host Done.
}
else
{
Write-Host " "
Write-Host Skipping $VMName ..
}
}
This script should work even if you have pass-through disks on virtual machine or virtual hba.
Posted in
Virtual Machine Manager,
Windows Powershell,
Windows Server |
No Comment | 2,128 views | 02/06/2014 09:35
If you need to increase memory of your virtual machines, you can use following script.
You need to create a txt file like:
VM01;2048
VM02;4096
Save it as Memory.txt and run following script:
1
2
3
4
5
6
7
8
9
10
| $Servers = Get-Content Memory.txt
foreach ($Server in $Servers)
{
$ServerName = $Server.Split(";")[0]
$ServerName = $ServerName.Split(".")[0]
$Memory = $Server.Split(";")[1]
Stop-VM $ServerName
Get-VM $ServerName | Set-VM -MemoryMB $Memory
Start-VM $ServerName
} |
$Servers = Get-Content Memory.txt
foreach ($Server in $Servers)
{
$ServerName = $Server.Split(";")[0]
$ServerName = $ServerName.Split(".")[0]
$Memory = $Server.Split(";")[1]
Stop-VM $ServerName
Get-VM $ServerName | Set-VM -MemoryMB $Memory
Start-VM $ServerName
}
So that will stop VM, change memory and start VM again.