I am trying to run this from my local machine (Win7 & PS v2.0)
cls
$sess = Enter-PSSession -ComputerName blmcrmpoc
Invoke-Command -Session $sess -Scriptblock
{
$path = "C:\inetpub\logs\LogFiles"
$lastWrite = (Get-Date).AddDays(-90)
$oldLogs = Get-ChildItem -path $path -Recurse -Filter *.log | Where {$_.LastWriteTime -le $lastWrite}
if ($oldlogs.count -gt 0)
{foreach ($log in $oldLogs)
{$log.Delete()}}}
But I get this error.
***Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
What am I missing?
Try:
cls
$sess = New-PSSession -ComputerName blmcrmpoc
Invoke-Command -Session $sess -Scriptblock
{
$path = "C:\inetpub\logs\LogFiles"
$lastWrite = (Get-Date).AddDays(-90)
$oldLogs = Get-ChildItem -path $path -Recurse -Filter *.log | Where {$_.LastWriteTime -le $lastWrite}
if ($oldlogs.count -gt 0)
{
foreach ($log in $oldLogs)
{
$log.Delete()
}
}
}
With Enter-PSSession
you ENTER a pssession(you start remoting it so you could write the commands directly as if it were the local machine). If you want to use Invoke-Command on a specific session, you create the session using New-Session
because this creates a session without entering it.