Reputation: 23
I see many questions on how to get UNC paths from mapped drive letters, but none on the reverse problem.
On different Windows 7 Enterprise machines, I'm getting different answers for the FileName
of System.Windows.Forms.OpenFileDialog
instances with the exact same input, current directory, and network drive mapping:
one computer answers with a UNC path
PS A:\Liver\Department\Records\check log> .\sign-log.ps1
VERBOSE: Writing signature to \\liver53-pc.ad.institute.edu\Documents\Liver\Department\Records\check log\00\check log.xlsx.sig
others answer with a mapped drive path
PS A:\Liver\Department\Records\check log> .\sign-log.ps1
VERBOSE: Writing signature to A:\Liver\Department\Records\check log\00\check log.xlsx.sig
I need every computer to answer with mapped drive paths: the command-line program receiving the FileName
doesn't understand UNC paths.
Here are the contents of sign-log.ps1
:
Add-Type -AssemblyName System.Windows.Forms
function Sign-Log ($file = '.\check-log.xlsx') {
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
Title = 'Select'
; InitialDirectory = (Get-Location).Path
; Filename = 'check log.xlsx'
; Filter = 'Excel Spreadsheet (*.xlsx;*.xls)|*.xlsx;*.xls|Any file (*.*)|*.*'
; ShowHelp = $true
}
if ($FileBrowser.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$file = $FileBrowser.FileName
$dest = $file + '.sig'
if (Test-Path -Path $dest) {
Write-Verbose -Message ('Appending signature to {0}' -f $dest)
Add-Content -Path $dest -Value (gpg '-ao' '-' '-b' $file)
} else {
Write-Verbose -Message ('Writing signature to {0}' -f $dest)
gpg '-ao' $dest '-b' $file
}
gpg '--verify' $dest
}
$FileBrowser.Dispose()
}
Sign-Log
What do I need to do to always get mapped drive paths?
Upvotes: 1
Views: 898
Reputation: 47792
I think the best bet would be to test for a UNC path being returned, and if so, temporarily map it to a drive letter, then send that to gpg
. Maybe something like this:
if (([uri]$dest).IsUnc) {
$destPath = $dest | Split-Path -Parent
# Find a free drive letter
$letter = ls function:[d-z]: -n | ?{ !(test-path $_) } | Select-Object -Last 1
New-PSDrive -Name $letter[0] -Root $destPath -Persist -PSProvider FileSystem
$dest = $destPath | Join-Path -ChildPath $file
}
# Then after you're done calling gpg:
if ($destPath) {
Remove-PSDrive -Name $letter[0] -Force
}
This is untested..
The one-liner for getting the free drive letter came from here: Getting a free drive letter
Upvotes: 1