简体   繁体   English

在PowerShell中传递PortQry参数

[英]Passing PortQry arguments in PowerShell

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. 我正在使用PowerShell自动执行服务器列表的PortQry请求(基于其角色),这似乎很简单。 But I cannot seem to pass the arguments correctly to PortQry and end up with the PortQry help screen. 但是我似乎无法正确地将参数传递给PortQry,并最终得到PortQry帮助屏幕。

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: 这是我的ForEach循环,用于循环访问由CSV创建和导入的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. 添加的信息:好像我没有从数组中正确提取$Server变量。 Here is my import: 这是我的导入:

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

My Servers.csv looks like: 我的Servers.csv看起来像:

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

Thoughts?? 想法? BTW I've tried this in powershell v2 & v3 顺便说一句,我已经在Powershell v2和v3中尝试过

Im sure this is an issue of the command line parameters being sent to PortQry not looking like you would expect them to be. 我确定这是发送到PortQry的命令行参数的问题,看起来不像您期望的那样。 I still don't know what your $Role1Ports looks like so that might be the source of your woes. 我仍然不知道您的$Role1Ports是什么样子,所以这可能是您$Role1Ports的根源。 Making an assumtion that you have it declared like this $Role1Ports = "80,443" 假设您已声明$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. 需要在"TCP"上加上引号,以便将其解释为字符串。 Nothing is wrong with your ForEach loop that i can see. 我可以看到您的ForEach循环没有问题。 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. 我在CheckPorts函数中包装了$z ,以便z$中的端口也应正确传递。 That was done as per the examples of PortQry i found online 这是根据我在网上找到的PortQry的示例完成的

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 = 80,443或采用某种形式的数组,则您可以执行上述任何一个操作,并且我上面使用的代码仍然可以使用。

$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 -ne "")语句,它仍然可以工作。 if ($Server) would also have worked. if ($Server)也可以。

My ForEach loop was wrong. 我的ForEach循环是错误的。 2 problems existed: first, the null check was not correct and the call for the column was not correct. 存在2个问题:首先,空检查不正确,并且对列的调用不正确。 This is the correct loop: 这是正确的循环:

ForEach ($Server in $ServerRoleArray) {
    if (!$Server.Role1) {
    break
    } else {
    CheckPorts $Server.Role1 "TCP" $Role1Ports
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM