MapNetworkSharesRemotely.psm1
# # # MapNetworkSharesRemotely module description # # $ErrorActionPreference = 'Stop' # # Module functions # Function Invoke-MapNetworkShare { <# .SYNOPSIS Map a Windows network share remotely .DESCRIPTION Map a Windows network share remotely .PARAMETER ComputerName Name of the target computer .PARAMETER DriveLetter The drive letter to assign the share to .PARAMETER NetworkPath The path to the network location .PARAMETER UserName The user name to connect the share to .PARAMETER OnLogon Map the network share when the user logon (Optional) .EXAMPLE Invoke-MapNetworkShare -ComputerName LabPC2064 -DriveLetter R -NetworkPath \\Server1\Documents\Finance -UserName MyDomain\John.Doe .EXAMPLE Invoke-MapNetworkShare -ComputerName LabPC2064 -DriveLetter D, J, T -NetworkPath \\Server1\Documents\Finance, \\Server1\Documents\Receipts, \\Server1\Documents\Invoices -UserName MyDomain\John.Doe -OnLogon .NOTES The share mapping is done via a scheduled task, it might take up to a minute to execute this task .LINK N/A #> [CmdletBinding ()] Param ( [Parameter (Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = 'Enter computer name' ) ] [String[]]$ComputerName, [Parameter (Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = 'Enter drive letter' ) ] [String[]]$DriveLetter, [Parameter (Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = 'Enter network path' ) ] [String[]]$NetworkPath, [Parameter (Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = 'Enter user name' ) ] [String[]]$UserName, [Switch]$OnLogon ) BEGIN { $DriveLetterIndex = 0 $UserNameIndex = 0 Function Show-Output ($Values) { [PSCustomObject]@{ ComputerName = $Values[0] DriveLetter = $Values[1] NetworkPath = $Values[2] UserName = $Values[3] Status = $Values[4] } } } PROCESS { ForEach ($Computer In $ComputerName) { If (Test-Connection -ComputerName $Computer -Count 1 -Quiet) { $MapUser = $UserName[$UserNameIndex] ForEach ($MapLetter In $DriveLetter) { $MapLetter = $MapLetter.Replace(':', '') $Time = (Get-Date).AddMinutes(1).ToString('HH:mm') $TaskID = New-Guid $Task = "Net Use $MapLetter" + ': ' + $NetworkPath[$DriveLetterIndex] + ' /Persistent:Yes' $TaskName = 'Map_Share_' + $TaskID If ($OnLogon) { SchTasks /CREATE /S $Computer /RU $MapUser /SC ONLOGON /TN $TaskName /TR $Task /F | Out-Null If ($LASTEXITCODE -eq 0) { Show-Output ($Computer.ToUpper(), $MapLetter.ToUpper(), $NetworkPath[$DriveLetterIndex], $MapUser, 'Map on logon') } Else { Show-Output ($Computer.ToUpper(), '', '', '', 'Failed') } } Else { SchTasks /CREATE /S $Computer /RU $MapUser /SC ONCE /TN $TaskName /TR $Task /ST $Time /F | Out-Null If ($LASTEXITCODE -eq 0) { Show-Output ($Computer.ToUpper(), $MapLetter.ToUpper(), $NetworkPath[$DriveLetterIndex], $MapUser, 'Mapped') } Else { Show-Output ($Computer.ToUpper(), '', '', '', 'Failed') } } $DriveLetterIndex++ } } Else { Show-Output ($Computer.ToUpper(), '', '', '', 'Unreachable') } $DriveLetterIndex = 0 } $UserNameIndex++ } END {} } Function Invoke-DisconnectNetworkShare { <# .SYNOPSIS Disconnect a Windows network share remotely .DESCRIPTION Disconnect a Windows network share remotely .PARAMETER ComputerName Name of the target computer .PARAMETER DriveLetter The drive letter to disconnect .PARAMETER UserName The user name to disconnect the share from .PARAMETER OnLogon Disconnect the network share when the user logon (Optional) .EXAMPLE Invoke-DisconnectNetworkShare -ComputerName LabPC2064 -DriveLetter R -UserName MyDomain\John.Doe .EXAMPLE Invoke-DisconnectNetworkShare -ComputerName LabPC2064 -DriveLetter D, N, Z -UserName MyDomain\John.Doe -OnLogon .NOTES The share disconnection is done via a scheduled task, it might take up to a minute to execute this task .LINK N/A #> [CmdletBinding ()] Param ( [Parameter (Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = 'Enter computer name' ) ] [String[]]$ComputerName, [Parameter (Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = 'Enter drive letter' ) ] [String[]]$DriveLetter, [Parameter (Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = 'Enter user name' ) ] [String[]]$UserName, [Switch]$OnLogon ) BEGIN { $DriveLetterIndex = 0 $UserNameIndex = 0 Function Show-Output ($Values) { [PSCustomObject]@{ ComputerName = $Values[0] DriveLetter = $Values[1] Status = $Values[2] } } } PROCESS { ForEach ($Computer In $ComputerName) { If (Test-Connection -ComputerName $Computer -Count 1 -Quiet) { $MapUser = $UserName[$UserNameIndex] ForEach ($MapLetter In $DriveLetter) { $MapLetter = $MapLetter.Replace(':', '') $Time = (Get-Date).AddMinutes(1).ToString('HH:mm') $TaskID = New-Guid $Task = "Net Use $MapLetter" + ': /Delete' $TaskName = 'Map_Share_' + $TaskID If ($OnLogon) { SchTasks /CREATE /S $Computer /RU $MapUser /SC ONLOGON /TN $TaskName /TR $Task /F | Out-Null If ($LASTEXITCODE -eq 0) { Show-Output ($Computer.ToUpper(), $MapLetter.ToUpper(), 'Disconnect on logon') } Else { Show-Output ($Computer.ToUpper(), '', 'Failed') } } Else { SchTasks /CREATE /S $Computer /RU $MapUser /SC ONCE /TN $TaskName /TR $Task /ST $Time /F | Out-Null If ($LASTEXITCODE -eq 0) { Show-Output ($Computer.ToUpper(), $MapLetter.ToUpper(), 'Disconnected') } Else { Show-Output ($Computer.ToUpper(), '', 'Failed') } } $DriveLetterIndex++ } } Else { Show-Output ($Computer.ToUpper(), '', 'Unreachable') } $DriveLetterIndex = 0 } $UserNameIndex++ } END {} } |