Output with timestamp

Quick question. Im looking to add a timestamp to command below. My challenge is that it must be a single command (one liner, as opposed referring to a variable in a previous step) and i cant use double-quotes ("), just single quotes

(Get-WmiObject win32_serverconnection | select username -ExpandProperty username | Sort-Object -Property usernam
e -Unique ) -join ’ - ’ | Out-file C:\User.txt -Append

 

 

 

@(Get-Date) + (Get-WmiObject win32_serverconnection | select username -ExpandProperty username | Sort-Object -Property Username -Unique) -join ' – ' | Add-Content -Path C:\User.txt

But in truth, relying on one liners is often an exercise in unnecessary frustration. :slight_smile:

 

Awesome!

One more (creativity) question with the same requirements as above. So following the same command as above, is it possible to join multiple objects using a one-liner. I assume the answer is no, but asking anyways. Im looking for Username,ShareName,ComputerName. So display as the following:

5/16/2019 12:06:50 PM User1\ShareName\192.168.1.1 - User2\ShareName\192.168.1.2

 

out of curiosity…

exactly where is the requirement for a one-liner coming from?

It sounds like you’d be better off with a CSV file, tbh.

Get-CimInstance win32_serverconnection | Select-Object UserName, ShareName, ComputerName, @{ Name = "Date"; Expression = { Get-Date } } | Sort-Object -Property Username -Unique | Export-Csv -Append C:\User.csv

And personally I wouldn’t leave that on one line. Much more readable and easy to work with if you break it up a bit.

Get-CimInstance -ClassName Win32_ServerConnection |
    Select-Object -Property UserName, ShareName, ComputerName, @{ Name = "Date"; Expression = { Get-Date } } |
    Sort-Object -Property Username -Unique |
    Export-Csv -Path C:\User.csv -Append

Putting everything on one line is not a best practice for script.

It makes them hard to read, debug, maintain and pass on. Just as if one put a whole paragraph in a book on one line of uses unnecessary run-on sentences.

In an interactive session, most of the time, then most certainly.

If your code / comment / example line in a script will not fit the width of a normal 8.5x11 piece of paper, Word doc for example, then it is too long (though there are times when it cannot be avoided), and you need to leverage natural line breaks / continuation and PowerShell splitting
https://devblogs.microsoft.com/scripting/use-splatting-to-simplify-your-powershell-scripts

Having to scroll hundreds of characters across a screen to see code, is just, self-imposed and transferred torture. Just say’in… Yes, I’ve had to deal with that more that I’d like. The longest so-called one-liner some one threw at me to fix / troubleshoot, was literally 300 characters. Needles to say, I had very terse chat with that person. ;-} Will it change them , who knows.

… or even the often maligned backtick (this last one does have its place / use case IMHO).

There is also a real difference between a one liner and putting all your code on one line with a ton of semi-colon use.

References

PowerShellPracticeAndStyle
https://github.com/PoshCode/PowerShellPracticeAndStyle

https://martin77s.wordpress.com/2014/06/17/powershell-scripting-best-practices

Powershell - Recommended coding style
https://lazywinadmin.com/2011/06/powershell-recommended-coding-style.html

What is the recommended coding style for PowerShell?
https://stackoverflow.com/questions/2025989/what-is-the-recommended-coding-style-for-powershell

Thanks Joel. Its working well!

David, the one-liner requirement is because im running a remote utility which executes each line at a time as a new PS process. I could put it all together in a ps1, send it down and run the file, but it involves extra steps.

Postanote, i agree with you. You’re spot on. Unfortunately though, thats a limitation im facing while using this tool