Question

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:

Was it helpful?

Solution

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

-argumentlist @('E:\Share\Dir1\Dir2',10)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top