0

I'm trying to verify the correct firewall rules have been implemented and flows are functional. I've searched and can't seem to find my solution. I'm using PowerShell to automate PortQry requests (based on it's role) for list of servers which seems simple. But I cannot seem to pass the arguments correctly to PortQry and end up with the PortQry help screen.

Here is my function:

  Function CheckPorts($x,$y,$z) {
    PortQry -n $x -p $y -o $z | Out-File $ResultsFile -Append
    }

And here is my ForEach loop that I'm using to iterate through an array created by and imported CSV:

  $Type = TCP      
   Foreach ($Server in $ServerRoleArray.Role1) {
            if ($Server -ne "") {
            CheckPorts $Server $Type $Role1Ports
        }

Added information: It appears as though I'm not extracting $Server variable correctly from the array. Here is my import:

$ServerRoleArray = Import-CSV $FilePath\Servers.csv

My Servers.csv looks like:

Role1,Role2
google.com,msn.com
yahoo.com,

Thoughts?? BTW I've tried this in powershell v2 & v3

npocmaka
  • 55,367
  • 18
  • 148
  • 187
Joe
  • 1
  • 3
  • Step 1 would be strongly-typing your input parameters. One of them could be an object. – JNK Sep 12 '14 at 20:49
  • Before it shows the help screen, is there an error listed? – Norman Skinner Sep 12 '14 at 20:52
  • Are you trying to pass an array to -o, or are you passing a string of comma separated values? – TheMadTechnician Sep 12 '14 at 20:56
  • I'm not getting an error. The results log simply shows the same information as when you type a PortQry wrong or help. I am passing a string of comma separated values using " " to -o. – Joe Sep 12 '14 at 22:53
  • What is the contents of `$ports` or `$role1ports`? Code seems sound but if you are getting the help menu for output you are getting a formatting issue on your command line it seems. – Matt Sep 12 '14 at 23:33

2 Answers2

0

Im sure this is an issue of the command line parameters being sent to PortQry not looking like you would expect them to be. I still don't know what your $Role1Ports looks like so that might be the source of your woes. Making an assumtion that you have it declared like this $Role1Ports = "80,443"

Function CheckPorts($x,$y,$z) {
    PortQry -n $x -p $y -o `"$z`" | Out-File $ResultsFile -Append
}

$ServerRoleArray = Import-Csv C:\temp\server.tkt.txt
$Role1Ports = "80,443"

$Type = "TCP"      
Foreach ($Server in $ServerRoleArray.Role1) {
    if ($Server -ne "") {
    CheckPorts $Server $Type $Role1Ports
    }
}

Needed to puts quotes on "TCP" so that it would be interpreted as a string. Nothing is wrong with your ForEach loop that i can see. I used your test example data and it processed fine. I wrapped up $z in the CheckPorts function so that the ports in z$ should be passed properly as well. That was done as per the examples of PortQry i found online

If your $Role1Ports looked like this instead: $Role1Ports = 80,443 or was in some form of array then you could do either of these and the code I used above would work still.

$Role1Ports = $Role1Ports -join ","

or

$Role1Ports = 80,443 -join ","

The command output that you should get is something like this.

PortQry -n google.com -p TCP -o "80,443"
PortQry -n yahoo.com -p TCP -o "80,443"

Because of other answer

I dont know why you had issues with you logic since it was working fine. Based on your comments i used a different text file.

Role1,Role2
,msn.com
google.com,
yahoo.com,

Even with the the if ($Server -ne "") statement it still worked. if ($Server) would also have worked.

Matt
  • 45,022
  • 8
  • 78
  • 119
  • Thanks for responding. I have tried the `$Role1Ports = "80,443"` as well as `[string]$Role1Ports = 80,443`. I continue to get errors, even after copying and pasting your code. So, just for giggles I changed my function script to `Write-Host "PortQry -n $x -p $y -o $z"` and my suspicion of PortQry not receiving the hostname correctly was confirmed. Output was `PortQry -n -p TCP -o 80,443` – Joe Sep 13 '14 at 20:51
  • Is your test data and different than your example? It was working fine as per my command output. It is also odd since you filter empty ones out. – Matt Sep 13 '14 at 20:56
  • That's why I'm confused. My test data was created in excel and saved as a CSV (not a text file from notepad). What I pasted in was the result of right-click>edit opened in notepad. – Joe Sep 13 '14 at 23:55
  • So are you still having a problem then? – Matt Sep 14 '14 at 00:51
  • I do, and it's related to pulling the data from my array. I created a flat array using `$ServerRoleArray = @(google.com,yahoo.com)` and everything works fine. My current test environment is PS v2 w/ PortQry v2. – Joe Sep 14 '14 at 02:26
  • If you just run `Import-CSV $FilePath\Servers.csv` do you get proper output? It sounds like you are getting nulls or empty strings – Matt Sep 14 '14 at 02:28
  • Yes. The array looks fine. `@{Role1=; Role2=msn.com} @{Role1=yahoo.com; Role2=} @{Role1=google.com; Role2=}` – Joe Sep 14 '14 at 02:36
  • Out of curiosity why did you respond with 3 individual arrays? I would have expected a response like "I got a table of data" or "It looks right" or something like that – Matt Sep 14 '14 at 03:20
  • The array shows 2 column headers with the appropriate data under that column for `$ServerRoleArray`. What was posted above was a result of `Write-Host $ServerRoleArray`. Sorry for the confusion. – Joe Sep 14 '14 at 13:53
0

My ForEach loop was wrong. 2 problems existed: first, the null check was not correct and the call for the column was not correct. This is the correct loop:

ForEach ($Server in $ServerRoleArray) {
    if (!$Server.Role1) {
    break
    } else {
    CheckPorts $Server.Role1 "TCP" $Role1Ports
    }
}
Joe
  • 1
  • 3
  • Your `If` would work as just `if ($Server.Role1)` instead of being so negative. – Matt Sep 15 '14 at 13:39
  • That is how I'm checking for null since my other method was not working. It was passing null to the function. – Joe Sep 16 '14 at 01:52