0

I'm writing a small script to map and rename netword drives. I wanted to use variables (user input) in drive letter, but for some reason the script won't accept anything but static drive letters. Please help

$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))

Error messeges as below

New-PSDrive : Cannot process the drive name because the drive name contains one or more of the following characters that are not valid: ; ~ / \ . : At C:\Users\Axel\Desktop\TESTY\Szmery Bajery.ps1:9 char:21 + ... New-PSDrive -Name $textBox -PSProvider FileSystem -Root " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-PSDrive], PSArgumentException + FullyQualifiedErrorId : Argument,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
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
Nawad-sama
  • 151
  • 2
  • 12

2 Answers2

1

Persistent drives MUST be named with letter.

-Name parameter is described: Specifies a name for the new drive. For persistent mapped network drives, use a drive letter. For temporary PowerShell drives, you aren't limited to drive letters, use any valid string.

Check -Persist parameter here https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-5.1#parameters

Karolina Ochlik
  • 908
  • 6
  • 8
  • [Correction] Thanks, I did not know that the name for persistent drives must be plain text. So on that note is it possible to (in single script block), create temporary network drive assigned to "variable letter" and then make it persistent? – Nawad-sama Jun 07 '21 at 09:14
  • 1
    _The name of the drive must be a letter, such as D or E._ I guess it pretty much sums up your question :) – Karolina Ochlik Jun 07 '21 at 09:32
  • I guess it does. I was just hoping ;) I will try to use cmd via PS and invoke net.exe. It worked via batch script. – Nawad-sama Jun 07 '21 at 09:50
0

For those inetrested. This is what I came Up with, and it works like a charm

$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"
                    }
Nawad-sama
  • 151
  • 2
  • 12