用于打开和 select 网上邻居的 PowerShell 脚本?

PowerShell Script to Open and select Network Places?

我有一个 VbScript 提示用户 select 一个文件夹。该文件夹通常位于网络上。对于 Vista 和 XP,VbScript BrowseForFolder 工作正常。

但是,对于 7、8、8.1 和即将推出的 10,BrowseForFolder 不显示网络 :|所以我检测到 OS,如果它是 7 或更高版本,我尝试使用 powerShell,但它没有显示网络列表。

我试图查看并设置为默认起始位置的命名空间是网上邻居。

::{208d2c60-3aea-1069-a2d7-08002b30309d}

保存为 getFolder.ps1 在临时文件夹中是

Add-Type -AssemblyName System.Windows.Forms;
$Folder = New-Object System.Windows.Forms.FolderBrowserDialog;
$Folder.ShowNewFolderButton = $true;
$Folder.SelectedPath = [System.Environment]::GetFolderPath("MyComputer");
[void]$Folder.ShowDialog();
$Folder.SelectedPath;

目前我正在开发 Windows 10 TP build 9879,但我在 Windows 8 上有类似的行为。理论上,这是开箱即用的。

调用VBScript本质上是

Dim oShell
If WScript.Arguments.Named.Exists( "elevated" ) = False Then
 CreateObject( "Shell.Application" ).ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
 WScript.Quit
Else
 Set oShell = CreateObject( "WScript.Shell" )
 oShell.CurrentDirectory = CreateObject( "Scripting.FileSystemObject" ).GetParentFolderName( WScript.ScriptFullName )
End If

Dim tmpFolder, psScrpt, psLine, execmd, pth
Set oShell = CreateObject( "WScript.Shell" )
tmpFolder = oShell.ExpandEnvironmentStrings( "%temp%" )
psScrpt = getFolder.ps1

psLine = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -NoProfile -Command " & tmpFolder & "\" & psScrpt

Set execmd = shl.Exec( psLine )
execmd.StdIn.Close

pth = Trim( Replace( execmd.StdOut.ReadAll, vbNewLine, "", 1, -1, vbTextCompare ) )
Wscript.Echo pth

有趣的是,当我右键单击 PowerShell 脚本时,select“运行 with PowerShell”显示了我想要的内容我想看看我的 nameSpace 显然没有正确设置,而且无法输入位置 :\

当我从我的 vbScript 运行 它时,我只看到 "My Computer":C 驱动器、USB 驱动器、光驱、映射驱动器。但是,运行ning-from-inside-script 版本允许我输入路径,\SomeServer\SomeShare,它被接受了。 “运行 With PowerShell”没有显示。

是的,我很困惑,需要帮助 - 任何帮助将不胜感激。谢谢。

您的 powershell 脚本中存在一些问题。首先,$Folder 对象没有 SelectedFolder 成员(它是 SelectedPath)。要查看成员,请在命令提示符下键入 $Folder | gm。其次,您可以使用 System.Environment 上提供的 GetFolderPath 方法访问特殊文件夹,提供特殊文件夹名称。

Add-Type -AssemblyName System.Windows.Forms
$Folder = New-Object System.Windows.Forms.FolderBrowserDialog
$Folder.ShowNewFolderButton = $true
$Folder.SelectedPath = [System.Environment]::GetFolderPath("NetworkShortcuts")
[void]$Folder.ShowDialog()
$Folder.SelectedPath

我无法在 Windows 10 上测试此脚本,但我不明白为什么它不起作用。