Getting newest file info from remote computer (Invoke or PSSession)

A computer that should be creating a log file wasn’t and hadn’t been for a month or so. I’d like to be able to run something like the below within Invoke-Command so I don’t need to log in to find out if it’s running but I don’t seem to be able to no matter how I seem to modify it. Would PSSession work better for this?

 

$Dir = "C:\logs"
$Latest = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1
$Name = $Latest.name
$Creation = $Latest.CreationTime
$Modified = $Latest.LastWriteTime

Write-Host "The newest file is '$Name' which was created on $Creation and last modified on $Modified."

 

what happens if you change this:

$Dir = ‘c:\logs’

To this:

$dir = “\$computer\c$\logs”

You would of course need to define $computer as the remote computer. I am unable to test, but my thought is using this method would only require admin on the remote system and you dont have to mess with remote powershell.

[quote quote=279942]what happens if you change this:

$Dir = ‘c:\logs’

To this:

$dir = “\$computer\c$\logs”

You would of course need to define $computer as the remote computer. I am unable to test, but my thought is using this method would only require admin on the remote system and you dont have to mess with remote powershell.

[/quote]

Doesn’t work. I tried it whether or not the directory is shared or not, and all I get is Access Denied. I’m using a Domain Admin account too to test.

C$ is always shared by default, but not accessible by default. There are changes you can make to get this to work (GPO), but that of course increases the security risk. Not sure which will be easier, getting PSRemoting configured or access to C$.

[quote quote=279948]C$ is always shared by default, but not accessible by default. There are changes you can make to get this to work (GPO), but that of course increases the security risk. Not sure which will be easier, getting PSRemoting configured or access to C$.

[/quote]

Yeah. I wouldn’t open things up. I may need to just move the files from C: as I believe this specific server has a D: share for something else. May just need it to end up there and I may be able to do it that way instead.

You mention “server” … typically, PSRemoting is enabled on Servers. Have you run any tests to check the server as in Test-WSMan?

If that looks good, change your script to this (again, I am unable to test)

$Latest = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-ChildItem -Path $Using:dir | Sort-Object CreationTime -Descending | Select-Object -First 1}

[quote quote=279957]You mention “server” … typically, PSRemoting is enabled on Servers. Have you run any tests to check the server as in Test-WSMan?

If that looks good, change your script to this (again, I am unable to test)

$Latest = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-ChildItem -Path $Using:dir | Sort-Object CreationTime -Descending | Select-Object -First 1}

[/quote]

This is basically working as I want. Whatever I had my Invoke-command set up as originally wasn’t working, so I posted here, thinking it wasn’t allowed to get the information.

You solved my answer. Thanks.