2

I have a script that works correctly on other servers, however on this one server it is operating in the parent directory of where the script is supposed to run. It is only on this one machine and works correct else where.

Script:

Param (
    [Parameter(Mandatory=$true)][string]$destinationRoot,
    [string]$localPath
)

Get-ChildItem $localPath\* -Include *.bmp, *.svg |
    Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} |
    ForEach-Object {
        $content = $localPath + "\" + $_.Name

        $year = (Get-Item $content).LastWriteTime.Year.ToString()
        $monthNumber = (Get-Item $content).LastWriteTime.Month
        $month = (Get-Culture).DateTimeFormat.GetMonthName($monthNumber)

        $destination = $destinationRoot + "\" + $year + "\" + $month 

        New-Item -ItemType Directory -Force -Path $destination

        Move-Item -Path $content -Destination $destination -Force
    }

Here is the execution statement from the CMD prompt:

powershell -File "C:\L1_Vision_Images\MoveFiles.ps1" -destinationRoot "\\OB-VM-ME-Data\ME-Data\Archived\LEDTools\MT-1\L1Images\" -localPath "C:\L1_Vision_Images"

Instead of copying the contents in the L1_Vision_Images directory it scans the root of C:.

2 Answers 2

3

The trailing backslash in the argument for the parameter -destinationRoot escapes the closing double quote, meaning that instead of \\OB-VM-ME-Data\ME-Data\Archived\LEDTools\MT-1\L1Images\ you're passing \\OB-VM-ME-Data\ME-Data\Archived\LEDTools\MT-1\L1Images" -localPath C:\admin\scripts.

Simply checking your parameter values in a debugger or outputting them at the beginning of the script (e.g. via Write-Host $destinationRoot) would have revealed that to you.

Remove the trailing backslash from that argument (it's not needed since you're appending a backslash when defining $destination anyway) and the problem will disappear.

powershell -File "C:\L1_Vision_Images\MoveFiles.ps1" -destinationRoot "\\OB-VM-ME-Data\ME-Data\Archived\LEDTools\MT-1\L1Images" -localPath "C:\L1_Vision_Images"
Sign up to request clarification or add additional context in comments.

1 Comment

I did see this actually, but the problem isnt with the destination, its the local path that is have issues
1

You can streamline your script in various points,

  • $content is the same as $_.FullName,
  • $_ is already present as an object with all properties,
    so no need to several times invoke Get-Item for LastWriteTime
  • you can build the \year\monthname folder by using .ToString('\\yyyy\\MMMM')
    (escaping the backslash with another one to have it literal)

Param (
    [Parameter(Mandatory=$true)][string]$destinationRoot,
    [string]$localPath
)

Get-ChildItem $localPath\* -Include *.bmp, *.svg |
    Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} |
    ForEach-Object {
        $destination = Join-Path $destinationRoot `
                       $_.LastWriteTime.ToString("\\yyyy\\MMMM")
        New-Item -ItemType Directory -Force -Path $destination
        $_ | Move-Item -Destination $destination -Force
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.