我看到很多关于如何从映射的驱动器号获取UNCpath的问题,但是却没有发现相反的问题。
在不同的Windows 7企业机器上,我得到了具有完全相同的input,当前目录和networking驱动器映射的System.Windows.Forms.OpenFileDialog
实例的FileName
的不同答案:
一台计算机用UNCpath回答
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
其他人用映射的驱动器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
我需要每台计算机都使用映射的驱动器path来回答:接收FileName
的命令行程序不理解UNCpath。 这里是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
我总是需要做什么才能获得映射的驱动器path?
我认为最好的办法是测试返回的UNC路径,如果是的话,暂时将其映射到驱动器盘符,然后将其发送到gpg
。 也许这样的事情:
if (([uri]$dest).IsUnc) { $destPath = $dest | Split-Path -Parent # Find a free drive letter $letter = ls function:[dz]: -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 }
这是未经测试的..
获取免费驱动器盘符的单线来自这里: 获取免费的驱动器盘符
新PSDrive参考
删除PSDrive参考