PowerShell: Test-Host

       
       .Synopsis
            Test to see if this host is online and accessible.
       
        .Description
            Sends two pings the the hostname, then attempts to access the admin share for this host.
       
        .Parameter HostName
            Hostname or IP address to be tested.
       
        .Parameter skipPingTest
            This will allow you to skip the ping test when validating a host is up.
       
        .Parameter skipShareTest
            This will allow you to skip the share test when validating a host is up.
       
        .Example
            Test-Host DanielPC
            Description
            ———–
            Tests the host with both two pings and an admin share connection test.
           
        .Example
            Test-Host DanielPC -p
            Test-Host DanielPC -skipPingTest
            Description
            ———–
            Tests the host by admin share test and skips the ping test…
       
        .Example
            Test-Host DanielPC -s
            Test-Host DanielPC -skipShareTest
            Description
            ———–
            Tests the host by ping test only and skips the admin share test…
           
        .Example
            "DanielPC " | Test-Host
            Description
            ———–
            Receives input from the pipeline for testing if a host is online or not.
           
        .OUTPUTS
            Boolean
       
        .NOTES
            NAME:       Test-Host
            AUTHOR:     Daniel Moran
            Website:    http://www.moranit.com
            LASTEDIT:   04/18/2011
            #Requires   -Version 2.0
       
        .LINK
            https://danielheth.com/2011/04/18/powershell-test-host/

Advertisements

Recursive File Copier Function

Have you ever had the need for a function to copy every file in a directory… wait no longer… here is a recursive way to copy files:

Sub CopySelectedFiles(ByVal strSourceDirectory As String, ByVal strDestDirectory As String, ByVal strSearchString As String)
  Dim dirInfo As New DirectoryInfo(strSourceDirectory)
  Dim fsInfo As FileSystemInfo

  For Each fsInfo In dirInfo.GetFileSystemInfos
    Dim strDestFileName As String = Path.Combine(strDestDirectory, fsInfo.Name)

    If TypeOf fsInfo Is FileInfo And fsInfo.Name.IndexOfAny(strSearchString) > -1 Then
      File.Copy(fsInfo.FullName, strDestFileName, True) ' 'This will overwrite files that already exist
    Else
      CopySelectedFiles(fsInfo.FullName, strDestFileName, strSearchString) 'we have found a subdirectory
    End If
  Next
End Sub


					

Recursive File Search Code

Below is the first function I’m publishing… It uses my favorite programing feature… recursion. Recursion is a tricky thing to do right. So many loops, so many variables, etc… This function searches for a file and all sub-directories of your root/starter directory.

VB.net Source Code:

Public Function SearchForFile(ByVal strSourceDirectory As String, ByVal strSearchString As String) As String
  On Error Resume Next
  Dim ret As String = ""
  Dim dirInfo As New DirectoryInfo(strSourceDirectory)
  Dim fsInfo As FileSystemInfo

  For Each fsInfo In dirInfo.GetFileSystemInfos
    If TypeOf fsInfo Is FileInfo And _
      fsInfo.Name.IndexOfAny(strSearchString) > -1 Then
      'FOUND IT...
      If fsInfo.Name = strSearchString Then
        'return directory where file is located...
        ret = strSourceDirectory
        Exit For
      End If
    Else
      'recurrsively search this sub-folder...
      ret = SearchForFile(fsInfo.FullName, strSearchString)
      If ret <> "" Then Exit For
    End If
  Next

  If ret <> "" Then If Right(ret, 1) <> "\" Then ret = ret & "\"
  SearchForFile = ret
End Function