简体   繁体   English

从批处理文件运行Powershell命令以重命名网络驱动器

[英]Run Powershell Command from Batch File to Rename Network Drive

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. 我已经创建了一个可以正常工作的PS1脚本,但是我试图将该脚本放入一个批处理文件中,以便我可以运行处理多个顺序的非Powershell命令。 My PS1 is as follows: 我的PS1如下:

$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 Powershell CLI错误

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. 我最初关于如何解决此问题的参考来自此处: 如何在包含&符的批处理文件中运行powershell命令

When I could not get it to work I then tried the format here: Run powershell command from cmd 当我无法正常工作时,我尝试在此处尝试以下格式: 从cmd运行powershell命令

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. 这似乎是一个简单的问题,感谢您提供的任何帮助。

The $DriveMapping variable always comes out $null here. $DriveMapping变量总是在此处$null

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. 因此, Name属性不可用。

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. 诸如$DriveMapping变量不会在PowerShell调用之间持久存在。 When PowerShell is run again, the $DriveMapping variable does not exist. 再次运行PowerShell时, $DriveMapping变量不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM