我試圖找出是否在網絡上啓用/禁用Windows防火牆,以掃描網絡並讓我知道防火牆服務是否啓用/禁用。Powershell腳本錯誤
這裏是我使用的腳本,記住我大時間入門級:
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
您是否有足夠的權利訪問這些機器上的註冊表? – Deruijter 2013-03-22 15:26:44