-
Notifications
You must be signed in to change notification settings - Fork 40
/
PowerShellWrapper.cs
177 lines (163 loc) · 6.35 KB
/
PowerShellWrapper.cs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Microsoft.HyperV.PowerShell;
using System.Management.Automation.Runspaces;
using System.Management.Automation;
using Microsoft.Management.Infrastructure;
using System.Windows.Forms;
namespace DiscreteDeviceAssigner
{
class PowerShellWrapper
{
private static Collection<PSObject> RunScript(string scriptText)
{
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
return pipeline.Invoke();
}
}
private static Collection<string> GetPnpDeviceLocationPath(string instanceId)
{
Collection<string> results = new Collection<string>();
foreach (var dev in RunScript("Get-PnpDeviceProperty -InstanceId \"" + instanceId + "\" DEVPKEY_Device_LocationPaths"))
{
CimInstance ci = dev.BaseObject as CimInstance; if (ci == null) continue;
var data = ci.CimInstanceProperties["Data"]; if (data == null) continue;
var data2 = data.Value as IEnumerable<string>; if (data2 == null) continue;
foreach (var d in data2)
{
results.Add(d);
}
}
return results;
}
public static Collection<VirtualMachine> GetVM()
{
Collection<VirtualMachine> results = new Collection<VirtualMachine>();
foreach (var vm in RunScript("Get-VM"))
{
if (vm.BaseObject is VirtualMachine)
{
results.Add(vm.BaseObject as VirtualMachine);
}
}
return results;
}
public static Collection<VMAssignedDevice> GetVMAssignableDevice(VirtualMachine vm)
{
Collection<VMAssignedDevice> results = new Collection<VMAssignedDevice>();
foreach (var vmad in RunScript("Get-VMAssignableDevice -VMName \"" + vm.Name + "\""))
{
if (vmad.BaseObject is VMAssignedDevice)
{
results.Add(vmad.BaseObject as VMAssignedDevice);
}
}
return results;
}
public static CimInstance GetPnpDevice(string instanceId)
{
foreach (var dev in RunScript("Get-PnpDevice -InstanceId \"" + instanceId + "\""))
{
if (dev.BaseObject is CimInstance)
{
return dev.BaseObject as CimInstance;
}
}
return null;
}
public static Collection<CimInstance> GetPnpDevice()
{
Collection<CimInstance> results = new Collection<CimInstance>();
foreach (var dev in RunScript("Get-PnpDevice"))
{
if (dev.BaseObject is CimInstance)
{
results.Add(dev.BaseObject as CimInstance);
}
}
return results;
}
public static void SetGuestControlledCacheTypes(VirtualMachine vm, bool value)
{
if (value)
{
RunScript("Set-VM \"" + vm.Name + "\" -GuestControlledCacheTypes $true");
}
else
{
RunScript("Set-VM \"" + vm.Name + "\" -GuestControlledCacheTypes $false");
}
}
public static void SetLowMemoryMappedIoSpace(VirtualMachine vm, uint bytes)
{
RunScript("Set-VM \"" + vm.Name + "\" -LowMemoryMappedIoSpace " + bytes);
}
public static void SetHighMemoryMappedIoSpace(VirtualMachine vm, ulong bytes)
{
RunScript("Set-VM \"" + vm.Name + "\" -HighMemoryMappedIoSpace " + bytes);
}
public static void RemoveVMAssignableDevice(VirtualMachine vm, VMAssignedDevice device)
{
RunScript("Remove-VMAssignableDevice -LocationPath \"" + device.LocationPath + "\" -VMName \"" + vm.Name + "\"");
try
{
RunScript("Mount-VmHostAssignableDevice -LocationPath \"" + device.LocationPath + "\"");
}
catch { }
try
{
RunScript("Enable-PnpDevice -InstanceId \"" + device.InstanceID + "\" -Confirm:$false");
}
catch { }
}
public static void AddVMAssignableDevice(VirtualMachine vm, CimInstance device)
{
string id = device.CimInstanceProperties["DeviceId"] != null ? device.CimInstanceProperties["DeviceId"].Value as string : null;
var locationPaths = GetPnpDeviceLocationPath(id);
if (locationPaths.Count == 0) throw new InvalidOperationException("无法添加指定类型的设备");
try
{
if (vm.AutomaticStopAction != StopAction.TurnOff)
{
RunScript("Set-VM -AutomaticStopAction:TurnOff -VMName \"" + vm.Name + "\"");
}
}
catch { }
try
{
if (vm.DynamicMemoryEnabled && vm.MemoryStartup != vm.MemoryMinimum)
{
RunScript("Set-VM -MemoryStartupBytes:" + vm.MemoryMinimum + " -VMName \"" + vm.Name + "\"");
}
}
catch { }
try
{
if (!vm.GuestControlledCacheTypes)
{
SetGuestControlledCacheTypes(vm, true);
}
}
catch { }
try
{
RunScript("Disable-PnpDevice -InstanceId \"" + id + "\" -Confirm:$false");
}
catch { }
try
{
RunScript("Dismount-VmHostAssignableDevice -LocationPath \"" + locationPaths[0] + "\" -force");
}
catch { }
RunScript("Add-VMAssignableDevice -LocationPath \"" + locationPaths[0] + "\" -VMName \"" + vm.Name + "\"");
}
}
}