测试使用Invoke命令的网络连接。

huangapple go评论64阅读模式
英文:

Test-Netconneciton using Invoke command

问题

我正在尝试从笔记本电脑上调用test-Net连接命令,用于超过1000台服务器。我正在使用以下代码进行执行。但是结果总是显示为TRUE,这是不正确的,如果我单独运行命令,那么一些结果也是FALSE。

Test-NetConnection $args[1] -InformationLevel Quiet
$MyObjectArray = Import-Csv ".\Sources.csv";

$AllObjects = @()

for ($i = 0; $i -lt $MyObjectArray.count; $i++)
{
    $SourceServer = $MyObjectArray[$i].Source
    $HostServer = $MyObjectArray[$i].Host
    $PortNumber = $MyObjectArray[$i].Port

    $args = "Test-NetConnection " + $HostServer + " -Port " + $PortNumber

    $result = Invoke-Command -FilePath .\myTNC.ps1 -ArgumentList $args -ComputerName $SourceServer

    $AllObjects += [pscustomobject]@{
        Source = $SourceServer
        Dest = $HostServer
        Port = $PortNumber
        Result = $result
    }
}

$AllObjects | Export-Csv -Path ".\TestResult.csv" -NoTypeInformation

预期输出:
Source, Dest, Port, Result
AAA, BBB, 8080, TRUE
AAA, CCC, 5678, TRUE
BBB, AAA, 7878, FALSE


<details>
<summary>英文:</summary>

I am trying to invoke test-Net connection command from laptop for more than 1000 servers. I am using below code for execution. However the result it showing is always TRUE which is not correct , if I ran individual command the several result is FALSE as well. 


Test-NetConnection $args[1] -InformationLevel Quiet


$MyObjectArray = Import-Csv ".\Sources.csv"

#################################################################

Define a new empty Array which will be used to print the data

#################################################################

$AllObjects = @()

#################################################################

Run a for loop to read ,and execute command for every row

#################################################################

for ($i = 0; $i -lt $MyObjectArray.count ; $i++)
{
$SourceServer= $MyObjectArray[$i].Source
$HostServer = $MyObjectArray[$i].Host
$PortNumber = $MyObjectArray[$i].Port

#############################################################################################

Change and modify the $args and $result variable to add/upate/remove command execution

#############################################################################################

#$args = "-computer " + $HostServer + " -Port " + $PortNumber #+ " -InformationLevel Quiet"

$args = "Test-NetConnection " + $HostServer + " -Port " + $PortNumber #+ " -InformationLevel Quiet"
#$result = Invoke-Command -FilePath .\myTNC.ps1 -ArgumentList $args -ComputerName $SourceServer

#################################################################

Define Field name which you need to print in CSV file

#################################################################

$AllObjects += [pscustomobject]@{
#$result =Invoke-command -ComputerName $SourceServer -ScriptBlock {Test-NetConnection $args -InformationLevel detailed}
$result = Invoke-Command -FilePath .\myTNC.ps1 -ArgumentList $args -ComputerName $SourceServer

Invoke-command -ComputerName INDELVR1077 -ScriptBlock {Test-NetConnection INDELVS200 -Port 49667 -InformationLevel Quiet}

   Source = $SourceServer
   Host = $HostServer
   Port = $PortNumber
   Status =$result

}}$AllObjects | Export-Csv -Path ".\TestResult.csv" -NoTypeInformation #Store in CSV file


Expected output:
Source, Dest, Port, Result
AAA,BBB,8080,TRUE
AAA,CCC, 5678, TRUE
BBB,AAA.7878,FALSE






</details>


# 答案1
**得分**: 1

$args 被保留,无法使用。通常我会将其命名为 $myargs。严格模式似乎不会捕获它。

```php
$args = 'hi';
$args

# 什么都没有

$myargs = 'hi';
$myargs

hi
英文:

$args is reserved and won't work. I usually make it $myargs. It doesn't look like strictmode catches it.

$args = &#39;hi&#39;
$args

# nothing


$myargs = &#39;hi&#39;
$myargs

hi

答案2

得分: 0

# 将 CSV 数据存储在临时数组中
$MyObjectArray = Import-Csv ".\Sources.csv"

# 定义一个新的空数组,用于打印数据
$AllObjects = @()

# 运行循环以读取并执行每一行的命令
for ($i = 0; $i -lt $MyObjectArray.count ; $i++) {
    $SourceServer = $MyObjectArray[$i].Source
    $HostServer = $MyObjectArray[$i].Host
    $PortNumber = $MyObjectArray[$i].Port

    # 更改和修改 $args 和 $result 变量以添加/更新/移除命令执行
    # 所有命令都应作为变量传递
    # $args = " Test-NetConnection " + $HostServer + " -Port " + $PortNumber + " -InformationLevel Quiet"

    $InvokeCommand = "Invoke-Command " + "-ComputerName " + $SourceServer

    $Expression = $InvokeCommand + " -Scriptblock { " + $args + " } "

    $Result = Invoke-Expression $Expression

    # 定义要在 CSV 文件中打印的字段名
    $AllObjects += [pscustomobject]@{
        Source = $SourceServer
        Host = $HostServer
        Port = $PortNumber
        Status = $Result
    }
}

$AllObjects | Export-Csv -Path ".\Output.csv" -NoTypeInformation # 存储到 CSV 文件
英文:
# Store the CSV data in temporary Array
$MyObjectArray = Import-Csv &quot;.\Sources.csv&quot;

# Define a new empty Array which will be used to print the data
$AllObjects = @()

# Run a for loop to read and execute command for every row
for ($i = 0; $i -lt $MyObjectArray.count ; $i++) { $SourceServer= $MyObjectArray[$i].Source $HostServer = $MyObjectArray[$i].Host $PortNumber = $MyObjectArray[$i].Port

# Change and modify the $args and $result variable to add/update/remove command execution
# All commands should be passed as variable
# $args = &quot; Test-NetConnection &quot; + $HostServer + &quot; -Port &quot; + $PortNumber + &quot; -InformationLevel Quiet&quot;

$InvokeCommand = &quot;Invoke-Command &quot;+ &quot;-ComputerName &quot;+ $SourceServer

$Expression= $InvokeCommand + &quot; -Scriptblock { &quot;+ $args +&quot; } &quot;

$Result = Invoke-Expression $Expression

# Define Field name which you need to print in CSV file

$AllObjects += [pscustomobject]@{ Source = $SourceServer Host = $HostServer Port = $PortNumber Status =$Result

}}$AllObjects | Export-Csv -Path &quot;.\Output.csv&quot; -NoTypeInformation #Store in CSV file

huangapple
  • 本文由 发表于 2023年3月15日 19:11:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定