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



>我正在尝试创建一个批处理文件来重命名网络驱动器,因为"标签"不起作用。 我已经创建了一个可以工作的 PS1 脚本,但我正在尝试将脚本放入单个批处理文件中,以便我可以运行处理几个顺序的非 powershell 命令。 我的 PS1 如下:

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

我当前的批处理文件命令如下:

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

这给了我以下错误:

Powershell CLI 错误

在此对象上找不到属性"名称"。验证该属性是否存在并且可以设置...

我关于如何处理这个问题的原始参考来自这里:如何在包含 & 符号的批处理文件中运行 powershell 命令。

当我无法让它工作时,我在这里尝试了格式:从cmd运行powershell命令

在这一点上,我已经尝试了几种不同的分号、引号、撇号等排列方式,但没有任何成功。 这似乎是一个简单的问题,并感谢提供的任何帮助。

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

因此,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

变量(如$DriveMapping(不会在 PowerShell 调用中保留。再次运行 PowerShell 时,$DriveMapping变量不存在。

最新更新