Melbourne, Australia
To be provided
Use Contact to message

Read Log File and Mount PS Drive based on shares in log file

Harmik Batth Tech's Blog

Read Log File and Mount PS Drive based on shares in log file

This Script will Read the provided log file and Mount PS Drive based on the shares provided.

Make sure that Log File and Script is in same location, or alternatively change the log file location to desired location

This script is basically developed in response to  – https://gallery.technet.microsoft.com/scriptcenter/site/requests/Need-a-script-to-map-network-drives-listed-in-a-log-by-most-recent-date-7b6c0dd9

Log file looks like:

logfile

Script as per below

#**************************************************************************** 
# ReadLogAndMountDrives.ps1 
# This script is designed to read log file and mount drives for any listed drives 
# Author:         Harmik Singh Batth 
# Created On:     28-OCT-2016 
# Created By:     Harmik Singh Batth 
# Version:         1.0 
# Change History: 
# 1. 
# 2.         
#**************************************************************************** 
 
#########################VARIABLES###################### 
$strFileName = "log.txt" 
$strbasedir = Split-Path $script:MyInvocation.MyCommand.Path 
 
$strFilePath = $strbasedir + "\" + $strFileName 
 
#####################END OF VARIABLES################## 
 
##############EXECUTION STARTS HERE##################### 
 
# Get the contents of File in variable to read line by line 
$reader = [System.IO.File]::OpenText($strFilePath) 
 
# Loop through all lines of the file until the end is reached 
While ($reader.peek()-ne -1) 
{ 
    $strLine = $reader.ReadLine() 
    # Check for common creteria in line which describe it is a share "\\" 
    IF ($strLine -match "\\") 
    { 
        # Get Drive Letter and Drive path from the line 
        # Split the line by comma "," 
        $strLine=$strLine.split(",")  
        $DriveLetter = $strLine[0] 
        $DrivePath = $strLine[2] 
         
        $DriveLetter  
        $DrivePath  
         
        #Map Drive 
        New-psdrive $DriveLetter -PSProvider "FileSystem" -Root $DrivePath 
    } 
} 
 
#####################SCRIPT FINISHED#####################

 

 

No Comments

Add your comment