我相信你已经顺利通过了这个,但我目前正在开发一个需要类似功能的项目,我能够使用 .NET 和 PowerShell 提出一个解决方案来做到这一点。首先,我在 C# 中创建了一个自定义 cmdlet,如下所示:
using COMAdmin;
using System;
using System.Runtime.InteropServices;
using System.Management.Automation;
namespace COMAdminModule
{
// Name the cmdlet
[Cmdlet("Set", "COMConstructorString")]
public class SetCOMConstructorSting : PSCmdlet
{
// App name Parameter
private string comAppName;
[Parameter(
Mandatory = true,
ValueFromPipelineByPropertyName = true,
ValueFromPipeline = true,
Position = 0,
HelpMessage = "Name of COM+ Application"
)]
[Alias("App Name")]
public string COMApp
{
get { return comAppName; }
set { comAppName = value; }
}
// App Component name
private string componentName;
[Parameter(
Mandatory = true,
ValueFromPipelineByPropertyName = true,
ValueFromPipeline = true,
Position = 1,
HelpMessage = "The name of the Component that will receive a new Constructor string"
)]
[Alias("Component Name")]
public string ComponentName
{
get { return componentName; }
set { componentName = value; }
}
// Constructor String
private string constructorString;
[Parameter(
Mandatory = true,
ValueFromPipelineByPropertyName = true,
ValueFromPipeline = true,
Position = 2,
HelpMessage = "The new Constructor string"
)]
[Alias("Constructor String")]
public string ConstructorString
{
get { return constructorString; }
set { constructorString = value; }
}
// Provides a one-time, preprocessing functionality for the cmdlet
protected override void BeginProcessing()
{
base.BeginProcessing();
}
// Provides a record-by-record processing functionality for the cmdlet
protected override void ProcessRecord()
{
string working = "Setting the constructor string " + constructorString;
working = " to the Component " + componentName;
working += " for the COM App " + comAppName;
WriteObject(working);
setConstructorString(comAppName, componentName, constructorString);
}
// Provides a one-time, post-processing functionality for the cmdlet
protected override void EndProcessing()
{
base.EndProcessing();
}
//Add component method
private void setConstructorString(string comAppName, string componentName, string constructorString)
{
ICOMAdminCatalog2 oCatalog = null;
try
{
//Create the comAdmin object
oCatalog = (ICOMAdminCatalog2)Activator.CreateInstance(Type.GetTypeFromProgID("ComAdmin.COMAdminCatalog"));
//Get the comApps
ICatalogCollection comApps = (ICatalogCollection)oCatalog.GetCollection("Applications");
comApps.Populate();
foreach (ICatalogObject app in comApps)
{
//Find the comApp
if (app.Name.ToString().Equals(comAppName))
{
//Get the Components
ICatalogCollection components = (ICatalogCollection)comApps.GetCollection("Components", app.Key);
components.Populate();
foreach (ICatalogObject component in components)
{
//Find the component
if (component.Name.ToString().Equals(componentName))
{
// Set the constructor string
component.set_Value("ConstructorString", constructorString);
components.SaveChanges();
break;
}
}
break;
}
}
}
catch (Exception e)
{
WriteObject(e.Source);
throw;
}
}
}
}
然后将该模块导入 PowerShell 脚本并按如下方式运行它:
PS C:\Windows\system32> Import-Module "<dll path>"
PS C:\Windows\system32> Set-COMConstructorString <Application Name> <Component Name> <Constructor String>