Вы должны войти, чтобы создавать сообщения и темы.

Парсинг PowerShell

Привет 🖐
Пытаюсь в проекте использовать powershell, но не получается использовать команду с переменными. Подскажите, как это можно осуществить.

Код который работает в powershell:

$adapterMemory = (Get-ItemProperty -Path "HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*" -Name "HardwareInformation.qwMemorySize" -Exclude PSPath -ErrorAction SilentlyContinue)
foreach ($adapter in $adapterMemory) {
[PSCustomObject] @{
" "=[math]::round($adapter."HardwareInformation.qwMemorySize"/1GB)}
}

Как это выглядит в проекте:

Process process = Process.Start(new ProcessStartInfo
{
FileName = "powershell",
Arguments = @"/command $adapterMemory = (Get-ItemProperty -Path /""HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*/"" -Name
/""HardwareInformation.qwMemorySize/"" -Exclude PSPath -ErrorAction SilentlyContinue)
foreach ($adapter in $adapterMemory) {[PSCustomObject] @{' '=[math]::round($adapter./""HardwareInformation.qwMemorySize/""/1GB)}}",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
});
label23.Text = process.StandardOutput.ReadToEnd();

Цитата: N от 28.02.2024, 14:59

Привет 🖐
Пытаюсь в проекте использовать powershell, но не получается использовать команду с переменными. Подскажите, как это можно осуществить.

Код который работает в powershell:

 
 
 
 
$adapterMemory = (Get-ItemProperty -Path "HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*" -Name "HardwareInformation.qwMemorySize" -Exclude PSPath -ErrorAction SilentlyContinue)
foreach ($adapter in $adapterMemory) {
[PSCustomObject] @{
" "=[math]::round($adapter."HardwareInformation.qwMemorySize"/1GB)}
}
$adapterMemory = (Get-ItemProperty -Path "HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*" -Name "HardwareInformation.qwMemorySize" -Exclude PSPath -ErrorAction SilentlyContinue) foreach ($adapter in $adapterMemory) { [PSCustomObject] @{ " "=[math]::round($adapter."HardwareInformation.qwMemorySize"/1GB)} }
 
$adapterMemory = (Get-ItemProperty -Path "HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*" -Name "HardwareInformation.qwMemorySize" -Exclude PSPath -ErrorAction SilentlyContinue)
foreach ($adapter in $adapterMemory) {
[PSCustomObject] @{
" "=[math]::round($adapter."HardwareInformation.qwMemorySize"/1GB)}
}

Как это выглядит в проекте:

 
 
 
 
Process process = Process.Start(new ProcessStartInfo
{
FileName = "powershell",
Arguments = @"/command $adapterMemory = (Get-ItemProperty -Path /""HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*/"" -Name
/""HardwareInformation.qwMemorySize/"" -Exclude PSPath -ErrorAction SilentlyContinue)
foreach ($adapter in $adapterMemory) {[PSCustomObject] @{' '=[math]::round($adapter./""HardwareInformation.qwMemorySize/""/1GB)}}",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
});
label23.Text = process.StandardOutput.ReadToEnd();
Process process = Process.Start(new ProcessStartInfo { FileName = "powershell", Arguments = @"/command $adapterMemory = (Get-ItemProperty -Path /""HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*/"" -Name /""HardwareInformation.qwMemorySize/"" -Exclude PSPath -ErrorAction SilentlyContinue) foreach ($adapter in $adapterMemory) {[PSCustomObject] @{' '=[math]::round($adapter./""HardwareInformation.qwMemorySize/""/1GB)}}", UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true }); label23.Text = process.StandardOutput.ReadToEnd();
 
Process process = Process.Start(new ProcessStartInfo
{
FileName = "powershell",
Arguments = @"/command $adapterMemory = (Get-ItemProperty -Path /""HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*/"" -Name
/""HardwareInformation.qwMemorySize/"" -Exclude PSPath -ErrorAction SilentlyContinue)
foreach ($adapter in $adapterMemory) {[PSCustomObject] @{' '=[math]::round($adapter./""HardwareInformation.qwMemorySize/""/1GB)}}",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
});
label23.Text = process.StandardOutput.ReadToEnd();

Привет, вот попробуй этот код

 

Process process = Process.Start(new ProcessStartInfo
{
    FileName = "powershell",
    Arguments = "-Command \"$adapterMemory = Get-ItemProperty -Path 'HKLM:\\SYSTEM\\ControlSet001\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}\\0*' -Name 'HardwareInformation.qwMemorySize' -Exclude PSPath -ErrorAction SilentlyContinue; foreach ($adapter in $adapterMemory) { [PSCustomObject] @{'' = [math]::round($adapter.'HardwareInformation.qwMemorySize' / 1GB)} }\"",
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardOutput = true
});
label23.Text = process.StandardOutput.ReadToEnd();