【问题标题】:Script to restart server if IP address binding doesn't exists如果 IP 地址绑定不存在,脚本重新启动服务器
【发布时间】:2013-12-03 20:27:54
【问题描述】:

我需要一个脚本(powershell 或 vbs)来检查服务器是否拥有 IP 地址。如果它什么都不做,如果它不重启服务器

虽然我会运行 netstat 并输出到文件,然后让脚本读取文件并检查特定值(IP 地址)

if "ip address" exists in txt file then quit
if "ip address" doen't exists in txt file then retstart computer

我有一些 powershell,但我需要将它们连接在一起

第一次运行

C:>netstat -a -p tcp >c:netstat.txt

然后运行这个,如果输出为真则退出,如果输出为假则重启计算机

C:>get-content c:\netstat.txt | select-string "192.168.0.1" -quiet

【问题讨论】:

    标签: powershell


    【解决方案1】:

    有很多方法可以做到这一点,这里有一些。

    # locally, check if the ip was found in the output of ipconfig
    [bool] ((ipconfig) -like '*IPv4 Address*192.168.0.1*')
    
    # ask dns
    $cn = [System.Net.Dns]::GetHostByAddress('192.168.0.1') | ForEach-Object {$_.HostName}
    
    if($cn -notlike "*server1*")
    {
        Restart-Computer server1 -Force
    }
    
    # get all ip addresses of a local or remote server
    $ip = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName server1 | ForEach-Object {$_.IPAddress}
    
    if($ip -notcontains '192.168.0.1')
    {
        Restart-Computer server1 -Force
    }
    

    【讨论】:

    • 感谢 Sahy,第三个脚本适用于我的情况。感谢您的帮助问候
    猜你喜欢
    • 2021-01-20
    • 1970-01-01
    • 2014-12-24
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2017-05-04
    相关资源
    最近更新 更多