// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
namespace Microsoft.Samples.HyperV.FibreChannel
{
using System;
using System.Collections.Generic;
using System.Management;
using System.Globalization;
using Microsoft.Samples.HyperV.Common;
static class ConfigureWwnGenerator
{
///
/// The WWN Generator uses the MinimumWWPNAddress, MaximumWWPNAddress
/// and CurrentWWNNAddress of the Msvm_VirtualSystemManagementServiceSettingData.
/// These properties are modified through the ModifyServiceSettings() method
/// of the Msvm_VirtualSystemManagementService class.
///
/// The Minimum WWPN for the WWPN range.
/// The Maximum WWPN for the WWPN range.
/// The new WWNN value. If null, do not modify the CurrentWWNNAddress.
private static void
ConfigWwnGenerator(
string minWwpn,
string maxWwpn,
string newWwnn)
{
Console.WriteLine("Trying to configure the WWN generator with:");
Console.WriteLine("\tMinimumWWPNAddress = {0}", minWwpn);
Console.WriteLine("\tMaximumWWPNAddress = {0}", maxWwpn);
if (newWwnn == null)
{
Console.WriteLine("\tThe CurrentWWNNAddress will not be modified.");
}
else
{
Console.WriteLine("\tCurrentWWNNAddress = {0}", newWwnn);
}
ManagementScope scope = FibreChannelUtilities.GetFcScope();
using (ManagementObject service =
WmiUtilities.GetVirtualMachineManagementService(scope))
using (ManagementObject settings =
WmiUtilities.GetVirtualMachineManagementServiceSettings(scope))
using (ManagementBaseObject inParams =
service.GetMethodParameters("ModifyServiceSettings"))
{
settings["MinimumWWPNAddress"] = minWwpn;
settings["MaximumWWPNAddress"] = maxWwpn;
if (newWwnn != null)
{
settings["CurrentWWNNAddress"] = newWwnn;
}
inParams["SettingData"] = settings.GetText(TextFormat.WmiDtd20);
using (ManagementBaseObject outParams =
service.InvokeMethod("ModifyServiceSettings",
inParams,
null))
{
WmiUtilities.ValidateOutput(outParams, scope, true, true);
}
}
Console.WriteLine("Successfully configured the WWN Generator.");
}
///
/// Entry point for the CreateSan sample.
///
/// The command line arguments.
internal static void
ExecuteSample(
string[] args)
{
if (args.Length < 2 || (args.Length > 0 && args[0] == "/?"))
{
Console.WriteLine("Usage: ConfigureWwnGenerator [New WWNN]");
return;
}
try
{
string minWwpn = args[0];
string maxWwpn = args[1];
string newWwnn = null;
if (args.Length > 2)
{
newWwnn = args[2];
}
ConfigWwnGenerator(minWwpn, maxWwpn, newWwnn);
}
catch (Exception ex)
{
Console.WriteLine("Failed to configure WWN generator. Error message details:\n");
Console.WriteLine(ex.Message);
}
}
}
}