Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 回路不循环的电源外壳_Powershell_Loops_Foreach_Windows Firewall - Fatal编程技术网

Powershell 回路不循环的电源外壳

Powershell 回路不循环的电源外壳,powershell,loops,foreach,windows-firewall,Powershell,Loops,Foreach,Windows Firewall,因此,输出工作正常,但我有一个问题,它只输出它运行的最后一行。是否有检查循环以在将来进行测试的方法 但是我有一个ip地址列表,我正在检查windows中的防火墙是启用的还是禁用的。 他们在一个大的(300多个工作组)上。任何帮助使其正确循环都将不胜感激。安全性和其他方面都没有问题,因为我有其他运行良好的脚本。我没有任何错误。只有一个输出 我已经试过移动阵列了,但是没有用。我想它可能是PSCustomObject的一部分,因为我刚刚开始学习这些。或者可能是我的输入和输出格式不同导致了问题 clea

因此,输出工作正常,但我有一个问题,它只输出它运行的最后一行。是否有检查循环以在将来进行测试的方法

但是我有一个ip地址列表,我正在检查windows中的防火墙是启用的还是禁用的。 他们在一个大的(300多个工作组)上。任何帮助使其正确循环都将不胜感激。安全性和其他方面都没有问题,因为我有其他运行良好的脚本。我没有任何错误。只有一个输出

我已经试过移动阵列了,但是没有用。我想它可能是PSCustomObject的一部分,因为我刚刚开始学习这些。或者可能是我的输入和输出格式不同导致了问题

clear
$ComputerList = get-content C:\Users\Administrator\Desktop\DavidsScripts\TurnOffFirewall\input.txt
$Status = @(
foreach ($Computer in $ComputerList) {

netsh -r $Computer advfirewall show currentprofile state})[3] -replace 'State' -replace '\s' 


$Object = [PSCustomObject]@{
    Computer = $Computer
    Firewall = $Status
}

Write-Output $Object
$Object | Export-Csv -Path "C:\FirewallStatus.csv" -Append -NoTypeInformation

您以前的代码没有逃逸循环,只是将循环中的最后一台计算机添加到对象中

我发现的最好的方法是创建一个临时对象并将其添加到数组列表中,然后将其导出。好多了

$ComputerList = get-content C:\Users\Administrator\Desktop\DavidsScripts\TurnOffFirewall\input.txt
$collectionVariable = New-Object System.Collections.ArrayList

ForEach ($Computer in $ComputerList) {
    # Create temp object
    $temp = New-Object System.Object
    # Add members to temp object
    $temp | Add-Member -MemberType NoteProperty -Name "Computer" -Value $Computer
    $temp | Add-Member -MemberType NoteProperty -Name "Firewall" -Value $((netsh -r $Computer advfirewall show currentprofile state)[3] -replace 'State' -replace '\s')
    # Add the temp object to ArrayList
    $collectionVariable.Add($temp)
}

Write-Output $collectionVariable
$collectionVariable | Export-Csv -Path "C:\FirewallStatus.csv" -Append -NoTypeInformation

下面是一个简化的、功能性的代码版本,使用单个管道:

获取内容C:\Users\Administrator\Desktop\davidscripts\TurnOffFirewall\input.txt|
ForEach对象{
[pscustomobject]@{
计算机=$_
防火墙=(-split((netsh-r$\advfirewall显示当前配置文件状态)-匹配“^state”)[-1]
}
}|导出Csv-路径C:\FirewallStatus.Csv-notype信息
注:

  • 不需要中间变量;从输入文件中读取的每个计算机名都将逐个处理,并将基于其构造的每个自定义对象发送到输出CSV文件

  • 为了基于行内容(regex
    ^state
    ,即以
    state
    开头的行,而不是行索引(
    [3]
    )提取状态信息,从
    netsh
    的输出中提取防火墙状态的命令变得更加健壮;
    -split
    的一元形式通过空格将感兴趣的行拆分为标记,索引
    [-1]
    提取最后一个标记,即状态值


至于你所尝试的:

  • 您的
    foreach
    循环在构建
    $Object
    之前结束,因此您只构建了一个对象,并使用
    Export Csv
    发送到输出文件

  • 如果你已经正确地格式化了你的代码,那么这个事实会更加明显;尝试与一起使用,它通过
    >格式化文档
    (Shift+Alt+F)命令提供自动格式化


您的
$Status
包含
ForEach
,这意味着您将只获得最后一次运行
$Computer
。您的解决方案是有效的,但除非您需要PowerShell版本2兼容性,否则我建议使用
[pscustomobject]@{…}
用于创建自定义对象的语法-如问题中所用-可读性更强,性能更好。