• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED Need powershell help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

medo145

Member
Joined
Jun 11, 2004
I have managed to fumble my way through powershell to read a log file based on a search name and return the last entry

Code:
$a = read-host "Enter search"
get-content \\server\logon.log | where {$_ -match "$a"} | select-object -last 1

Write-Host "Press any key to close ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")


the log file logs users logging on and off in this format

Logon ; 10/21/2013 1:28:31 PM ; COMPUTER_NAME ; username; ip_address

searching by username will yield the last line in the log file with that user's information.

I'd like to go one step further by launching remote assistance and inserting the computername.
I can run RA by typing "msra.exe /offerRA <device>"

so I'm thinking

Code:
$a = read-host "Enter search"
get-content \\server\logon.log | where {$_ -match "$a"} | select-object -last 1

Write-Host "Press any key to close ..."
Start-Process msra.exe /offerRA something_here
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

How can I grab the computername from the line and put it into "something_here"?

Thanks
 
Last edited:
I have managed to fumble my way through powershell to read a log file based on a search name and return the last entry

Code:
$a = read-host "Enter search"
get-content \\server\logon.log | where {$_ -match "$a"} | select-object -last 1
 
Write-Host "Press any key to close ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")


the log file logs users logging on and off in this format

Logon ; 10/21/2013 1:28:31 PM ; COMPUTER_NAME ; username; ip_address

searching by username will yield the last line in the log file with that user's information.

I'd like to go one step further by launching remote assistance and inserting the computername.
I can run RA by typing "msra.exe /offerRA <device>"

so I'm thinking

Code:
$a = read-host "Enter search"
get-content \\minos\installs\TestW2k.log | where {$_ -match "$a"} | select-object -last 1
 
Write-Host "Press any key to close ..."
Start-Process msra.exe /offerRA something_here
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

How can I grab the computername from the line and put it into "something_here"?

Thanks

If it were me, I'd put the Get-Content statement into $info

PHP:
 $info = (get-content \\minos\installs\TestW2k.log | where {$_ -match "$a"} | select-object -last 1)

Then use $info.MachineName in the MSRA command statement

PHP:
Start-Process msra.exe /offerRA $info.MachineName

I haven't tested it because I am not allowed to query any of our servers but you should be able to get the gist of it and make the appropriate changes.
 
Thanks, but I don't think you understood my question completely.

Code:
get-content \\server\logon.log | where {$_ -match "$a"} | select-object -last 1

that code finds the last line in the log file related to that user

and it spits out something like this

Code:
Logon ; 10/21/2013 1:28:31 PM ; COMPUTER_NAME ; username; ip_address

I need to somehow have an array that figures out the highlighted part

Code:
Logon ; 10/21/2013 1:28:31 PM ; [COLOR="Red"]COMPUTER_NAME[/COLOR] ; username; ip_address

edit:
I think I figured it out, will have to test when I get to work tomorrow.


Code:
$a = read-host "Enter search"
$b = get-content \\server\logon.log | where {$_ -match "$a"} | select-object -last 1
$c = $b.split(";",[StringSplitOptions]'RemoveEmptyEntries')[2].trim()

Write-Host "Press any key to close ..."
msra.exe /offerRA $c
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

edit2:
It worked, this is my final powershell script if anyone wants to use it for their purposes

Code:
$a = read-host "Enter username"
$b = get-content \\server\logon.log | where {$_ -match "$a"} | select-object -last 1
$c = $b.split(";",[StringSplitOptions]'RemoveEmptyEntries')[2].trim()
$d = $b.split(";",[StringSplitOptions]'RemoveEmptyEntries')[1..4]


Write-Host " "
$d
Write-Host " "
$c
Write-Host " "
Write-Host "Press any key to close ..."
msra.exe /offerRA $c
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
 
Last edited:
Back