在 IP 地址绑定不存在时重新启动服务器的脚本



我需要一个脚本(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

然后运行此程序,如果输出为true,则退出;如果输出为false,则重新启动计算机

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

有很多方法可以实现这一点,下面是一些。

# 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
}

相关内容

最新更新