powershell 7.0 core
my script:
function date{
(Get-Date).ToString("dd.MM.yyyy/HH:mm:ss");
}
function renew {
date >>"LastAccessTime"
Write-Host "file lastaccesstime renew!!!" -ForegroundColor Red
}
function view{
Get-Content "LastAccessTime" -ErrorAction SilentlyContinue
}
function getstart{
$date = date
$view = view
$viewlenth = (view).length
($view)[0..$viewlenth]
}
getstart
OUTPUT:
PS C:\Users\Administrador> getstart
InvalidOperation:
Line |
20 | ($view)[0…$viewlenth]
| ~~~~~~~~~~~~~~~~~~~~~~
| Cannot index into a null array.
PS C:\Users\Administrador> getstart
2
2
.
0
4
.
2
0
2
0
/
2
0
:
5
7
:
5
9
2
2
.
0
4
.
2
0
2
0
/
2
0
:
5
7
:
5
9
YOU CAN CHECK THE ERRORS DOWNLOADING THE SCIPT IN GITHUB :
https://github.com/Krip4us/POWERSHELL/blob/master/Interactive%20Console.ps1
Can you let us know what did you really intent by below expression.
Get-Content "LastAccessTime"
Above line will expect a file named “LastAccessTime” in the present working directory where the script runs. If the file doesn’t exists it wil throw exception, but you are ignoring the exception with ErrorAction set to SilentlyContinue
At what point do you call renew? It seems like you’re trying to output the data to a file with no extension named “LastAccessTime” using output redirect >>. If that’s the case, renew would need to be called before view - to create this file. I don’t understand why you’re outputting it to a file just to read it back in, versus just saving it to a variable. It seems you’re just missing the call to renew?
So you just needed to call renew?