程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在 New-PSDrive 中使用变量?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在 New-PSDrive 中使用变量??

开发过程中遇到如何在 New-PSDrive 中使用变量?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在 New-PSDrive 中使用变量?的解决方法建议,希望对你解决如何在 New-PSDrive 中使用变量?有所启发或帮助;

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

$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 字符:21 + ... New-PSDrive -name $textBox -PSProvIDer fileSystem -Root " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~ + categoryInfo : InvalIDArgument: (:) [New-PSDrive],PSArgumentexception + FullQualifIEdErrorID:参数,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

解决方法

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

-Name 参数描述: 指定新驱动器的名称。对于持久映射网络驱动器,请使用驱动器号。对于临时 PowerSHell 驱动器,您不限于驱动器号,可以使用任何有效字符串。

在此处检查 -Persist 参数 https://docs.microsoft.com/en-us/powersHell/module/microsoft.powersHell.management/new-psdrive?view=powersHell-5.1#parameters

,

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

$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"
                    }

大佬总结

以上是大佬教程为你收集整理的如何在 New-PSDrive 中使用变量?全部内容,希望文章能够帮你解决如何在 New-PSDrive 中使用变量?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。