【问题标题】:How to use a variable with New-PSDrive?如何在 New-PSDrive 中使用变量?
【发布时间】:2021-08-24 08:08:49
【问题描述】:

我正在编写一个小脚本来映射和重命名网络驱动器。我想在驱动器号中使用变量(用户输入),但由于某种原因,脚本只接受静态驱动器号。请帮忙

$button_click_2 = { Remove-PSDrive -Name K -Force
                    New-PSDrive -Name $textBox -PSProvider FileSystem -Root "\\192.168.0.10\GRY" -Persist -Scope Global
                    $shell = New-Object -ComObject Shell.Application
                    $letter = -join($textBox,":")
                    $shell.NameSpace("$letter").Self.Name = "Test Oliego 3"
                    }


$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(240,20)
$form.Controls.Add($textBox)

$test_button = New-Object System.Windows.Forms.Button
$test_button.Location = New-Object System.Drawing.Size(200,420)
$test_button.Size = New-Object System.Drawing.Size (170,23)
$test_button.Text = "Mapowanie Dysku Sieciowego"
$test_button.Add_Click($button_click_2)

$form.Controls.AddRange(@($test_button,$textBox))

错误信息如下

New-PSDrive : 无法处理驱动器名称,因为驱动器名称 包含一种或多种 以下无效字符: ; 〜/ \ 。 : 在 C:\Users\Axel\Desktop\TESTY\Szmery Bajery.ps1:9 char:21 + ... New-PSDrive -Name $textBox -PSProvider FileSystem -Root " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-PSDrive], PSArgumentException + FullyQualifiedErrorId : 参数,Microsoft.PowerShell.Commands.NewPSDriveCommand

The property 'Name' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\Axel\Desktop\TESTY\Szmery Bajery.ps1:12 char:21
+ ...               $shell.NameSpace("$letter").Self.Name = "Test Oliego 3"
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

【问题讨论】:

    标签: powershell variables new-psdrive


    【解决方案1】:

    永久驱动器必须以字母命名。

    -Name参数说明: 指定新驱动器的名称。对于永久映射网络驱动器,请使用驱动器号。对于临时 PowerShell 驱动器,您不限于驱动器号,使用任何有效字符串。

    在这里查看-Persist参数https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-5.1#parameters

    【讨论】:

    • [更正] 谢谢,我不知道永久驱动器的名称必须是纯文本。因此,在该注释中是否可以(在单个脚本块中)创建分配给“可变字母”的临时网络驱动器,然后使其持久化?
    • 驱动器的名称必须是字母,例如 D 或 E。 我想它几乎概括了你的问题 :)
    • 我猜是的。我只是希望 ;) 我将尝试通过 PS 使用 cmd 并调用 net.exe。它通过批处理脚本工作。
    【解决方案2】:

    对于那些感兴趣的人。这就是我想出的,它就像一个魅力

    $button_click_2 = { $letter = -join($textBox.Text,":")
                        Invoke-Expression "C:\Windows\System32\net.exe use $letter \\PATH /persistent:yes"            
                        $shell = New-Object -ComObject Shell.Application
                        $shell.NameSpace("$letter").Self.Name = "Test Oliego 3"
                        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多