×
Felmeddelande :( Din CSS har inte laddats som den ska. Testa reloada sidan.
1

c# + Powershell

Postat av hessas den 5 September 2016, 22:31
14 kommentarer · 686 träffar
Tjenare, behöver lite hjälp med ett program jag skapar..
Koden i C# ser ut så här:
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;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.IO;

namespace ClientCheck
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private string RunScript(string scriptText)
{
// create Powershell runspace
InitialSessionState initial = InitialSessionState.CreateDefault();
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
//Runspace runspace = RunspaceFactory.CreateRunspace();

// open it
ps.Runspace.Open();
ps.Runspace.SessionStateProxy.SetVariable("ComputerName", CompnameInput.Text);

// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);

// add an extra command to transform the script output objects into nicely formatted strings
// remove this line to get the actual objects that the script returns. For example, the script
// "Get-Process" returns a collection of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");

// execute the script
Collection<PSObject> results = pipeline.Invoke();

// close the runspace
ps.Runspace.Close();

// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}

// return the results of the script that has
// now been converted to text
return stringBuilder.ToString();
}
// helper method that takes your script path, loads up the script
// into a variable, and passes the variable to the RunScript method
// that will then execute the contents
private string LoadScript(string filename)
{
try
{
// Create an instance of StreamReader to read from our file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filename))
{

// use a string builder to get all our lines from the file
StringBuilder fileContents = new StringBuilder();

// string to hold the current line
string curLine;

// loop through our file and read each line into our
// stringbuilder as we go along
while ((curLine = sr.ReadLine()) != null)
{
// read each line and MAKE SURE YOU ADD BACK THE
// LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
fileContents.Append(curLine + "\n");
}

// call RunScript and pass in our file contents
// converted to a string
return fileContents.ToString();
}
}
catch (Exception e)
{
// Let the user know what went wrong.
string errorText = "The file could not be read:";
errorText += e.Message + "\n";
return errorText;
}

}

private void Exitbutton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}

private void DeviceStatusButton_Click(object sender, RoutedEventArgs e)
{
// run our script and put the result into our textbox
// NOTE: make sure to change the path to the correct location of your script
Testlabel_Out.Content = RunScript(LoadScript(@"<script.ps1>"));
}
}
}

ps1-scriptet som körs ser kort och gott ut så här:
If(Test-Connection $computername -Quiet -Count 1){
$devicestatus = "Online"
}
Else{
$devicestatus = "Offline"
}
$devicestatus




Den använder variablen $ComputerName och skickar tillbaka värdet som det ska, problemet är att den inte kan köra med ett datornamn, utan måste köra med en IP. Sitter i en domän och det fungerar inte om jag skriver <datornamn>.<domän> heller.
Någon som vet/kan hjälpa mig?

Tackar ödmjukast!
Uppdatering skriven 5 September 2016, 23:11
Här är koden i Pastebin istället!
C#: http://pastebin.com/05sWZZSV
ps1: http://pastebin.com/xDNs4XAP

14 kommentarer — skriv kommentar

Kommentarerna nedan är skrivna av användare på Fragbite. Fragbite granskar inte sanningshalten i texten och du uppmanas att själv kritiskt granska och bemöta texten. Förutsätt inte att innehållet i texterna är sanning.
Visa 14 kommentarer

Skriv en kommentar

Laddar..