更新:下面的答案能够让我得到我需要的地方。 Here is the full solution,如果有兴趣在后端看到与PowerShell的Angular / WebAPI接口。
我将一个mDatabase = FirebaseDatabase.getInstance().getReference();
DatabaseReference connectedRef = mDatabase.child(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
mDatabase.child(ReferenceToFirebase.CHILD_CATEGORIES)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//do something
//then dismiss dialog
mDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.err.println("Listener was cancelled");
mDialog.dismiss();
}
});
} else {
System.out.println("not connected");
mDialog.dismiss();
}
}
@Override
public void onCancelled(DatabaseError error) {
System.err.println("Listener was cancelled");
}
});
文件保存为C#类库中Scripts文件夹中的嵌入式资源。我想要做的是将此脚本传递到新的.ps1
类,如下所示:
System.Management.Automation.Runspaces.Command
每当我点击InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
using (Runspace rs = RunspaceFactory.CreateRunspace(iss))
{
rs.Open();
Command queryWmi = new Command("PowerShellAPIFramework.Core.Scripts.QueryWmi.ps1");
queryWmi.Parameters.Add("query", model.query);
queryWmi.Parameters.Add("properties", model.properties);
queryWmi.Parameters.Add("computername", model.computername);
queryWmi.Parameters.Add("wmiNamespace", model.wmiNamespace);
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = rs;
ps.Commands.AddCommand(queryWmi);
var results = ps.Invoke();
if (ps.HadErrors)
{
if (ps.Streams.Error.Count > 0)
{
foreach (var error in ps.Streams.Error)
{
Console.WriteLine(error.Exception.GetExceptionMessageChain());
}
}
}
else
{
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
}
}
}
ps.Invoke()
我运行了相同的代码,但是为我的硬盘驱动器上的文件指定了一个网址,例如
"The term 'PowerShellAPIFramework.Core.Scripts.QueryWmi.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
,它运行正常。
答案 0 :(得分:1)
在阅读PetSerAl的评论后,我更新了代码。使用Command(string命令,bool isScript)构造函数。
using (Stream st = new MemoryStream(Properties.Resources.yourResource))
{
using (StreamReader sr = new StreamReader(st))
{
string script = sr.ReadToEnd();
using (PowerShell ps = PowerShell.Create())
{
Command cmd = new Command(script, true);
//Add Parameters
ps.Commands.AddCommand(cmd);
ps.Invoke();
}
}
}