【问题标题】:PowerShell Script to Open and select Network Places?PowerShell 脚本打开并选择网上邻居?
【发布时间】:2015-01-20 02:08:02
【问题描述】:

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

但是,对于 7、8、8.1 和即将推出的 10,BrowseForFolder 不显示网络:|所以我检测操作系统,如果是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 脚本并选择“使用 PowerShell 运行”时,它显示了我想看到的内容,尽管我的命名空间显然不正确,并且无法键入位置:\

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

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

【问题讨论】:

    标签: powershell vbscript namespaces showdialog


    【解决方案1】:

    您的 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 上测试此脚本,但我不明白为什么它不起作用。

    【讨论】:

    • 谢谢 - 整个 SelectedFolder 不存在是我正在寻找的东西之一 :) 我对 PowerShell 脚本很陌生:\
    • Grrr - 我按下回车键,它在我完成评论之前提交。

      无论如何,我没有收到错误消息,但是当从 VBScript 中调用时它仍然不显示网络...所以我认为这是一个窗口错误。

      我试图 +1 你的回复,但它不会让我因为我是新人。所以几天后,如果没有其他人cets,我会在Win10中将其作为一个错误关闭。再次感谢您。
    • 我是个白痴 - 经过进一步测试,我的操作系统测试似乎无法正常工作:\ 它默认为 VBScript getFolder :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多