1

I can't seem to find the correct syntax to pass 2 variables from the CALL-script to the execution script in order to have it executed on the remote server. I tried single quotes, double quotes, brackets, .. nothing I can fiind passes the $Target and $OlderThanDays parameters to the script.

Thank you for your help.

The CALL-Script:

#================= VARIABLES ==================================================
$ScriptDir = "\\Server\Scripts"
#================= BODY =======================================================
# Invoke-Command -ComputerName SERVER1 -FilePath $ScriptDir\"Auto_Clean.ps1"         

Invoke-Command -FilePath .\Test.ps1 -ComputerName SERVER01 -ArgumentList {-Target ´E:\Share\Dir1\Dir2´,-OlderThanDays ´10´}

The execution Script:

#================= PARAMETERS =================================================
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$Target,

   [Parameter(Mandatory=$True,Position=2)]
   [string]$OlderThanDays
)

#================= BODY =======================================================
# Set start time & logname
$StartTime = (Get-Date).ToShortDateString()+", "+(Get-Date).ToLongTimeString()
$LogName = "Auto_Clean.log"

# Format header for log
$TimeStamp = (Get-Date).ToShortDateString()+" | "+(Get-Date).ToLongTimeString()+" |"
$Header = "`n$TimeStamp Deleting files and folders that are older than $OlderThanDays days:`n"
Write-Output  "$Header" # to console
Out-File $Target\$LogName -inputobject $Header -Append # to log
# PS 2.0 Workaround (`tee-object -append`) // PS 4.0: `Write-Output "`nDeleting folders that are older than $OlderThanDays days:`n" | Tee-Object $LogFile -Append` 

# Remove files older than
Get-ChildItem -Path $Target -Exclude $LogName -Recurse | 
    Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$OlderThanDays) } | ForEach {
        $Item = $_.FullName
        Remove-Item $Item -Recurse -Force -ErrorAction SilentlyContinue
        $Timestamp = (Get-Date).ToShortDateString()+" | "+(Get-Date).ToLongTimeString()      
        # If folder can't be removed
        if (Test-Path $Item)
            { "$Timestamp | FAILLED: $Item (IN USE)" } 
        else
            { "$Timestamp | REMOVED: $Item" }  
        } | Out-File $Target\$LogName -Append
        # PS 4.0: ´| Tee-Object $Target\$LogName -Append` # Output folder names to console & logfile at the same time

# Remove empty folders   
while (Get-ChildItem $Target -recurse | where {!@(Get-ChildItem -force $_.FullName)} | Test-Path) {
    Get-ChildItem $Target -recurse | where {!@(Get-ChildItem -force $_.FullName)} | Remove-Item
}             

# Format footer
$EndTime = (Get-Date).ToShortDateString()+", "+(Get-Date).ToLongTimeString()
$TimeTaken = New-TimeSpan -Start $StartTime -End $EndTime

Write-Output ($Footer = @"

   Start Time          : $StartTime
   End Time            : $EndTime
   Total Runtime       : $TimeTaken
$("-"*79)
"@)

# Write footer to log
Out-File -FilePath $Target\$LogName -Append -InputObject $Footer

# Clean up variables
$Target=$StartTime=$EndTime=$OlderThanDays = $null

The execution script:

6
  • can you try with -argumentlist @(´E:\Share\Dir1\Dir2´,10) Commented May 15, 2014 at 7:14
  • As you suggested I tried Invoke-Command -FilePath .\Test.ps1 -ComputerName SERVER1 -ArgumentList @(´E:\Share\Dir1\Dir2´,10)but no luck. PowerShell complains about the comma separator. I also tried -ArgumentList @(´E:\Share\Dir1\Dir2´,´10´), but the same problem. Commented May 15, 2014 at 7:20
  • use " instead of ´ Commented May 15, 2014 at 7:24
  • Yes, this does the trick -ArgumentList @("E:\Share\Dir1\Dir2","10"), but it deletes all files instead of taking care of the $OlderThanDaysvariable. Even when I put Write-Output $OlderThanDaysin the execution script, it displays nothing in the console. But Write-Output $Targetdoes give the correct output. Any ideas? Commented May 15, 2014 at 7:32
  • 1
    Fixed it by changing [Parameter(Mandatory=$True)to [Parameter(Mandatory=$True,Position=2) Thank you for your help :) Commented May 15, 2014 at 7:47

1 Answer 1

1

you have got to use " or ' but not ´ :

-argumentlist @('E:\Share\Dir1\Dir2',10)
Sign up to request clarification or add additional context in comments.

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.