英文:
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 = 'hi'
$args
# nothing
$myargs = 'hi'
$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 ".\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/update/remove command execution
# All commands should be passed as variable
# $args = " Test-NetConnection " + $HostServer + " -Port " + $PortNumber + " -InformationLevel Quiet"
$InvokeCommand = "Invoke-Command "+ "-ComputerName "+ $SourceServer
$Expression= $InvokeCommand + " -Scriptblock { "+ $args +" } "
$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 ".\Output.csv" -NoTypeInformation #Store in CSV file
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论