Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中集成Powershell_C#_Powershell_Scripting_Visual Studio 2013 - Fatal编程技术网

C# 在C中集成Powershell

C# 在C中集成Powershell,c#,powershell,scripting,visual-studio-2013,C#,Powershell,Scripting,Visual Studio 2013,需要将此PowerShell脚本集成到我的C程序集代码中。不知道怎么做。如果需要任何其他信息,请随时询问。提前谢谢 PowerShell脚本: #Script that does the linking and renaming: # Creates a variable called IncidentID and points Incident # to it for use within the script Param( [string]$IncidentID ) # Load the

需要将此PowerShell脚本集成到我的C程序集代码中。不知道怎么做。如果需要任何其他信息,请随时询问。提前谢谢

PowerShell脚本:

#Script that does the linking and renaming:

# Creates a variable called IncidentID and points Incident # to it for use within the script
Param(
[string]$IncidentID
)

# Load the SMlets module
Import-Module SMlets 

# Get the Incident Class
$IncClass = Get-SCSMClass -Name System.WorkItem.Incident$

# Get the RMA Class
$RMAClass = Get-SCSMClass -Name COMPANY.RMA.Class

# Build the Filter String
$FilterStr = "ID -eq " + $IncidentID

# Find the Incident we need to link to an RMA
$Inc = Get-SCSMObject -Class $IncClass -Filter $FilterStr
$RMAIncText = "[Linked to Incident " + $Inc.ID + "]"
$RMADescription = $RMAIncText 
New-SCSMObject -Class $RMAClass -PropertyHashtable (@{Title = $Inc.Title; Description = $RMADescription})

# Find the new RMA to be linked
$FilterStr = "Description -eq '$RMADescription'"
$RMA = Get-SCSMObject -Class $RMAClass -Filter $FilterStr

#Set RMA Number Variable
$RMANumber = $RMA.RMA_ID; 

#Clean up DisplayName, Title and Description  
$RMA | Set-SCSMObject -PropertyHashtable @{"DisplayName"  =  $RMANumber; "Title" =  $RMANumber; "Description" =  $RMANumber;}

## Create an Incident Related Items instance referencing the new RMA
$RWIClass = Get-SCSMRelationshipClass -Name System.WorkItemRelatesToWorkItem$
New-SCSMRelationshipObject -Relationship $RWIClass -Source $Inc -Target $RMA -Bulk

# Unload the SMlets module
Remove-Module SMlets
汇编代码:

public class RMATask : ConsoleCommand
    {
        public RMATask()
        {
        }
        public override void ExecuteCommand(IList<NavigationModelNodeBase> nodes, NavigationModelNodeTask task, ICollection<string> parameters)
        {
            IManagementGroupSession session = (IManagementGroupSession)FrameworkServices.GetService<IManagementGroupSession>();
            EnterpriseManagementGroup emg = session.ManagementGroup;

            ManagementPack mp = emg.ManagementPacks.GetManagementPack(new Guid("a82d62c5-ece0-35fd-a266-9afa246dea78"));
            ManagementPackClass mpc = emg.EntityTypes.GetClass(new Guid("4b081ab1-f48e-9c62-77bc-76bc31349030"));
            ManagementPackObjectTemplate mpt = emg.Templates.GetObjectTemplate(new Guid("92ed7c4d-aff5-819e-90f8-c92064c50cd6"));

            NavigationModelNodeBase nodeIn = nodes[0];

            NavigationModelNodeBase nmnbNew;
            NavigationModelNodeTask nmntNew = NavigationTasksHelper.CreateNewInstanceLink(mpc, mpt);
            Microsoft.EnterpriseManagement.GenericForm.GenericCommon.MonitorCreatedForm(nodeIn, nmntNew, out nmnbNew);
        }
    }
对此处的详细信息感兴趣的人士可以: 问题 基本上,我们有帮助台分析员,他们负责生成事件。有时,他们可能需要生成RMA退货授权书,如果您不知道这是什么意思,只需知道这是他们需要填写的另一张表格,并且RMA需要与事件关联。事件不需要有RMA,但每个RMA都需要附加到相应的父事件

为此,我创建了一个名为COMPANY.RMA.class的新类,在Visual Studio中从头创建了一个新表单,并将MP管理包XML和form assembly.dll打包到MPB管理包包包包中。 我将其上传到控制台,并创建了一个名为创建RMA的新控制台任务,该任务在选择事件模块时可见

此任务将启动我的PowerShell脚本,该脚本将获取所选或打开的事件的ID,创建RMA对象,并将创建的RMA对象与事件的ID关联,以便稍后在事件的“相关项目”选项卡中看到它。 然而,我在这里遇到了一个问题。我正确地创建了链接功能,但我无法使RMA表单在创建后自动打开。相反,当任务运行时,它创建并保存对象,但分析师必须关闭事件并重新打开它以刷新新数据,导航到“相关项目”选项卡,并选择新创建的RMA以打开它并填写表单。这是大量的额外工作,应该取消。 正确的功能应该是创建RMA表单,将其与开放/选定事件关联,并启动刚刚创建的RMA表单,以允许分析师填写其详细信息

显然,没有直接从控制台任务调用/启动表单的函数。似乎我必须直接修改汇编代码才能访问我需要使用的层的SCSM SDK。这就是我迷路的地方——我不知道如何将我的PowerShell脚本转换为C汇编代码。非常感谢您的帮助。

您可以使用在应用程序中托管PowerShell

宿主应用程序可以定义运行命令的运行空间,在本地或远程计算机上打开会话,并根据应用程序的需要同步或异步调用命令。
这里有指导。

我最终解决了这个问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// using System.Threading.Tasks; <- .NET 4.5 only
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.ConnectorFramework;
// using Microsoft.EnterpriseManagement.UI.Core;
using Microsoft.EnterpriseManagement.UI.Extensions.Shared;
using Microsoft.EnterpriseManagement.UI.FormsInfra;
using Microsoft.EnterpriseManagement.UI.Core.Connection;
using Microsoft.EnterpriseManagement.UI.DataModel;
using Microsoft.EnterpriseManagement.UI.SdkDataAccess;
using Microsoft.EnterpriseManagement.UI.SdkDataAccess.DataAdapters;
using Microsoft.EnterpriseManagement.UI.WpfWizardFramework;
using Microsoft.EnterpriseManagement.ServiceManager.Application.Common;
using Microsoft.EnterpriseManagement.ConsoleFramework;
using Microsoft.EnterpriseManagement.GenericForm;
// using System.Management.Automation;
[assembly: CLSCompliant(true)]

namespace COMPANY.RMA
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>

    class RMATask : CreateWithLinkHandler
    {
        public RMATask()
        {


            try
            {
                // Sealed Class GUID
                this.createClassGuid = new Guid("9ebd95da-1b16-b9ea-274d-6b0c16ce1bf3");
                this.classToDelegate = new Dictionary<Guid, CreateLinkHelperCallback>()
                {
                    { ApplicationConstants.WorkItemTypeId, new CreateLinkHelperCallback (this.WorkItemCallback) }
                };
            }
            catch (Exception exc1)
            {
                MessageBox.Show(exc1.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public void WorkItemCallback(IDataItem RMAForm, IDataItem IncidentForm)
        {
            try
            {
                // Note to self: RelatedWorkItems should be in MP XML as alias under TypeProjections
                if (RMAForm != null && RMAForm.HasProperty("RelatedWorkItems"))
                {
                    // Perform Linking
                    RMAForm["RelatedWorkItems"] = IncidentForm;
                    // Copy Incident Title to RMA Title
                    RMAForm["Title"] = IncidentForm["Title"];
                    // Copy Incident Description to RMA Description
                    RMAForm["Description"] = IncidentForm["Description"];
                }
            }
            catch (Exception exc2)
            {
                MessageBox.Show(exc2.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }

您使用powershell脚本和c代码做什么?你所说的整合是什么意思?你想尝试格式化你的问题吗?如果你正在寻找一个可以帮助你的方法。@Admdew我以前在这个论坛上问过一个不同的问题,因为我的问题太具体,文本太多而被责骂。我试图缩短它,现在我有同样的反应--@SirLearnAlot,这绝对是一种平衡。技能是能够提供足够的信息来指导我们解决问题,而无需粘贴代码墙。不断地发布问题并磨练这项技能。你会到达那里的我可以把它添加到我的.dll汇编代码中吗?所以我添加了Powershell程序集引用,并创建了Powershell名称空间或其他什么?@SirLearnAlot-Yes。我建议先在一个简单的独立应用程序中使用其中一个。