Skip to content

Commit

Permalink
Merge pull request #167 from PowerShell/export_scenario
Browse files Browse the repository at this point in the history
ProcessList native resource
  • Loading branch information
SteveL-MSFT authored Aug 24, 2023
2 parents 3f07ab4 + 727d3f2 commit 5a1f8b5
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
4 changes: 2 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ if (Test-Path $target) {
New-Item -ItemType Directory $target > $null

$windows_projects = @("pal", "ntreg", "ntstatuserror", "ntuserinfo", "registry")
$projects = @("dsc_lib", "dsc", "osinfo", "test_group_resource", "y2j", "powershellgroup")
$pedantic_clean_projects = @("dsc_lib", "dsc", "osinfo", "y2j", "pal", "ntstatuserror", "ntuserinfo", "test_group_resource", "sshdconfig")
$projects = @("dsc_lib", "dsc", "osinfo", "process", "test_group_resource", "y2j", "powershellgroup")
$pedantic_clean_projects = @("dsc_lib", "dsc", "osinfo", "process", "y2j", "pal", "ntstatuserror", "ntuserinfo", "test_group_resource", "sshdconfig")

if ($IsWindows) {
$projects += $windows_projects
Expand Down
9 changes: 9 additions & 0 deletions process/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "process"
version = "0.1.0"
edition = "2021"

[dependencies]
sysinfo = "*"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
21 changes: 21 additions & 0 deletions process/process.dsc.resource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"manifestVersion": "1.0",
"description": "Returns information about running processes.",
"type": "Microsoft/ProcessList",
"version": "0.1.0",
"get": {
"executable": "process",
"args": [
"get"
]
},
"set": {
"executable": "process",
"args": [
"set"
],
"input": "stdin",
"preTest": true,
"return": "state"
}
}
51 changes: 51 additions & 0 deletions process/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

mod process_info;
use std::env;
use std::process::exit;
use sysinfo::{ProcessExt, System, SystemExt, PidExt};

fn print_task_list() {

let mut s = System::new();
s.refresh_processes();
for (pid, process) in s.processes() {
let mut p = process_info::ProcessInfo::new();
p.pid = pid.as_u32();
p.name = String::from(process.name());
p.cmdline = format!("{:?}", process.cmd());

let json = serde_json::to_string(&p).unwrap();
println!("{json}");
}
}

fn help() {
println!("usage: process list");
}

fn main() {
let args: Vec<String> = env::args().collect();
if args.len() == 2 {
// one argument passed
match args[1].as_str() {
"list" => {
print_task_list();
exit(0);
},
"set" => { // used for testing only
println!("{{\"result\":\"Ok\"}}");
exit(0);
},
_ => {
help();
exit(1);
},
}
}
else {
help();
exit(1);
}
}
22 changes: 22 additions & 0 deletions process/src/process_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use serde::Serialize;

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ProcessInfo {
pub pid: u32,
pub name: String,
pub cmdline: String
}

impl ProcessInfo {
pub fn new() -> Self {

Self {
pid: 0,
name: String::new(),
cmdline: String::new(),
}
}
}

0 comments on commit 5a1f8b5

Please sign in to comment.