首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在脚本上使用ping命令?

问如何在脚本上使用ping命令?
EN

Stack Overflow用户
提问于 2017-11-02 12:33:54
回答 1查看 1.7K关注 0票数 0

我经常使用PowerShell进行自动批处理修补。我通常需要获得一个机器列表,ping测试,获取该文件的输出并将其放入PowerShell,移动文件,然后使用PSEXEC执行bat文件。(我不能使用PowerShell远程执行,WinRM在我的世界中被阻塞)。

我找到了一个很好的脚本,用于从pings组中移除用户,如果没有响应,请继续,但如果有,则执行命令并移到下一个。我正在尝试将它用于我自己的用途,使之适合ping、移动文件,如果没有响应,请继续前进。看起来,到现在为止,脚本正在点击、移动,如果没有响应,它就会错误地使用“没有找到的网络路径”。

我的PS脚本如下:

代码语言:javascript
复制
#Install Cisco AnyConnect Core VPN and ISE Posture Installation
#Author: Erik R. Little
#Creation Date: 1 November 2017

#This script will do the following actions:
#1.- Get a computer list from a TXT file.
#2.- Do a ping to every computer on the list, if the computer is offline it will skip it and pass to the next one.
#3.- If the computer answers the ping it will push the AnyConnect installation files to the remote machine.
#4.- Creates a log with all the transactions.

        # Log Time Variables
        $Date = Get-Date -UFormat %b-%m-%Y
        $Hour = (Get-Date).Hour
        $Minuntes = (Get-Date).Minute
        $Log = "C:\support\cisco\Log Files\AnyConnect Install-" + $Date + "-" + $Hour + "-" + $Minuntes + ".log"

        #Creates a log file for this process.
        Start-Transcript -Path $Log  -Force 

        #List of computers to be check
        $ComputerNames = Get-Content "C:\support\cisco\Computers.txt"

        #Ping the computers on the list
        foreach ($ComputerName in $ComputerNames) {
            if (-not (Test-Connection $ComputerName -Quiet -Count 1 -ErrorAction Continue)) {
                #If theres no ping answer pass to the next one
                Write-Output "Computer $ComputerName not reachable (PING) - Skipping this computer..."
            } else {
                #If computer does answer the ping
                Write-Output "Computer $ComputerName is online"
            }
        }

        foreach ($ComputerName in $ComputerNames) {
            Write-Host $ComputerName
            if ((Test-Path "\\$ComputerName\c$\support\cisco") -eq 0) {
                mkdir "\\$ComputerName\c$\support\cisco"
                robocopy /mir "C:\support\cisco" "\\$ComputerName\c$\support\cisco"
            } elseif ((Test-Path "\\$ComputerName\c$\support\cisco") -eq 1) {
                robocopy /mir "C:\support\cisco" "\\$ComputerName\c$\support\cisco"
            }
        }
    }
}
Stop-Transcript

总之:如果没有响应,我希望脚本进行ping,然后继续前进,但是如果有ping,则移动文件,然后移动到下一个。

EN

回答 1

Stack Overflow用户

发布于 2017-11-02 13:06:29

使用单个foreach。

我还更新了Test-Connection和Test-path的逻辑,以便代码更加简洁。

代码语言:javascript
复制
#Creates a log file for this process.
Start-Transcript -Path $Log  -Force 

#List of computers to be check
$ComputerNames = Get-Content "C:\support\cisco\Computers.txt"

foreach ($ComputerName in $ComputerNames) {
    if (Test-Connection $ComputerName -Quiet -Count 1 -ErrorAction Continue ) {
        Write-Output "Computer $ComputerName is online" 

        if (Test-path "\\$ComputerName\c$\support\cisco") {
            robocopy /mir "C:\support\cisco" "\\$ComputerName\c$\support\cisco"
        }
        else {
            mkdir "\\$ComputerName\c$\support\cisco"
            robocopy /mir "C:\support\cisco" "\\$ComputerName\c$\support\cisco"
        }
    }
    Else {
        Write-Output "Computer $ComputerName not reachable (PING) - Skipping this computer..."
    }
}

Stop-Transcript

编辑:

删除所有重复的代码和值将为您提供更简洁的foreach循环:

代码语言:javascript
复制
foreach ($Computer in $ComputerNames) {
    $source = "C:\support\cisco"
    $destination = "\\$Computer\c$\support\cisco"

    if (Test-Connection $Computer -Quiet -Count 1) {
        Write-Output "Computer $Computer is online" 
        if (-not (Test-path $destination)) {New-Item -Path $destination -ItemType Directory}
        robocopy /mir $source $destination
    }
    else {
        Write-Output "Computer $Computer not reachable (PING) - Skipping this computer..."
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47075426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档