Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Test-PCConnection {
- [cmdletbinding()]
- param(
- [parameter(Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelineByPropertyName = $true)]
- [String[]]
- $ComputerName
- )
- Begin{
- $ComputersArray = New-Object System.Collections.Generic.List[object]
- }
- Process{
- ForEach($name in $ComputerName){
- $ComputersArray.Add(
- [PSCustomObject]@{
- ComputerName = $name
- Job = (Test-Connection -ComputerName $name -Count 3 -BufferSize 16 -AsJob)
- Return = $null
- PingResult = $null})
- }
- }
- End{
- [void](Get-Job | Wait-Job)
- ForEach($computer in $ComputersArray){
- $computer.Return = Receive-Job $computer.Job
- If($computer.Return | ForEach-Object {If($_.StatusCode -eq 0){$true}}){
- $computer.PingResult = "Online"
- }
- Else{
- $computer.PingResult = "Offline"
- }
- $computer.PSObject.Members.Remove('Job')
- $computer.PSObject.Members.Remove('Return')
- }
- Get-Job -Filter @{Command = "Test-Connection"} | Remove-Job
- $ComputersArray
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment