【问题标题】:Using CVS file to map network drives - PowerShell使用 CVS 文件映射网络驱动器 - PowerShell
【发布时间】: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 Output

还附加了 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

我现在的两个问题是:

  1. 对于我想要实现的目标(即使用创建的 cvs 文件将网络驱动器映射到计算机),使用“Net use”而不是“New-PSDrive”会更合适吗?
  2. 如果使用 New-PSDrive cmdlet 没有问题,我该如何纠正脚本当前输出的错误?

再次感谢 RetiredGeek 和 Theo 的投入。

【问题讨论】:

  • 将开关-NoTypeInformation 附加到Export-Csv mappedDrives.csv。这将防止写入导致问题的字段类型描述的顶行。

标签: powershell csv mapping network-drive


【解决方案1】:

金融时报,

您需要在 New-PSDrive 行中评估您的论点。 在那里使用 Substring 消除了代码并提高了效率。 我在使用 Import-CSV 时遇到问题,所以我切换到 Get-Content 并进行了调整

# 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)" 
        Scope       = "Global"
        Persist     = $True
        ErrorAction = "Continue"
                }
        New-PSDrive @NPSDArgs -WhatIf
}

我使用了 -WhatIf 参数,因为我没有你的目标可用,但它显示了应该做什么。

输出:

What if: Performing the operation "New drive" on target "Name: X Provider: Micro
soft.PowerShell.Core\FileSystem Root: \\hapi\desktop$\Decommission Log".
What if: Performing the operation "New drive" on target "Name: Y Provider: Micro
soft.PowerShell.Core\FileSystem Root: \\gonzo\Temp\AZ".
What if: Performing the operation "New drive" on target "Name: Z Provider: Micro
soft.PowerShell.Core\FileSystem Root: \\gonzo\Temp\001".

PS> 

更新 1: 在我的网络(Peer-to-Peer)上的进一步测试表明,添加 Scope 参数(见上文)将创建映射,即使您收到相同的消息,它也会持续到您重新启动!但是,它不会在重新启动后持续存在,因此它没有做它应该做的事情。我仍然不明白为什么该消息显示为根在另一台计算机上。此外,虽然我可以打开命令提示符并成功在驱动器上执行 DIR,但映射不会显示在文件资源管理器中。

更新 2: 我尝试了另一个映射到我的 Synology NAS 的测试,它在没有错误消息的情况下工作。但是,唉,它并没有坚持重新启动!

【讨论】:

  • 感谢您的输入 RG。当我使用您提供的代码运行时(删除 -WhatIf 参数并更改 $mappedDrives 中的文件路径),我收到错误“New-PSDrive:当您使用 Persist 参数时,根必须是远程文件系统位置计算机”在 New-PSDrive @NPSDArgs 行。我现在想问的问题是,New-PSDrive cmdlet 是否适合我正在尝试做的事情(映射网络驱动器),还是应该将命令切换到网络使用?如果是这样,我将如何修改脚本以利用 Net Use?
  • Foxtrot360,查看修改后的答案。
  • 添加了范围参数,但仍然收到相同的错误。使用 -WhatIf 参数运行并确实返回了一些有趣的东西:((如果:在目标“名称:”提供者:Microsoft.PowerShell.Core\FileSystem Root:“\\gonzo\temp”上执行“新驱动器”操作。 )) 似乎它没有从创建的 csv 文件中获取驱动器号“名称”。这会导致持久参数错误吗?
  • 另外,点源脚本能否解决重启后脚本不存在的问题?
  • Foxtrot360,很抱歉,但我觉得我的工资等级有点高!希望有更多知识的其他人能加入进来。祝你好运!
猜你喜欢
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 2019-11-21
  • 2011-06-08
  • 2021-02-23
  • 1970-01-01
相关资源
最近更新 更多