【发布时间】: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