【发布时间】:2022-01-07 19:32:44
【问题描述】:
我正在开发几个 PowerShell 脚本来帮助加快将用户数据从旧工作站迁移到新工作站的过程。目前正在尝试制作一个来帮助检索然后部署映射的网络驱动器,并且在部署方面遇到了障碍。我是 PowerShell 的新手,并且在使用 ISE 帮助发现脚本存在的一些问题区域的过程中不断学习。这是脚本当前外观的副本以及尝试在机器上运行它时收到的错误。
# Import drive list.
$mappedDrives = Import-Csv C:\Users\########\Desktop\WIP_Scripts\MasterCopy\mappedDrives.csv
$mappedDrives | %{$_ -replace ":"}
foreach ($Name in $mappedDrives) {
New-PSDrive -Name $Name.Name -PSProvider FileSystem -Root "ProviderName" -Persist -ErrorAction Continue
}
一旦我让它工作,我将对导入的来源进行编辑。我目前收到的错误是:
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\#######\Desktop\WIP_Scripts\MasterCopy\ImportMappedDrives.ps1:8 char:5
+ New-PSDrive -Name $Name.Name -PSProvider FileSystem -Root "Provid ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-PSDrive], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.NewPSDriveCommand
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\#######\Desktop\WIP_Scripts\MasterCopy\ImportMappedDrives.ps1:8 char:5
+ New-PSDrive -Name $Name.Name -PSProvider FileSystem -Root "Provid ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-PSDrive], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.NewPSDriveCommand
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\#######\Desktop\WIP_Scripts\MasterCopy\ImportMappedDrives.ps1:8 char:5
+ New-PSDrive -Name $Name.Name -PSProvider FileSystem -Root "Provid ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-PSDrive], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.NewPSDriveCommand
用于检索驱动器信息的脚本:
$mappedDrives = @()
$Name = Get-WmiObject -ClassName Win32_MappedLogicalDisk | Select Name, ProviderName
foreach ($Name in $Name) {
if ($Name. ProviderName) {
$mappedDrives += Select-Object Name, ProviderName -InputObject $Name
}
}
$mappedDrives | Export-Csv mappedDrives.csv
还附加了 mappeddrives.csv 输出在检索时的样子。我认为 csv 文件可能会导致无效的字符争论,因为在 csv 文件中找到的名称包含“:”字符。我也有点困惑它是否能够在 csv 文件中看到“ProviderName”,或者我是否需要声明它以便脚本将其添加到其参数中。再次,我对 Powershell 非常陌生,所以我写下的很多内容都是我从这个网站、微软或其他博客/论坛中找到的,并试图将科学怪人放在一起工作脚本。对于如何改进或使其发挥作用和/或为什么在这种情况下使用另一种方法会更好的任何反馈,我们将不胜感激。
###Revision 1###
利用 RetiredGeek 提供的新脚本
# Import drive list.
$CFSArgs = @{PropertyNames = "Name", "ProviderName"
Delimiter = ','}
$MappedDrives = (Get-Content "G:\BEKDocs\Scripts\Test\mappedDrives.csv") |
ConvertFrom-String @CFSArgs
for ($cnt = 1; $cnt -lt $MappedDrives.count; $cnt++) {
$NPSDArgs =
@{Name = $(($MappedDrives[$cnt].Name).Substring(0,1))
PSProvider = "FileSystem"
Root = "$($MappedDrives[$cnt].ProviderName)"
Persist = $True
ErrorAction = "Continue"
}
New-PSDrive @NPSDArgs
}
我现在收到以下错误:
New-PSDrive : When you use the Persist parameter, the root must be a file system location
on a remote computer.
At C:\Users\######\Desktop\MasterCopy\Test2.ps1:16 char:9
+ New-PSDrive @NPSDArgs
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (":PSDriveInfo) [New-PSDrive], NotSupported
Exception
+ FullyQualifiedErrorId : DriveRootNotNetworkPath,Microsoft.PowerShell.Commands.NewPSD
riveCommand
我现在的两个问题是:
- 对于我想要实现的目标(即使用创建的 cvs 文件将网络驱动器映射到计算机),使用“Net use”而不是“New-PSDrive”会更合适吗?
- 如果使用 New-PSDrive cmdlet 没有问题,我该如何纠正脚本当前输出的错误?
再次感谢 RetiredGeek 和 Theo 的投入。
【问题讨论】:
-
将开关
-NoTypeInformation附加到Export-Csv mappedDrives.csv。这将防止写入导致问题的字段类型描述的顶行。
标签: powershell csv mapping network-drive