1

I´m a newbie still and learning to create PowerShell scripts to make Life in IT easier.

At present I´m trying to build a script, which runs a certain Microsoft tool, scanning defined network shares in a csv file and creating an JSON output file. Now as the pattern of this file is always the same like "Report_Username_Hostname.vba.JSON", I would like to append either the scanned directory name or even a range of numbers, fe. "Report_Username_Hostname(100).vba.JSON" or "Report_Username_Hostname(sharename).vba.JSON"

This is neccessaray as after this renaming step, I upload this and other files within this folder to another folder on different server to upload them into a Database. I planned to run this script in in many different locations on most automatic level and they all copy the their collected files to just one upload folder.

I already tried several options I found somewhere in the deep of the Internet, but I only came to the point where the file was renamed to 0 or similar, but not to expected result.

The Powershell script doing the work is this:

 $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'

the fileremediation_1st.csv looks like

sharefolder
\\server\sharename

Can someone please help me on this, I don´t have a clue what I´m doing wrong. Thank you!

Current error I get is

Rename-Item : Cannot rename the specified target, because it represents a path or device name. At C:\temp\FileRemediation\scripts\fileremediation_V2_1st.ps1:28 char:55 + ... share -Filter '*.JSON' | Rename-Item -NewName {$_.FullName+$uncshare} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand

As said before, I would also be fine with a dedicated range of numbers, which is append to the file name "Report_Username_Hostname(100).vba.JSON" The perfect world would be if I could split the \server\sharename from csv file and append the sharename to my filename.

jrider
  • 1,571
  • 1
  • 10
  • 26
  • Best solution I found so far is Get-ChildItem -Path $localshare -Filter '*.JSON' | Rename-Item -NewName {$_.Name.Replace(".vba","_EU.vba")} but it keeps renaming to endless _EU_EU_EU_EU if I have many files in the folder. – Little_Dutchy Jun 18 '19 at 08:03

1 Answers1

0

I think the issue is with:

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

Your input file (Get-ChildItem) path is:

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

The Rename-Item uses $_.FullName which resolves to something like this:

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

The variables then contain:

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

So Your Rename-Item ... $_.FullName+$uncshare will try to rename it to:

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

Which is not a valid path.

HAL9256
  • 12,384
  • 1
  • 34
  • 46
  • OK, but what would you suggest to change? – Little_Dutchy Jun 18 '19 at 07:10
  • The problem in the error message is that you can't have slashes (e.g. the UNC path) in the filename which is why the rename is failing. So replace `$uncshare` with something else like `"server_sharename_JSON"` – HAL9256 Jun 18 '19 at 15:21