【问题标题】:Rename file before copy to certain folder在复制到某个文件夹之前重命名文件
【发布时间】:2019-06-14 14:35:52
【问题描述】:

我还是个新手,正在学习创建 PowerShell 脚本以使 IT 生活更轻松。

目前我正在尝试构建一个脚本,该脚本运行某个 Microsoft 工具,扫描 csv 文件中定义的网络共享并创建一个 JSON 输出文件。 现在,由于此文件的模式始终与“Report_Username_Hostname.vba.JSON”相同,我想附加扫描的目录名称甚至是数字范围,fe。 "Report_Username_Hostname(100).vba.JSON" 或 "Report_Username_Hostname(sharename).vba.JSON"

这是必要的,因为在此重命名步骤之后,我将此文件夹中的此文件和其他文件上传到不同服务器上的另一个文件夹,以将它们上传到数据库中。 我计划在大多数自动级别上在许多不同的位置运行此脚本,他们都将收集的文件复制到一个上传文件夹。

我已经尝试了几个我在互联网深处找到的选项,但我只是到了文件被重命名为 0 或类似的点,但没有达到预期的结果。

执行这项工作的 Powershell 脚本是这样的:

 $PSpath = 'C:\temp\FileRemediation\Scripts\'
 $Rpath = $PSpath +'..\Reports\1st'
 $localshare = $PSpath +'..\temp\1st'
 $csvinputs = Import-Csv $PSpath\fileremediation_1st.csv
 $uploadshare = '\\PGC2EU-WFSFR01.eu1.1corp.org\upload\'

# This section checks if the folder for scan reports is availabe and if not will create necessary folder.
If(!(test-path $Rpath))
{
      New-Item -ItemType Directory -Force -Path $Rpath
} 
If(!(test-path $localshare))
{
      New-Item -ItemType Directory -Force -Path $localshare
}
Set-Location $Rpath

# This section reads input from configuration file and starts Ms ReadinessReportCreator to scan shares in configuration file. 
ForEach($csvinput in $csvinputs)
{
    $uncshare = $csvinput.sharefolder

    $Executeable = Start-Process "C:\Program Files (x86)\Microsoft xxx\xxx.exe" `
    -Argumentlist "-p ""$uncshare""", "-r", "-t 10000", "-output ""$localshare"""`
    -Wait
   Get-ChildItem -Path $localshare -Filter '*.JSON' | Rename-Item     -NewName {$_.FullName+$uncshare}
}
# This section copies the output *.JSON file of the xxx to the share, where I they will be uploaded to DB.        
Get-ChildItem -Path $localshare -Filter '*.JSON' | Where {$_.Length -ge 3} | move-item -Destination '$uploadshare'

fileremediation_1st.csv 看起来像

sharefolder
\\server\sharename

有人可以帮我解决这个问题,我不知道我做错了什么。 谢谢!

我得到的当前错误是

Rename-Item : 无法重命名指定的目标,因为它 表示路径或设备名称。在 C:\temp\FileRemediation\scripts\fileremediation_V2_1st.ps1:28 char:55 + ... 分享 -Filter '*.JSON' |重命名项目 -NewName {$_.FullName+$uncshare} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException + FullyQualifiedErrorId : 参数,Microsoft.PowerShell.Commands.RenameItemCommand

如前所述,我也可以使用专用的数字范围,将其附加到文件名“Report_Username_Hostname(100).vba.JSON” 如果我可以从 csv 文件中拆分 \server\sharename 并将共享名附加到我的文件名中,那么完美的世界将是。

【问题讨论】:

  • 目前我发现的最佳解决方案是 Get-ChildItem -Path $localshare -Filter '*.JSON' | Rename-Item -NewName {$_.Name.Replace(".vba","_EU.vba")} 但如果文件夹中有很多文件,它会不断重命名为无穷无尽的 _EU_EU_EU_EU。

标签: powershell powershell-4.0 rename-item-cmdlet


【解决方案1】:

我认为问题在于:

Rename-Item -NewName {$_.FullName+$uncshare}

您的输入文件 (Get-ChildItem) 路径是:

$PSpath = 'C:\temp\FileRemediation\Scripts\'
$localshare = $PSpath +'..\temp\1st'

Rename-Item 使用 $_.FullName 解析为如下内容:

C:\temp\FileRemediation\Scripts\..\temp\1st\MyFile.JSON

然后变量包含:

$_.FullName = C:\temp\FileRemediation\Scripts\..\temp\1st\MyFile.JSON
$uncshare = "\\server\sharename"

所以您的Rename-Item ... $_.FullName+$uncshare 将尝试将其重命名为:

C:\temp\FileRemediation\Scripts\..\temp\1st\MyFile.JSON\\server\sharename

这不是一个有效的路径。

【讨论】:

  • 好的,但是你建议改变什么?
  • 错误消息中的问题是文件名中不能有斜杠(例如 UNC 路径),这就是重命名失败的原因。所以把$uncshare换成"server_sharename_JSON"之类的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多