【问题标题】:powershell: commands list run ok in CLI, but not in script?powershell:命令列表在 CLI 中运行正常,但在脚本中运行不正常?
【发布时间】:2020-11-24 13:28:09
【问题描述】:

我编写了一个小脚本来启动 ping 测试 + 抓取 Wifi 网络信息并将其导出为 CSV 文件。

在 powershell 控制台中使用 copy-past 启动这些命令时效果很好 如果我将这些命令保存在 wifi.ps1 脚本文件中并启动此脚本,它不会更新任何值

如果我删除了用于永久测试的 while(1) 注释,它总是将相同的数据复制到 CSV 中

有什么想法吗?

谢谢

    $RegEx=@'
    (?x)
    SSID\s\d+\s:\s(?<SSID>[a-z0-9\-\*\.&_]+)\r\n
    Network\stype\s+:\s(?<NetworkType>\w+)\r\n
    Authentication\s+:\s(?<Authentication>[a-z0-9\-_]+)\r\n
    Encryption\s+:\s(?<Encryption>\w+)\r\n
    BSSID\s1\s+:\s(?<BSSID>(?:\w+:){5}\w+)\r\n
    Signal\s+:\s(?<Signal>\d{1,2})%\r\n
    Radio\stype\s+:\s(?<Radio>[a-z0-9\.]+)\r\n
    Channel\s+:\s(?<Channel>\w+)\r\n
'@ 

$destination = "192.168.0.1"
$delay = 1  

#while (1) {

$test = (Test-Connection -ComputerName $destination -Count 1  | measure-Object -Property ResponseTime -Average).average 
$response = ($test -as [int] ) 
$WiFi = (netsh wlan show  network mode=bssid  | Select-Object  ).Trim() | Out-String 
$Networks  = $WiFi  -split "\r\s+\n" #remove empty line
$Networks  | ForEach {
    If  ($_ -match  $RegEx) {
        $result= [pscustomobject]@{
            TimeStamp = (Get-Date -Format "dd-MM-yyyy HH:mm:ss")
            SSID =  $Matches.SSID
            BSSID1 =  $Matches.BSSID
            SignalPercentage = [int]$Matches.Signal
            RadioType = $Matches.Radio
            Channel = $Matches.Channel
            destination = $destination
            ping = $response 
            }
    } 
}
$result | Export-Csv -Path C:\tmp\output.csv -Append

#}
#Start-sleep -seconds $delay

【问题讨论】:

  • $Result =应该在循环外:$result = $Networks | ForEach { If ($_ -match $RegEx) { [pscustomobject]@{ ...,更好的是防止管道卡住,直接将每个项目通过管道导入csv文件:$Networks | ForEach { ... } | Export-Csv -Path C:\tmp\output.csv -Append
  • 如果我在新的 powershell 中启动脚本 wifi.ps1,我有一个错误: PS C:\tmp> .\script.ps1 Export-Csv : Cannot bind argument to parameter 'InputObject'因为它是空的。在 C:\tmp\wifi1.ps1:36 char:11 + $result | Export-Csv -Path C:\tmp\output.csv -Append + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo mmand
  • @iRon 它工作得更好,但只有当我在命令行 powershell 中复制脚本时。如果通过 script.PS1 文件启动,它不起作用

标签: powershell


【解决方案1】:

这是我在 powershell CLI 中复制过去时有效的更新版本,但不是通过 script.PS1。

然后通过使用 while(1),我希望 CSV 文件每秒更新一次

有什么想法吗?

$destination = "192.168.0.1"
$delay = 1
#####Don't change below#####
$RegEx=@'
    (?x)
    SSID\s\d+\s:\s(?<SSID>[a-z0-9\-\*\.&_]+)\r\n
    Network\stype\s+:\s(?<NetworkType>\w+)\r\n
    Authentication\s+:\s(?<Authentication>[a-z0-9\-_]+)\r\n
    Encryption\s+:\s(?<Encryption>\w+)\r\n
    BSSID\s1\s+:\s(?<BSSID>(?:\w+:){5}\w+)\r\n
    Signal\s+:\s(?<Signal>\d{1,2})%\r\n
    Radio\stype\s+:\s(?<Radio>[a-z0-9\.]+)\r\n
    Channel\s+:\s(?<Channel>\w+)\r\n
'@ 
#while (1) {

$test = (Test-Connection -ComputerName $destination -Count 1  | measure-Object -Property ResponseTime -Average).average 
$ping_TTL = ($test -as [int] ) 
$WiFi = (netsh wlan show  network mode=bssid  | Select-Object  ).Trim() | Out-String 
$Networks  = $WiFi  -split "\r\s+\n" #remove empty line
$Networks  | ForEach {
    If  ($_ -match  $RegEx) {
            [pscustomobject]@{
            TimeStamp = (Get-Date -Format "dd-MM-yyyy HH:mm:ss")
            SSID =  $Matches.SSID
            BSSID1 =  $Matches.BSSID
            SignalPercentage = [int]$Matches.Signal
            RadioType = $Matches.Radio
            Channel = $Matches.Channel
            destination = $destination
            ping = $ping_TTL 
            }
    } 
} | Export-Csv -Path C:\tmp\output.csv -Append


#Start-sleep -seconds $delay

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2022-01-01
    • 2022-08-19
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多