Welcome › Forums › General PowerShell Q&A › Getting newest file info from remote computer (Invoke or PSSession)
- This topic has 6 replies, 2 voices, and was last updated 1 month ago by
Participant.
-
AuthorPosts
-
-
December 15, 2020 at 12:39 pm #279930
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?
PowerShell1234567$Dir = "C:\logs"$Latest = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1$Name = $Latest.name$Creation = $Latest.CreationTime$Modified = $Latest.LastWriteTimeWrite-Host "The newest file is '$Name' which was created on $Creation and last modified on $Modified."-
This topic was modified 1 month ago by
ALombardi01. Reason: Crayon formatting jacked up
-
This topic was modified 1 month ago by
-
December 15, 2020 at 1:14 pm #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.
-
December 15, 2020 at 1:19 pm #279945
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.
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.
-
December 15, 2020 at 1:23 pm #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$.
-
December 15, 2020 at 1:36 pm #279954
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$.
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.
-
-
December 15, 2020 at 1:41 pm #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}
-
December 15, 2020 at 2:12 pm #279963
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}
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.
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.