Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 在复制到特定文件夹之前重命名文件_Powershell_Powershell 4.0_Rename Item Cmdlet - Fatal编程技术网

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

Powershell 在复制到特定文件夹之前重命名文件,powershell,powershell-4.0,rename-item-cmdlet,Powershell,Powershell 4.0,Rename Item Cmdlet,我还是个新手,正在学习创建PowerShell脚本,让生活更轻松 目前,我正在尝试构建一个脚本,它运行某个Microsoft工具,扫描csv文件中定义的网络共享,并创建一个JSON输出文件。 现在,由于该文件的模式始终与“Report\u Username\u Hostname.vba.JSON”相同,我想附加扫描的目录名,甚至是一系列数字,fe。“Report\u Username\u Hostname(100).vba.JSON”或“Report\u Username\u Hostname(

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

目前,我正在尝试构建一个脚本,它运行某个Microsoft工具,扫描csv文件中定义的网络共享,并创建一个JSON输出文件。 现在,由于该文件的模式始终与“Report\u Username\u Hostname.vba.JSON”相同,我想附加扫描的目录名,甚至是一系列数字,fe。“Report\u Username\u Hostname(100).vba.JSON”或“Report\u Username\u 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'
FileU 1st.csv看起来像

sharefolder
\\server\sharename
有人能帮我一下吗?我不知道我做错了什么。 谢谢大家!

当前我得到的错误是

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

如前所述,我也可以使用一个专用的数字范围,它附加到文件名“Report\u Username\u Hostname(100).vba.JSON”中
如果我能将\server\sharename从csv文件中分离出来,并将共享名附加到我的文件名中,那么这将是一个完美的世界。

我认为问题在于:

Rename-Item -NewName {$_.FullName+$uncshare}
您的输入文件(
Get ChildItem
)路径为:

重命名项
使用的
$\u.FullName
解析如下:

C:\temp\FileRemediation\Scripts\..\temp\1st\MyFile.JSON
然后,变量包含:

$_.FullName = C:\temp\FileRemediation\Scripts\..\temp\1st\MyFile.JSON
$uncshare = "\\server\sharename"
因此,您的
重命名项…$\。FullName+$uncshare
将尝试将其重命名为:

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

这不是有效的路径。

我认为问题在于:

Rename-Item -NewName {$_.FullName+$uncshare}
您的输入文件(
Get ChildItem
)路径为:

重命名项
使用的
$\u.FullName
解析如下:

C:\temp\FileRemediation\Scripts\..\temp\1st\MyFile.JSON
然后,变量包含:

$_.FullName = C:\temp\FileRemediation\Scripts\..\temp\1st\MyFile.JSON
$uncshare = "\\server\sharename"
因此,您的
重命名项…$\。FullName+$uncshare
将尝试将其重命名为:

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

哪个路径无效。

好,但您建议更改什么?错误消息中的问题是文件名中不能有斜杠(例如UNC路径),这就是重命名失败的原因。因此,将
$uncshare
替换为其他类似
的“server\u sharename\u JSON”
好,但您建议更改什么?错误消息中的问题是文件名中不能有斜杠(例如UNC路径),这就是重命名失败的原因。因此,将
$uncshare
替换为类似于
的“server\u sharename\u JSON”
到目前为止,我找到的最佳解决方案是Get-ChildItem-Path$localshare-Filter'*.JSON'| Rename-Item-NewName{$\u.Name.replace(“.vba”,“\u EU.vba”)}但如果我在文件夹中有很多文件,它会不断重命名为无尽的{u EU_EU_EU_EU_EU_EU_EU_______________________________)EU。目前为止,我找到的最佳解决方案是Get ChildItem-Path$localshare-Filter'*.JSON'.\Rename Item-NewName{$_.Name.Name.Name.replac。