小编Bax*_*orr的帖子

当命令运行时,如何从 C# 中的 powershell 获取输出?

我在 C# 中使用 powershell 和 system.management.automation,并且可以成功访问输出和错误流。对于大多数应用程序来说,这很好,但我现在的情况是,我需要在 c# 中运行 powershell 命令时获取它的输出,但我迷失了。

我尝试订阅outputcollection.DataAdded,我尝试订阅powershell实例详细流,但是当powershell给出输出时,它们都没有被调用。

这是我到目前为止的代码

public async Task<string> CMD(string script)
{
    ps = PowerShell.Create();
    string errorMsg = "";
    string output;

    ps.AddScript(script);
    ps.AddCommand("Out-String");

    PSDataCollection<PSObject> outputCollection = new();
    ps.Streams.Error.DataAdded += (object sender, DataAddedEventArgs e) =>
    { errorMsg = ((PSDataCollection<ErrorRecord>)sender)[e.Index].ToString(); };

    IAsyncResult result = ps.BeginInvoke<PSObject, PSObject>(null, outputCollection);

    while (!result.IsCompleted)
    {
        await Task.Delay(100);
    }

    StringBuilder stringBuilder = new();
    foreach (PSObject outputItem in outputCollection)
    {
        stringBuilder.AppendLine(outputItem.BaseObject.ToString());
    }
    output = stringBuilder.ToString();

    //Clears commands added to runspace
    ps.Commands.Clear();
    Debug.WriteLine(output);

    if …
Run Code Online (Sandbox Code Playgroud)

c# powershell automation

5
推荐指数
1
解决办法
3710
查看次数

如何使用在 kivy 中作为按钮按下时会播放的动画 gif?

我在 kivy 中有这个切换按钮,我希望它在按下时动画(这是一个开机按钮 gif)但没有循环。我似乎找不到任何关于此的有用信息。任何帮助表示赞赏,谢谢!

python animated-gif kivy kivy-language

4
推荐指数
1
解决办法
3903
查看次数

如何从猕猴桃的画布上删除线条

我正在创建一个简单的绘图应用程序,并在其中添加一个撤消按钮。到目前为止,我尝试过的是:

class DrawScreen(Screen):
    r = NumericProperty(0)
    g = NumericProperty(0)
    b = NumericProperty(0)
    brush_width = NumericProperty(2)

    def on_touch_down(self, touch):
        self.slider = self.ids.slider
        if self.slider.collide_point(touch.x, touch.y):
            self.brush_width = self.slider.value
        else:
            self.undo = [touch.x, touch.y]
            with self.canvas.before:
                Color(self.r, self.g, self.b)
                touch.ud["line"] = Line(points=self.undo, width=self.brush_width)
        return super(DrawScreen, self).on_touch_down(touch)

    def on_touch_move(self, touch):
        if self.slider.collide_point(touch.x, touch.y):
            self.brush_width = self.slider.value
        else:
            try:
                self.undo += [touch.x, touch.y]
                touch.ud["line"].points = self.undo

            except:
                pass
        return super(DrawScreen, self).on_touch_move(touch)

    def color(self, r, g, b):
        self.r = r
        self.g = g
        self.b = …
Run Code Online (Sandbox Code Playgroud)

python kivy kivy-language

3
推荐指数
1
解决办法
1501
查看次数

有没有办法知道一个号码是否包含在另一个号码中?

我正在制作一个简单的程序,我需要知道一个数字是否是用户输入的数字的一部分.比如说用户选择了数字3,我需要程序在另一个数字是13,23,33等时执行操作.如果用户选择了数字11,则需要在另一个数字时执行操作是11,21,31,41等.

python python-3.x

0
推荐指数
1
解决办法
72
查看次数

从 C# 调用图形 API 时出现不可接受的错误

嘿伙计们,我正在尝试获取租户中所有房间的列表,但我收到一个错误,我看不到解决方案。我的图形服务客户端似乎是正确的,因为我可以毫无问题地获取所有用户的列表,但获取所有房间失败并出现未知错误。

我想要复制的内容:https://learn.microsoft.com/en-us/graph/api/place-list ?view=graph-rest-1.0&tabs=csharp#request

创建图形客户端并尝试从地方获取房间列表

public GraphServiceClient CreateGraphClient()
{
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };

    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);

    return new GraphServiceClient(clientSecretCredential, scopes);
}

public async Task<List<string>> GetRoomsAsync()
{
    var rooms = await graphClient.Places.Request().GetAsync();

    var roomsList = new List<string>();

    foreach (var room in rooms)
    {
        Console.WriteLine(room.DisplayName);
        roomsList.Add(room.DisplayName);
    }
    return roomsList;
}
Run Code Online (Sandbox Code Playgroud)

当试图获取用户时,同样的事情也会发生。

    public async Task<List<string>> GetUsersAsync()
    {
        var users = await graphClient.Users.Request().GetAsync(); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core microsoft-graph-sdks microsoft-graph-api

-1
推荐指数
1
解决办法
619
查看次数