I am running a short script that will go through folders and delete log files older than 14 days on a remote computer:
param(
[Parameter(Mandatory=$true)]
[string] $remoteHost = $args[0],
[boolean] $run = $args[1]
)
$rootFolder = '\d$\Projects\';
$folders = 'Folders1\logs\','Folders2\logs\','Folders3\logs\';
foreach($folder in $folders){
$fullPath = ('\\' + $remoteHost + $rootFolder + $folder + '*.log*');
if($run){
Get-ChildItem $fullPath | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-15)} | Remove-Item -ErrorAction SilentlyContinue;
}
else{
Get-ChildItem $fullPath | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-15)} | Remove-Item -ErrorAction SilentlyContinue -WhatIf;
}
}
But, when running the script I am getting the error shown below. Is it because it may be going through some of those folders and is not seeing any files older than 14 days?
cmdlet at command pipeline position 1
Supply values for the following parameters:
remoteHost: PRLVWEPMSAP04
Cannot index into a null array.
+ CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : NullArray