Functions/ConvertTo-UncPath.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
function ConvertTo-UncPath { <# .SYNOPSIS A simple function to convert a local file path and a computer name to a network UNC path. .DESCRIPTION A simple function to convert a local file path and a computer name to a network UNC path. .PARAMETER LocalFilePath A file path ie. C:\Windows\somefile.txt .PARAMETER ComputerName One or more computers in which the file path exists on. Aliased to 'CN', 'Server' .PARAMETER IncludeInput Switch to include input parameters in output .EXAMPLE ConvertTo-UncPath -Path 'c:\adminTools\SomeFolder' -ComputerName 'SomeRemoteComputer' \\SomeRemoteComputer\c$\adminTools\SomeFolder .EXAMPLE ConvertTo-UncPath -Path 'c:\adminTools\SomeFolder' -ComputerName 'SomeRemoteComputer','SomeAnotherComputer' \\SomeRemoteComputer\c$\adminTools\SomeFolder \\SomeAnotherComputer\c$\adminTools\SomeFolder .EXAMPLE ConvertTo-UncPath -Path 'c:\Temp' -ComputerName DemoLaptop -IncludeInput Would return ComputerName Path UNCPath ------------ ---- ------- DemoLaptop c:\Temp \\DemoLaptop\c$\Temp .OUTPUTS Will create a string for remote computer path .NOTES Inspired by: https://www.powershellgallery.com/packages/PPoShTools/1.0.19 * added -IncludeInput #> [CmdletBinding()] [OutputType('String')] param ( [Parameter(Mandatory, HelpMessage = 'Add help message for user', ValueFromPipeline, ValueFromPipelineByPropertyName)] [Alias('LocalFilePath')] [string] $Path, [Alias('CN', 'Server')] [string[]] $ComputerName = $env:COMPUTERNAME, [switch] $IncludeInput ) begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { try { foreach ($Computer in $ComputerName) { $RemoteFilePathDrive = ($Path | Split-Path -Qualifier).TrimEnd(':') $ReturnVal = "\\$Computer\$RemoteFilePathDrive`$$($Path | Split-Path -NoQualifier)" if ($IncludeInput) { New-Object -TypeName psobject -Property ([ordered] @{ ComputerName = $Computer Path = $Path UNCPath = $ReturnVal }) } else { Write-Output -InputObject $ReturnVal } } } catch { Write-Log -Error -Message $_.Exception.Message } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |