PoshCode Archive  Artifact Content

Artifact ef02cb81eab0e54b687a0c3c57538d2f52a6918b8471e531cea694b3afab04f1:

  • File Search-Network.ps1 — part of check-in [6349ea5403] at 2018-06-10 13:19:48 on branch trunk — This is my first real PowerShell script, not sure if anyone has done anything liek this before (I’m sure it has been done) but I figured Id share it anyway. Makes use of a function I found on here by Nathan Hartley called Get-NetView. (user: pnorms size: 4147)

# encoding: ascii
# api: powershell
# title: Search-Network
# description: This is my first real PowerShell script, not sure if anyone has done anything liek this before (I’m sure it has been done) but I figured Id share it anyway. Makes use of a function I found on here by Nathan Hartley called Get-NetView.
# version: 0.1
# type: function
# author: pnorms
# license: CC0
# function: Get-NetView
# x-poshcode-id: 3063
# x-archived: 2012-12-25T08:04:45
# x-published: 2012-11-21T09:58:00
#
# Script searches all viewable machines on the network, tests to see if they are online, tests to see if a particular service is installed, if service isn’t found it will add the PC’s name to an error log, if the service exists but isn’t running it will try to start it. A second log is also created that shows all actions.
# I built this to check that all machines on our network are running our anti-virus (yes the server reports this, but I just wanted to have something like this available if need be for other services / processes.)
# Let me know what you guys think I, I want to get better so criticism / encouragement is appreciated.
# Change Lines 5,6,7 to suit your needs, should run out of the box as long as you have a directory c:\temp.
# Thanks, Patrick
#
#Define PreReqs
$timeStamp = Get-Date -UFormat "%m-%d-%Y-%H-%M"
$systemVars = Gwmi Win32_ComputerSystem -Comp "."
$userName = $systemVars.UserName
$compName = $systemVars.Name

#User Vars
$serviceName = "Spooler" # Spooler will check the Print Spooler <<< Change To Suit Your needs
$errorLog = "C:\Temp\Log_"+$serviceName+"_"+$timeStamp+"_Errors.txt" #Service Not Installed
$fullLog = "C:\Temp\Log_"+$serviceName+"_"+$timeStamp+"_All.txt" #Services Needed To Be Started / Running

#Write Some Info To Logs
"Check Service: " + $serviceName > $errorLog; Get-Date >> $errorLog; $compName >> $errorLog; $userName >> $errorLog; "_____________" >> $errorLog;  "" >> $errorLog;
"Check Service: " + $serviceName > $fullLog; Get-Date >> $fullLog; $compName >> $fullLog; $userName >> $fullLog; "_____________" >> $fullLog;  "" >> $fullLog;

# Define Functions
function Get-NetView {
	switch -regex (NET.EXE VIEW) { "^\\\\(?<Name>\S+)\s+" {$matches.Name}}
    }

function Process-PCs ($currentName) {
    $olStatus = Ping-Address $currentName
    If ($olStatus -eq "True") {
    Check-Service $currentName
    }
    #Else {Write-Host "PC Not Online"}
    Write-Host " "
    }

function Ping-Address ($pingAddress) {
    $ping = new-object system.net.networkinformation.ping
    $pingReply = $ping.send($pingAddress)
        If ($pingReply.status -eq "Success") {
            Return "True"
            }
        Else {
            Return "False"
            }
    }
    
function Check-Service ($currentName) {
    $currentService = Get-Service -ComputerName $currentName -Name $serviceName -ErrorAction SilentlyContinue
    If ($currentService.Status -eq $Null){
        $currentServiceStatus = "Not Installed"
        $currentName >> $errorLog
        }
    ElseIf ($currentService.Status -eq "Running"){
        $currentServiceStatus = "Running"
        }
    ElseIf ($currentService.Status -eq "Stopped"){
        $currentServiceStatus = "Stopped"
        }
    Else {
        $currentServiceStatus = "Unknown"
        }
        
        Write-Host $serviceName " is " $currentServiceStatus " on " $currentName
        $serviceName + " was " + $currentServiceStatus + " on " + $currentName >> $fullLog
    If ($currentService.Status -eq "Stopped"){
        Write-Host "Service was stoppped, trying to start it . . ."
        $currentService | Start-Service -ErrorAction SilentlyContinue
        If ($currentService.Status -eq "Running"){
            "   Service Successfully Started" >> $fullLog
            }
        Else {
            "   Service Could Not Be Started" >> $fullLog
            }
       }
    }


#Run Everything
cls
Get-NetView | %{Process-PCs $_}
# Test a single PC, Uncomment line below change pc name and comment line above
# Process-PCs "localhost" | %{Process-PCs $_}