OUTPUT WRONG ; if you know another form to do the script tell me us...

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:

  • FIRST OUTPUT WRONG
PS C:\Users\Administrador> getstart

InvalidOperation:
Line |
20 | ($view)[0…$viewlenth]
| ~~~~~~~~~~~~~~~~~~~~~~
| Cannot index into a null array.

  • SECOND OUTPUT WRONG
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?

thaks forum:

https://github.com/Krip4us/POWERSHELL/commit/721a9323c3204d225f4cde469d1ac4d7d4ee6abc?diff=split

So you just needed to call renew?