小编Mat*_*Moo的帖子

Powershell试用/捕获测试连接

我正在尝试将离线计算机记录在文本文件中,以便我可以在以后再次运行它们.似乎没有记录或陷入捕获.

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 }}}
Run Code Online (Sandbox Code Playgroud)

powershell try-catch get-eventlog

7
推荐指数
1
解决办法
1万
查看次数

PowerShell测试连接,如果使用get-service存在服务

基本上我想查看文本文件中的计算机是否在线.如果他们不在线,则写主机"$ computer is down".如果$计算机在线,那么检查是否存在此服务,如果它存在则写入主机"$ computer installed,如果没有则写入主机"$ computer not installed".测试连接似乎有效但如果计算机在线他们都返回写主机"$ computer installed",即使我有一台我知道没有运行此服务的测试机器.

 function Get-RunService {

 $service = get-service -name ABCService

 Get-Content "C:\powershell\computers.txt" | 

  foreach {if (-not (Test-Connection -comp $_ -quiet))
  {
  Write-host "$_ is down" -ForegroundColor Red
  }
   if ($service ) 
   { 
    write-host "$_  Installed"
  }

else {

Write-host "$_  Not Installed"

}


 }

}

get-RunService
Run Code Online (Sandbox Code Playgroud)

powershell if-statement

0
推荐指数
1
解决办法
2万
查看次数