2015-09-17 16 views
5

Çevrimdışı bilgisayarları bir metin dosyasına kaydetmeye çalışıyorum, böylece daha sonra tekrar çalıştırabilirim. Kaydedilmiyor ya da yakalanmış gibi görünmüyor.Powershell test bağlantısı ile deneme/yakalama

function Get-ComputerNameChange { 

    [CmdletBinding()] 
    Param(
    [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)] 
    [string[]]$computername, 
    [string]$logfile = 'C:\PowerShell\offline.txt' 
    ) 




    PROCESS { 

     Foreach($computer in $computername) { 
     $continue = $true 
     try { Test-Connection -computername $computer -Quiet -Count 1 -ErrorAction stop 
     } catch [System.Net.NetworkInformation.PingException] 
     { 
      $continue = $false 

      $computer | Out-File $logfile 
     } 
     } 

     if($continue){ 
     Get-EventLog -LogName System -ComputerName $computer | Where-Object {$_.EventID -eq 6011} | 
     select machinename, Time, EventID, Message }}} 

cevap

3

trycatch ing istisnalar içindir.anahtarını kullanıyorsunuz, bu nedenle Test-Connection$true veya $false döndürür ve bağlantı başarısız olduğunda throw bir istisna değildir.

Hemen yapın: çalışmıştır

if (Test-Connection -computername $computer -Quiet -Count 1) { 
    # succeeded do stuff 
} else { 
    # failed, log or whatever 
} 
+0

Evet. Metin dosyamda neden sadece son bilgisayar isminde borular olduğunu biliyor musunuz? "İçerik almayı deniyorum" \\ test \ c $ \ powershell \ computers.txt "| get-computernameChange – MattMoo

+0

@ user3620584 Sonunda sadece boru tesisatını nasıl biliyorsunuz? – briantist

+0

Bir test olarak var olmayan 3 bilgisayar ismini koyuyorum ve sadece log dosyasında sonuncuyu listeledim. – MattMoo