1

I am trying to create a batch file to rename network drives, since 'Label' does not work. I have created a PS1 script which works, but I am trying to get the script into a single Batch file so I can run handle several sequential non-powershell commands. My PS1 is as follows:

$ShellObject = New-Object –ComObject Shell.Application
$DriveMapping = $ShellObject.NameSpace('Z:')
$DriveMapping.Self.Name = 'DataStore'

My current Batch File command is as follows:

powershell -Command " $ShellObject = New-Object –ComObject Shell.Application; $DriveMapping = $ShellObject.NameSpace('Z:'); $DriveMapping.Self.Name = 'DataStore'"

Which gives me the following error:

Powershell CLI Error

The property 'Name' cannot be found on this object. Verify that the property exists and can be set...

My original reference on how to approach this was from here:How to run powershell command in batch file which included the ampersand.

When I could not get it to work I then tried the format here:Run powershell command from cmd

At this point I have tried a few different arrangements of semi-colons, quotes, apostrophes, etc without any success. This seems like a simple problem and appreciate any help offered.

Agator82
  • 13
  • 3
  • Have you checked to see if `$DriveMapping` is $null? That is what happens here. – lit Oct 10 '18 at 13:48
  • Thinking outside the box on this and is something I have never tried. Is it possible to create the PS1 file via a batch file, execute the PS1 file with PS calling the PS1 file, and then delete the PS1 that was created? – Clayton Lewis Oct 10 '18 at 13:50
  • I just ran the above command and then I ran, `powershell -Command "$DriveMapping -ne $null"` 'False'. So I do not think the value is null. @lit – Agator82 Oct 10 '18 at 14:29
  • @Backin I like that suggestion as as a work around, I'll remember it if I cannot get the translation between the CLI and PS to work out. Thanks. – Agator82 Oct 10 '18 at 14:30
  • Does the three (3) line PowerShell script run as expected from a .ps1 file? – lit Oct 10 '18 at 16:28

1 Answers1

0

The $DriveMapping variable always comes out $null here.

C:>type drivemap.bat
powershell -NoProfile -Command ^
    "$ShellObject = New-Object -ComObject Shell.Application;" ^
    "$DriveMapping = $ShellObject.NameSpace('Z:');" ^
    "$DriveMapping -eq $null;" ^
    "$DriveMapping.Self.Name = 'DataStore'"

Therefore, the Name property is not available.

C:>powershell -NoProfile -Command     "$ShellObject = New-Object -ComObject Shell.Application;"     "$DriveMapping = $ShellObject.NameSpace('Z:');"     "$DriveMapping -eq $null;"     "$Drive
Mapping.Self.Name = 'DataStore'"
True
The property 'Name' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:128
+ ... 'Z:'); $DriveMapping -eq $null; $DriveMapping.Self.Name = 'DataStore'
+                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

Variables such as $DriveMapping do not persist across PowerShell invocations. When PowerShell is run again, the $DriveMapping variable does not exist.

lit
  • 14,456
  • 10
  • 65
  • 119
  • Expand the question to ask, is there a way to use PowerShell at the CLI so that the second call for $DriveMapping occurs during the first invocation? – Agator82 Oct 10 '18 at 16:23
  • A second invocation of PowerShell starts afresh. It does not know about any variables from a different invocation of PowerShell. Is that what you are asking? – lit Oct 10 '18 at 16:27