2017-10-31 5 views
0

私はlog.iniファイルに以下の詳細を記載しています。テキストファイルからIPを割り当てます。

Hostname : DLC1MQF 
IP Address : 10.210.208.102 
Temporary IP : 10.212.215.91 
Subnet Mask : 255.255.248.0 
Gateway  : 10.212.208.1 

私が実際に探しているのは、チェックしたいです。デフォルトのゲートウェイがpingしているかどうか。 pingを実行しない場合は、log.iniからIP、サブネット、ゲートウェイを取得し、NICに割り当てる必要があります。

現在、以下のようなスクリプトを書くことができますが、問題は認識されていないコマンドであるというエラーメッセージが表示されていることです。

$imagelog = Get-Content C:\Windows\ImageLog.ini 
foreach ($line in $imagelog) { 
if ($line -like "*Gateway*") { 
$line | out-file -FilePath "C:\Windows\Gateway.txt" 
} 
} 

$gatewayIP = get-content c:\windows\Gateway.txt 
$GIP = $gatewayIp -replace '.*:.'.Trim() 


if (Test-Connection -ComputerName $Gip -Count 1 -ErrorAction SilentlyContinue) { 
Write-Host $GIP `t $GIP `t Ping Success -ForegroundColor Green 

} 

else{ 

$details = get-content c:\windows\imagelog.ini 

foreach ($line in $details) { 
if ($line -like "*IP Address*") { 
$line | out-file -FilePath "C:\Windows\IP Address.txt" 
} 
#} 


$IP = get-content c:\windows\IP Address.txt 
$systemip = $Ip -replace '.*:.'.Trim() 

foreach ($line in $details) { 
if ($line -like "*Subnet Mask*") { 
$line | out-file -FilePath "C:\Windows\subnet.txt" 
} 
} 

$subnet = get-content c:\windows\subnet.txt 
$subnetip = $subnet -replace '.*:.'.Trim() 


netsh interface ip set address "Local Area Connection" static $systemip $subnetip $GIP 

foreach ($line in $details) { 

if (Hostname.StartsWith("LCG")) { 

Set-DNSClientServerAddress –Local Area Connection –ServerAddresses (“10.0.6.65”,”10.0.25.65”,"10.0.0.1") 

} 

elseif (Hostname.StartsWith("ENG")) { 


Set-DNSClientServerAddress –Local Area Connection –ServerAddresses (“10.80.38.33”,”10.0.25.65”,"10.0.0.1") 

} 
} 
} 
} 

ご協力いただきまして誠にありがとうございます。

答えて

0

あなたは記述を更新try/catchブロック

Try{ 
    test-connection $IP -ErrorAction Stop | out-null 
    write-output "working" 
    ...do working stuff... 
}Catch{ 
    ...do failed stuff... 
    write-output "failed" 
} 
+0

でテスト接続をラップすることができます – Sandeep

関連する問題