Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


xpd

xpd

Geek of Coastguard
14116 posts

Uber Geek
+1 received by user: 4579

Retired Mod
ID Verified
Trusted
Lifetime subscriber

#296143 25-May-2022 07:22
Send private message

Just throwing around an idea for something that could come in handy at work at times, need some other views / input etc.... 

 

 

 

I want to be able to have a script sitting on a network drive that users can access, that will run a Windows command, save the output to a txt file, then email that txt file to me with some sort of identifier of the user.

 

Example : Bob runs script which runs a ping test and outputs to myping.txt then emails myping.txt to mypingtest@xpd which appears to be from bob@xpd

 

 

 

My main issue is the identifier - as I'm typing this, Im thinking Powershell might be able to do this for me, make a call to obtain the logged in users name and attach that somewhere to the txt file or email.......

 

 

 

Overall it needs to run without any other requirements, any Windows install must be able to run it.....

 

 





XPD / Gavin

 

LinkTree

 

 

 


Create new topic
bagheera
544 posts

Ultimate Geek
+1 received by user: 189


  #2918240 25-May-2022 08:22
Send private message

power shell the way to go, and use the dos variable %USERNAME%@xpd will use the logged-in user - as long as it is not like default user or admin, if it is then variable %COMPUTERNAME% would be a better one to use.

 

 

 

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.2

 

 

 

so

 

$user = $Env:USERNAME

 

$computer = $Env:COMPUTERNAME

 

 

 

would get you the user & computer name for the powershell




SirHumphreyAppleby
2939 posts

Uber Geek
+1 received by user: 1860


  #2918256 25-May-2022 09:37
Send private message

Easy to do with a batch file or PowerShell. I use CMail to send the actual e-mail (must be somewhere in the PATH or in the same directory as the script), but it can be done directly from within PowerShell, it's just more work.

 

When running a batch command, the username is available in the %username% environment variable. If you type 'set' on the command prompt you will see a list of common variables in the usual environment, such as the computer name.

 

At its simplest, you could just do this...

 

ping_command > myping.txt
cmail -host:mail.example.com -to:mypingtest@xpd -from:%username%@xpd -a:myping.txt
del myping.txt

 

If you want the results in the message body, it's even simpler...

 

ping_command | cmail -host:mail.example.com -to:mypingtest@xpd -from:%username%@xpd --

 

Note, I am biased since I wrote CMail. Older builds will run on Windows versions as old as NT 4. Newer builds require Vista due to LibreSSL dropping support for older platforms.


Lias
5655 posts

Uber Geek
+1 received by user: 3978

ID Verified
Trusted
Lifetime subscriber

  #2918270 25-May-2022 10:06
Send private message

Something like this should work:

 

 

$To = "xpd@work.com"
$From = "noreply@work.com"
$Server = "smtp.work.com"

 

$Ping = Test-NetConnection www.google.com

 

$Subject = "Test from $($env:COMPUTERNAME) at $(Get-Date)"
$Body = "Dear $To`r`n"
$Body = $Body + "`r`n"
$Body = $Body + "This is a test email sent from $($env:COMPUTERNAME) at $(Get-Date)`r`n"
$Body = $Body + "`r`n"
$Body = $Body + "It was sent via the following SMTP server: $Server`r`n"
$Body = $Body + "`r`n"
$Body = $Body + "Results`r`n"
$Body = $Body + "Ping Target: " + $Ping.ComputerName + "`r`n"
$Body = $Body + "Ping Address: " + $Ping.RemoteAddress + "`r`n"
$Body = $Body + "Ping Succeeded:" + $Ping.PingSucceeded + "`r`n"
$Body = $Body + "Ping RoundTripTime: " + $ping.PingReplyDetails.RoundtripTime + "`r`n"
$Body = $Body + "`r`n"

 

Send-MailMessage -Body $Body -Subject $Subject -To $To -From $From -SmtpServer $Server

 





I'm a geek, a gamer, a dad, a Quic user, and an IT Professional. I have a full rack home lab, size 15 feet, an epic beard and Asperger's. I'm a bit of a Cypherpunk, who believes information wants to be free and the Net interprets censorship as damage and routes around it. If you use my Quic signup you can also use the code R570394EKGIZ8 for free setup. Opinions are my own and not the views of my employer.


Create new topic








Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.