Integrate Powershell in C# - c#

Need to integrate this PowerShell script into my C# Assembly code. No idea how to do this. If any other information is needed please do not hesitate to ask. Thanks in advance.
PowerShell Script:
#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
Assembly Code:
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);
}
}
For those interested in the details here they are:
Problem
Basically, we have help desk analysts who generate the incidents. SOMETIMES, they may have a need to generate an RMA (Return Merchandise Authorization, if you don't know what that means, just know that it is another form they need to fill out), and that RMA needs to be associated with an incident. An incident does not REQUIRE to have an RMA, but every RMA needs to be attached to its appropriate parent incident.
To do this I created a new class called COMPANY.RMA.Class, created a new form from scratch in Visual Studio, and packaged the MP (Management Pack) XML and form assembly (.dll) into an MPB (management pack bundle).
I uploaded this to the console, and created a new console task called "Create RMA" that became visible when selecting the incident module.
This task would launch my PowerShell script, which in turn would take the ID of the incident selected or opened, create an RMA object, and associate the RMA object created with the ID # of the Incident (allowing it to be seen later in the "Related Items" Tab of the incident).
However, I ran into a problem here. I create the linking functionality correctly, but I cannot get the RMA form to automatically open up after it is created. Instead, when the task is run, it creates the object and saves it, but the analyst has to close the incident and reopen it to refresh the new data, navigate to the "Related Items" tab, and select the newly created RMA to open it up and fill out the form. This is alot of extra work and should be eliminated.
The correct functionality should be to create the RMA form, associate it with the open/selected incident, and launch the RMA form it just created to allow the analyst to fill in its details.
Apparently there is no function to call/launch the form directly from the console task. It seems I must modify the assembly code directly to be able to access the SCSM SDK (the layer I need to be working in). This is where I am lost - I have no idea how to convert my PowerShell script to C# Assembly code. Any help would be greatly appreciated.

You may use the PowerShell class to host PowerShell in your app.
The host application can define the runspace where commands are run, open sessions on a local or remote computer, and invoke the commands either synchronously or asynchronously based on the needs of the application.
There's guidance here.

I ended up solving this alternatively.
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);
}
}
}

Related

Error when add new detail row in Payments and Applications (AR302000) screen because of customization (in Acumatia ERP System)

I have already create some customization in screen Payment and Application of Acumatica ERP. I have created new Extension of ARPaymentEntryExtension.cs
The following is the source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using PX.Data;
using PX.Objects.AR;
using PX.Objects.GL;
namespace SGLCustomizeProject
{
public class ARPaymentEntryExtension: PXGraphExtension<ARPaymentEntry>
{
#region Override Button Menu
public override void Initialize()
{
Base.report.AddMenuAction(ReceiptVoucher);
}
#endregion
#region Button Receipt Vocher
public PXAction<ARPayment> ReceiptVoucher;
[PXButton]
[PXUIField(DisplayName = "Receipt Voucher")]
public IEnumerable receiptVoucher(PXAdapter adapter)
{
var result = adapter.Get<ARPayment>();
foreach (ARPayment doc in result)
{
object FinPeriodID;
if (Base.Caches[typeof(ARPayment)].GetStatus(doc) == PXEntryStatus.Notchanged)
{
Base.Caches[typeof(ARPayment)].SetStatus(doc, PXEntryStatus.Updated);
}
Base.Save.Press();
var docPeriod = (FinPeriodID = Base.Caches[typeof(ARPayment)].GetValueExt<ARRegister.finPeriodID>(doc)) is PXFieldState ? (string)((PXFieldState)FinPeriodID).Value : (string)FinPeriodID;
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters["ReferenceNbr"] = doc.RefNbr;
throw new PXReportRequiredException(parameters, "AR909095", "Report");
}
return result;
}
#endregion
}
}
I used extensoin above to preview report from current screen and it works.
When user need to create new document and then add some detail document and then click on save button, it will work.
But, when user need to add another detail document and then click on save button, system will show the error message.
Please refer to the following screenshot.
Actually the error message appear after Acumatica was upgraded into version 2017 R2 - Build 17.207.0029.
In previous version (Version 5.3 - Build 5.30.4209) it work fine.
Does anyone know how to solve this issue ?
I have solve this problem by remove the following If Condition of the code:
if (Base.Caches[typeof(ARPayment)].GetStatus(doc) == PXEntryStatus.Notchanged)
{
Base.Caches[typeof(ARPayment)].SetStatus(doc, PXEntryStatus.Updated);
}
After remove code above, the customization is work and no error.

Set HTML Signature on Lotus Notes with no user interaction

I have a small VS C# program that modifies some data from an email signature on HTML format that will be used later in Lotus Notes 8.5.3
Each user is able to generate his own .html file filled with his personal data from the common template.
My problem is that now I would like the software to save the .html file on Lotus Notes Data directory and then configure the client to use that specific signature without the user having to go through the menues "More-->Preferences-->Signature" himself.
Here is my code so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace FirmaCorreo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void Generar_Click(object sender, RoutedEventArgs e)
{
string suSaludo;
suSaludo = Texto1.Text;
string suNombre;
suNombre = Texto2.Text;
string suCargo;
suCargo = Texto3.Text;
string suCargo2;
suCargo2 = Texto4.Text;
string suMovil;
suMovil = Texto5.Text;
string suDirecto;
suDirecto = Texto6.Text;
string suDelegacion;
if (DelegacionVillajoyosa.IsChecked == true)
{
suDelegacion = "Villajoyosa";
}
else if (DelegacionCanarias.IsChecked == true)
{
suDelegacion = "Canarias";
}
else if (DelegacionVigo.IsChecked == true)
{
suDelegacion = "Vigo";
}
else
{
suDelegacion = "Francia";
}
var contents = System.IO.File.ReadAllText(#"C:\Users\Public\Documents\IS-IT\FirmaCorreo\Plantilla\Plantillacorreo.html", Encoding.GetEncoding(28591));
contents = contents.Replace("<!--Saludo-->", suSaludo);
contents = contents.Replace("<!--NombreDelEmpleado-->", suNombre);
contents = contents.Replace("<!--CargoDelEmpleado-->", suCargo);
contents = contents.Replace("<!--CargoDelEmpleado2-->", "<br />"+suCargo2);
contents = contents.Replace("<!--TelefonoMovil-->", "Mobile.- "+suMovil);
contents = contents.Replace("<!--TelefonoDirecto-->", "<br />Direct.- "+suDirecto);
contents = contents.Replace("<!--Delegacion"+suDelegacion, "<!-- -->");
System.IO.File.WriteAllText(#"C:\Users\Public\Documents\IS-IT\FirmaCorreo\Plantilla\PlantillacorreoModificada.html", contents, Encoding.GetEncoding(28591));
}
}
}
On my Lotus Notes 8.5.3 installation I see a notes.ini file which seems to have the signature configuration:
...
$SigSet=1
$SigState=1
$SigSetValue=1
....
$tmpStrFilter=HTML File
$tmpSigPath=C:\Lotus\Notes\Data\FirmaCorreo\FirmaPedro.html
...
Notes allows three different options for mail signatures:
I thought maybe I just had to edit notes.ini on each client to set up the resulting HTML file from my program.
The problem is notes.ini doesn't change when I change the type of signature on my client.
Is there any way to get the resulting HTML file to be the default signature on Lotus Notes without asking each user to go to the Preferences menu and select the right settings?
Can I have my C# program do that for them?
The html file path is stored in a profile document in the Mail database called $CalendarProfile and the field Signature2
You might be able hook into Notes using Microsoft COM objects(windows) and read the document, but then the code need to run on the client machine using the users password.
Another approach can be to create a lotusscript that updates the profile when user open the mailfile for the first time, this can be done once by changing the mail template and pushing it out to all users.
Keep in mind that a signature referenced to local disk will not work in webmail

Tracking files of a particular extension

I want to track the files which are opened by the user, and select them by one particular extension. If the opened file has that extension, then I want to assign it's file path to a variable for further processing. Example applications are very cpu demanding. Is there an easy, efficient way to do that?
System wide monitoring of file-->open events (including network drives, thumb drives, etc) would require you to write a FS filter driver.
Since you have access to the machine, and you definitely need system wide access, you could simply write a simple app that will be associated with the Powerpoint extensions, perform the copy, then open Powerpoint using the filepath as a command line argument. It would look similar to the following:
using System;
using System.Windows;
using System.Diagnostics;
using System.IO;
namespace WpfApplication1
{
internal class MainWindow : Window
{
public MainWindow()
{ }
[STAThread()]
static void Main(string[] args)
{
if (args.Length == 0)
{
// [ show error or print usage ]
return;
}
if (!File.Exists(args[0]))
{
// [ show error or print usage ]
return;
}
// Perform the copy
FileInfo target = new FileInfo(args[0]);
string destinationFilename = string.Format("X:\\ExistingFolder\\{0}", target.Name);
File.Copy(target.FullName, destinationFilename);
// You may need to place the filename in quotes if it contains spaces
string targetPath = string.Format("\"{0}\"", target.FullName);
string powerpointPath = "[FullPathToPowerpointExecutable]";
Process powerpointInstance = Process.Start(powerpointPath, targetPath);
// This solution is using a wpf windows app to avoid
// the flash of the console window. However if you did
// wish to display an accumulated list then you may choose
// to uncomment the following block to display your UI.
/*
Application app = new Application();
app.MainWindow = new MainWindow();
app.MainWindow.ShowDialog();
app.Shutdown(0);
*/
Environment.Exit(0);
}
}
}
Hope this helps.

unable to connect to R from c#

I am trying to connect to R from c# using the following code. It looks like C# is not reading the R dll files. My R installation directory is this:
C:\Users\R-2-13\R-2.13.0\bin\i386
and I also downloaded and put the R.NET.dll in the same directory. In Visual Studio, I set the reference to R.NET.dll file. When I run the following code, the code goes the the catch section "unable to find the R Installation". Any ideas? Has anybody got this working?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RDotNet;
namespace RNet_Calculator
{
public partial class Form1 : Form
{
// set up basics and create RDotNet instance
// if anticipated install of R is not found, ask the user to find it.
public Form1()
{
InitializeComponent();
bool r_located = false;
while (r_located == false)
{
try
{
REngine.SetDllDirectory(#"C:\Users\R-2-13\R-2.13.0\bin\i386");
REngine.CreateInstance("RDotNet");
r_located = true;
}
catch { MessageBox.Show(#"Unable to find R installation's \bin\i386 folder. Press OK to attempt to locate it.");
MessageBox.Show("error");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
This is http://rdotnet.codeplex.com/ (RDotNet) to develop Winform applications. While I know Shiny and all the other Web-like R-tools quite well, the combination of c# and R still is my preferred end-user combinations. Try simple things like disabling buttons with Shiny...
Too bad rdotnet is quite buggy; in the current version, it crashes on R exeptions, even in try-catched ones.
This said: please make absolutely sure that you use version 1.5, not the stupidly called "stable" (=early beta) version on the page. Best download it via NuGet. Also check if you did not mix 32bit R with 64 bit c#.
Using the Helper-functions of 1.5, initialization is:
Helper.SetEnvironmentVariables();
engine = REngine.CreateInstance(EngineName);
engine.Initialize();
# Assuming you want to catch the graphic window, use my RGraphAppHook
# on the rdotnet site http://rdotnet.codeplex.com/workitem/7
cbt = new RGraphAppHook { GraphControl = GraphPanelControl };

Im getting exception Typeload what the exception can be?

I added as reference 3 dll's: Google.Apis , Google.Apis.Translate.v2 , System.Runtime.Serialization
In Form1 i have one line:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Translator.translate(new TranslateInput());
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Now the error the exception is on the first line in the class Translator:
The line that throw the error is: var service = new TranslateService { Key = GetApiKey() };
The class code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using Google.Apis.Util;
using Google.Apis.Translate.v2;
using Google.Apis.Translate.v2.Data;
using TranslationsResource = Google.Apis.Translate.v2.Data.TranslationsResource;
public class Translator
{
public static string translate(TranslateInput input)
{
// Create the service.
var service = new TranslateService { Key = GetApiKey() };
string translationResult = "";
// Execute the first translation request.
Console.WriteLine("Translating to '" + input.TargetLanguage + "' ...");
TranslationsListResponse response = service.Translations.List(input.SourceText, input.TargetLanguage).Fetch();
var translations = new List<string>();
foreach (TranslationsResource translation in response.Translations)
{
translationResult = translation.TranslatedText;
}
return translationResult;
}
private static string GetApiKey()
{
return "AIzaSyCjxMe6RKHZzd7xSfSh2pEsBqUdXYm5tA8"; // Enter Your Key
}
}
/// <summary>
/// User input for this example.
/// </summary>
[Description("input")]
public class TranslateInput
{
[Description("text to translate")]
public string SourceText = "Who ate my candy?";
[Description("target language")]
public string TargetLanguage = "fr";
}
The error is:
Could not load type 'Google.Apis.Discovery.FactoryParameterV1_0' from assembly 'Google.Apis, Version=1.1.4497.35846, Culture=neutral, PublicKeyToken=null'.
Tried to google for help and also tried to change the project type to x64 platform but it didnt help. So i put it back on x86
I have windows 7 64bit visual studio c# 2010 pro .net 4.0 profile client.
Cant figure out what is the error ?
This error as reported in the above-posted messages is due to a local copy in the bin\Debug folder of your solution or project. Even though you attempt to clean your solution, such copies will persist to exist.
In order to avoid this to happen, you have to force Visual Studio to refer to the correct DLL by adding reference paths within a project properties. Unfortunately, if you got several projects within your solutions, you will have to set the reference paths for the projects one after another until completed.
Should you wish to know how to setup reference paths follow these simple instructions:
1.Select your project, right-click, then click "Properties";
2.In the project properties, click "Reference Paths";
3.Folder, type or browse to the right location of your DLL, click [Add Folder].
You will need to perform these steps for as many different locations you may have for each of your DLLs. Consider setting an output path under the Build tab of the same project properties, so that you may output your DLLs in the same directory for each of them, thus assuring you to find all the latest builds under the same location, simplifying forward your referencing.
Note this can only be one reason for this error. But it is sure that is has to do something with a wrong copy of the mentioned assembly.

Categories

Resources