0

我正在尝试找出网络上的 Windows 防火墙是否已启用/禁用,以扫描网络并让我知道防火墙服务是否已启用/禁用。

这是我正在使用的脚本,请记住我是入门级的:

function Get-FWState
{
    param
    (
        [int]$state
    )

    switch ($state)
    {
        0 { return "Off" }
        1 { return "On" }
    }
}

if (-not (Get-Module ActiveDirectory))
{
    Import-Module ActiveDirectory
}

$computers = Get-ADComputer -Filter *

ForEach ($computer in $computers)
{
    try 
    {
        $everythingOK = $True

        $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer.Name)

        $domkey = $reg.OpenSubkey("SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\DomainProfile")
        $domvalue = $domkey.GetValue("EnableFirewall")

    } 
    catch 
    {
        $everythingOK = $false
        $_
    }

    if ($everythingOK)
    {
        $stdkey = $reg.OpenSubkey("SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile")
        $stdvalue = $stdkey.GetValue("EnableFirewall")

        $pubkey = $reg.OpenSubkey("SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\PublicProfile")
        $pubvalue = $pubkey.GetValue("EnableFirewall")
    }

    $obj = New-Object -TypeName PSObject
    $obj | Add-Member -MemberType NoteProperty -Name 'HostName' -Value $computer.Name
    $obj | Add-Member -MemberType NoteProperty -Name 'DomainProfile' -Value $(Get-FWState -state $domvalue)
    $obj | Add-Member -MemberType NoteProperty -Name 'StandardProfile' -Value $(Get-FWState -state $stdvalue)
    $obj | Add-Member -MemberType NoteProperty -Name 'PublicProfile' -Value $(Get-FWState -state $pubvalue)

    Write-Output $obj
}

当我运行此脚本时,我收到以下错误消息。

You cannot call a method on a null-valued expression.
At C:\Scripts\Firewall.ps1:46 char:37
+         $pubvalue = $pubkey.GetValue <<<< ("EnableFirewall")
    + CategoryInfo          : InvalidOperation: (GetValue:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Scripts\Firewall.ps1:46 char:37
+         $pubvalue = $pubkey.GetValue <<<< ("EnableFirewall")
    + CategoryInfo          : InvalidOperation: (GetValue:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
4

1 回答 1

0

它说问题是在空表达式上调用该方法。你确定 "$pubkey" 不为空,因此,

$pubkey = $reg.OpenSubkey("SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\PublicProfile")

在第 45 行返回一个值?

于 2013-03-22T15:26:25.683 回答