Philip Loyer
Philip Loyer

Reputation: 768

PowerShell script operating in wrong directory

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:.

Upvotes: 2

Views: 573

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

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"

Upvotes: 3

user6811411
user6811411

Reputation:

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
    }

Upvotes: 1

Related Questions