在复制到特定文件夹之前重命名文件

时间:2019-06-14 14:35:52

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

我还是一个新手,正在学习创建PowerShell脚本来简化IT生活。

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

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

我已经尝试了在Internet深处发现的几种选项,但是我只是到了将文件重命名为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”之后 完美的世界是,如果我可以将\ server \ sharename从csv文件中拆分出来,并将共享名附加到我的文件名之后。

1 个答案:

答案 0 :(得分:0)

我认为问题在于:

onclick=\"openModal(this)\"

您的输入文件(using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication114 { class Program { static void Main(string[] args) { string input = "CREATE TABLE Tablex(" + " column1 INT," + " column2 TEXT," + " column3 INT," + " column4 INT," + ")"; string pattern1 = @"CREATE TABLE Tablex\((?'columns'[^)]+)\)"; Match columnsMatch = Regex.Match(input, pattern1); string columns = columnsMatch.Groups["columns"].Value.Trim(); string pattern = @"\s*(?'key'[^\s]+)\s+(?'value'\w+)[,$]"; MatchCollection matches = Regex.Matches(columns, pattern); foreach(Match match in matches.Cast<Match>()) { Console.WriteLine("Key : '{0}', Value : '{1}'", match.Groups["key"].Value, match.Groups["value"].Value); } Console.ReadLine(); } } } )路径为:

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"

这不是有效路径。