New to power shell and Given Task to validate Server configuration

The follow script yields True for firewall when " IsFireON = [boolean]$value -eq '0'"  is set to 1 or 0.

#http://www.powershellpro.com/powershell-tutorial-introduction/powershell-scripting-with-wmi/


$serverList = ".\Servers.txt"
$outputCSV = ".\softwareInventory.csv"


$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath

pushd $dir


[System.Collections.ArrayList]$sysCollection = New-Object System.Collections.ArrayList($null)



foreach ($server in (Get-Content $serverList))

{
    "Collecting information from $server"
    $totCores    = 0
    $HKLM        = 2147483650 #HKEY_LOCAL_MACHINE 
    $key         = "System\ControlSet001\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile"
    $value       = "EnableFirewall"



    try
    #http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx
    {
        [string]$id       = [Security.Principal.WindowsIdentity]::GetCurrent()
        [wmi]$firewall    = Get-wmiObject -list -Namespace "root\default" -ComputerName $server |where-object { $_.name -eq "StdRegProv" }


        $si = @{
            Server                     = [string]$server 
            FireSUPPORT                = [string] $results = $firewall.GetDWORDValue( $HKLM, $key, $value)
            IsFireON                   = [boolean]$value -eq '0'

        }

        $disks | foreach-object {$si."Drive$($_.Name -replace ':', '')"="$([string]([System.Math]::Round($_.Size/1gb,2))) GB"}

    }
    catch [Exception]
    {
        "Error communicating with $server, skipping to next"
        $si = @{
            Server          = [string]$server
            ErrorMessage    = [string]$_.Exception.Message
            ErrorItem       = [string]$_.Exception.ItemName
     }
        Continue
    }
    finally
    {
       [void]$sysCollection.Add((New-Object PSObject -Property $si))   
    }
}

$sysCollection `
    | select-object Server,Dev_Admins,IsFireON,FireWallOn,NotePadInstalled,Name,ErrorMessage,ErrorItem `
    | sort -Property Server `
    | Export-CSV -path $outputCSV -NoTypeInformation   

                                   
September 2nd, 2015 4:15pm

This is closer to what you want:

Get-Content $serverList |
	ForEach-Object{
	    $server=$_
		Write-Host "Collecting information from $server" -fore green
			try{
				$reg=Get-wmiObject -list StdRegProv -Namespace root\default -ComputerName $server -ea Stop
				$results=$reg.GetDWORDValue(2147483650, 'System\ControlSet001\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile', 'EnableFirewall')
				$si = @{
					Server=$server
					IsFireON=[boolean]$results.uValue
					ErrorMessage=''
					ErrorItem=$null
				}
			} 
			Catch{
				Write-Host "Error communicating with $server, skipping to next" -fore red
				$si = @{
					Server=$server
					IsFireON=$null
					ErrorMessage="$_"
					ErrorItem=$_.Exception.ItemName
				}
			}
			[pscustomobject]$si	
	} Select Server, IsFireOn, ErrorMessage, ErrorItem

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 5:29pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics