1

我仍然是新手,正在学习创建 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 : Argument,Microsoft.PowerShell.Commands.RenameItemCommand

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

4

1 回答 1

0

我认为问题在于:

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

这不是有效的路径。

于 2019-06-14T15:05:53.887 回答