Skip to content

Commit 4dcc399

Browse files
committed
Chore: Refactor / Cleanup code
1 parent af08f8c commit 4dcc399

1 file changed

Lines changed: 62 additions & 63 deletions

File tree

Source/NETworkManager.Models/Network/DiscoveryProtocol.cs

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -99,82 +99,81 @@ public void CaptureAsync(int duration, DiscoveryProtocol protocol)
9999
{
100100
Task.Run(() =>
101101
{
102-
using (var powerShell = System.Management.Automation.PowerShell.Create())
103-
{
104-
powerShell.AddScript("Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process");
105-
powerShell.AddScript("Import-Module NetAdapter");
106-
powerShell.AddScript(_psDiscoveryProtocolModule);
107-
powerShell.AddScript($"Invoke-DiscoveryProtocolCapture -Duration {duration}" +
108-
(protocol != DiscoveryProtocol.LldpCdp
109-
? $" -Type {protocol.ToString().ToUpper()}"
110-
: "") + " -Force | Get-DiscoveryProtocolData");
102+
using var ps = System.Management.Automation.PowerShell.Create();
103+
104+
var typeParam = protocol != DiscoveryProtocol.LldpCdp ? $" -Type {protocol.ToString().ToUpper()}" : "";
111105

112-
var psOutputs = powerShell.Invoke();
106+
ps.AddScript($@"
107+
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
108+
Import-Module NetAdapter -ErrorAction Stop
109+
{_psDiscoveryProtocolModule}
110+
Invoke-DiscoveryProtocolCapture -Duration {duration}{typeParam} -Force | Get-DiscoveryProtocolData");
113111

114-
if (powerShell.Streams.Error.Count > 0)
115-
{
116-
StringBuilder stringBuilder = new();
112+
var results = ps.Invoke();
117113

118-
foreach (var error in powerShell.Streams.Error)
119-
{
120-
if (string.IsNullOrEmpty(stringBuilder.ToString()))
121-
stringBuilder.Append(Environment.NewLine);
114+
if (ps.Streams.Error.Count > 0)
115+
{
116+
StringBuilder stringBuilder = new();
122117

123-
stringBuilder.Append(error.Exception.Message);
124-
}
118+
foreach (var error in ps.Streams.Error)
119+
{
120+
if (string.IsNullOrEmpty(stringBuilder.ToString()))
121+
stringBuilder.Append(Environment.NewLine);
125122

126-
OnErrorReceived(new DiscoveryProtocolErrorArgs(stringBuilder.ToString()));
123+
stringBuilder.Append(error.Exception.Message);
127124
}
128125

129-
if (powerShell.Streams.Warning.Count > 0)
130-
{
131-
StringBuilder stringBuilder = new();
126+
OnErrorReceived(new DiscoveryProtocolErrorArgs(stringBuilder.ToString()));
127+
}
132128

133-
foreach (var warning in powerShell.Streams.Warning)
134-
{
135-
if (string.IsNullOrEmpty(stringBuilder.ToString()))
136-
stringBuilder.Append(Environment.NewLine);
129+
if (ps.Streams.Warning.Count > 0)
130+
{
131+
StringBuilder stringBuilder = new();
137132

138-
stringBuilder.Append(warning.Message);
139-
}
133+
foreach (var warning in ps.Streams.Warning)
134+
{
135+
if (string.IsNullOrEmpty(stringBuilder.ToString()))
136+
stringBuilder.Append(Environment.NewLine);
140137

141-
OnWarningReceived(new DiscoveryProtocolWarningArgs(stringBuilder.ToString()));
138+
stringBuilder.Append(warning.Message);
142139
}
143140

144-
foreach (var outputItem in psOutputs)
141+
OnWarningReceived(new DiscoveryProtocolWarningArgs(stringBuilder.ToString()));
142+
}
143+
144+
foreach (var result in results)
145+
{
146+
if (result == null)
147+
continue;
148+
149+
List<string> ipAddresses = [];
150+
151+
if (result.Properties["IPAddress"] != null)
152+
ipAddresses.AddRange(result.Properties["IPAddress"].Value as List<string>);
153+
154+
List<string> managements = [];
155+
156+
if (result.Properties["Management"] != null)
157+
managements.AddRange(result.Properties["Management"].Value as List<string>);
158+
159+
var packageInfo = new DiscoveryProtocolPackageInfo
145160
{
146-
if (outputItem == null)
147-
continue;
148-
149-
List<string> ipAddresses = new();
150-
151-
if (outputItem.Properties["IPAddress"] != null)
152-
ipAddresses.AddRange(outputItem.Properties["IPAddress"].Value as List<string>);
153-
154-
List<string> managements = new();
155-
156-
if (outputItem.Properties["Management"] != null)
157-
managements.AddRange(outputItem.Properties["Management"].Value as List<string>);
158-
159-
var packageInfo = new DiscoveryProtocolPackageInfo
160-
{
161-
Device = outputItem.Properties["Device"]?.Value.ToString(),
162-
DeviceDescription = outputItem.Properties["SystemDescription"]?.Value.ToString(),
163-
Port = outputItem.Properties["Port"]?.Value.ToString(),
164-
PortDescription = outputItem.Properties["PortDescription"]?.Value.ToString(),
165-
Model = outputItem.Properties["Model"]?.Value.ToString(),
166-
IPAddress = string.Join("; ", ipAddresses),
167-
VLAN = outputItem.Properties["VLAN"]?.Value.ToString(),
168-
Protocol = outputItem.Properties["Type"]?.Value.ToString(),
169-
TimeToLive = outputItem.Properties["TimeToLive"]?.Value.ToString(),
170-
Management = string.Join("; ", managements),
171-
ChassisId = outputItem.Properties["ChassisId"]?.Value.ToString(),
172-
LocalConnection = outputItem.Properties["Connection"]?.Value.ToString(),
173-
LocalInterface = outputItem.Properties["Interface"]?.Value.ToString()
174-
};
175-
176-
OnPackageReceived(new DiscoveryProtocolPackageArgs(packageInfo));
177-
}
161+
Device = result.Properties["Device"]?.Value.ToString(),
162+
DeviceDescription = result.Properties["SystemDescription"]?.Value.ToString(),
163+
Port = result.Properties["Port"]?.Value.ToString(),
164+
PortDescription = result.Properties["PortDescription"]?.Value.ToString(),
165+
Model = result.Properties["Model"]?.Value.ToString(),
166+
IPAddress = string.Join("; ", ipAddresses),
167+
VLAN = result.Properties["VLAN"]?.Value.ToString(),
168+
Protocol = result.Properties["Type"]?.Value.ToString(),
169+
TimeToLive = result.Properties["TimeToLive"]?.Value.ToString(),
170+
Management = string.Join("; ", managements),
171+
ChassisId = result.Properties["ChassisId"]?.Value.ToString(),
172+
LocalConnection = result.Properties["Connection"]?.Value.ToString(),
173+
LocalInterface = result.Properties["Interface"]?.Value.ToString()
174+
};
175+
176+
OnPackageReceived(new DiscoveryProtocolPackageArgs(packageInfo));
178177
}
179178

180179
OnComplete();

0 commit comments

Comments
 (0)