quser(查询用户)没有结果powershell c#

时间:2017-03-23 09:30:05

标签: c# powershell

我几个星期以来一直在寻找这个问题的解决方案,但仍被封锁......

我需要在powershell中使用程序“quser”,但需要在我的C#程序中使用。

如果我在powershell控制台中运行“quser”,我会得到一个结果。 但是当我用“quser”执行一个powershell脚本时,它什么也没有返回......这很奇怪。

Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        PowerShell ps = PowerShell.Create();
        ps.Runspace = runspace;
        String script = "$re = '(\\S+)\\s+?(\\S*)\\s+?(\\d+)\\s+(\\S+)\\s+(\\S+)\\s+(\\d+\\/\\d+\\/\\d+\\s+\\d+:\\d+)'\n"
                            + "query user /server:"+machine+" | Where-Object { $_ -match $re } | ForEach-Object {\n"
                            + "New-Object -Type PSCustomObject -Property @{"
                            + "'Username' = $matches[1]\n"
                            + "'LogonTime' = $matches[6]\n"
                            + "}\n"
                            + "} | Select-Object Username, LogonTime | ForEach-Object { Write-Output (\"{0} is connected from {1}\" -f $_.Username, $_.LogonTime) }";
        ps.AddScript(script);
        Collection<PSObject> result = ps.Invoke();
        ps.Dispose();
        runspace.Close();
        runspace.Dispose();

        Console.WriteLine(result.Count);

ps.Invoke()的结果为空。 但是,当我从PowerShell控制台运行此代码时,它可以工作。

另外一个奇怪的事情,它适用于远程PowerShell:

String script = "$re = '(\\S+)\\s+?(\\S*)\\s+?(\\d+)\\s+(\\S+)\\s+(\\S+)\\s+(\\d+\\/\\d+\\/\\d+\\s+\\d+:\\d+)'\n"
                            + "query user | Where-Object { $_ -match $re } | ForEach-Object {\n"
                            + "New-Object -Type PSCustomObject -Property @{"
                            + "'Username' = $matches[1]\n"
                            + "'LogonTime' = $matches[6]\n"
                            + "}\n"
                            + "} | Select-Object Username, LogonTime | ForEach-Object { Write-Output (\"{0} is connected from {1}\" -f $_.Username, $_.LogonTime) }";
        string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
        System.Security.SecureString sString = new System.Security.SecureString();
        foreach (char passwordChar in password.ToCharArray())
        {
            sString.AppendChar(passwordChar);
        }
        PSCredential credential = new PSCredential(username, sString);
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, machine, 5985, "/wsman", shellUri, credential);
        List<String> scriptOutput = new List<String>();
        using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
        {
            runspace.Open();
            Pipeline pipe = runspace.CreatePipeline();
            pipe.Commands.AddScript(script);
            Collection<PSObject> result = pipe.Invoke();
            foreach (PSObject line in result)
            {
                scriptOutput.Add(line.ToString());
            }
            pipe.Dispose();
            runspace.Close();
        }
        foreach(String line in scriptOutput)
        {
            Console.WriteLine(line);
        }

返回已连接的用户。

事实是,我不想登录机器以查看连接的用户。 (因为它在powershell控制台中工作)

如果有人可以帮助我。

感谢。

0 个答案:

没有答案