停止包含示例的进程并将其停止

时间:2016-07-13 12:46:12

标签: powershell

以下脚本必须自动停止所有example*进程,而不是那些位于名为no_stop.txt的.txt文件中的进程:

    $no = get-content no_stop.txt;
$yes = get-process example* | Select-Object name >> text2.txt;

if($no)
{
for($i1=0; $i1 -le $no.length; $i1++)
{
    for($i2=0; $i2 -le $yes.length;$i2++)
    {
        if($no[$i1] -eq $yes[$i2])
        {
            $yes[$i2]='';

        }
        if($yes[$i2] -eq 'Name' >> text2.txt)
        {
            $yes[$i2]='';
        }
        if($yes[$i2] -eq '-' >> text2.txt)
        {
            $yes[$i2]='';
        }
    }
}
}

for($i1=0; $i1 -le $yes.length;$i1++)
{
if($yes[$i1])
{
        $yes[$i1] = $yes[$i1] -replace '\s','' >>test.txt;
        Stop-Process $yes[$i1] -Force >>test.txt;
}
}
$yes >> test.txt;

不知怎的,这不起作用,你能解释一下为什么吗?

2 个答案:

答案 0 :(得分:1)

你可以简化这个:

BankAccount

答案 1 :(得分:0)

$no = @(Get-Content C:\File\Path\no_start.txt) -join '|' Get-Service example* | ?{$_.Name -notmatch $no} | Start-Service -PassThru

这也很好,它可以读取多行。

相关问题