How to open a webpage from powershell once a file is logged with a particular string? - powershell-2.0

I have to open 2 URLs from powershell. Each creates a log file. Once the log file of the 1st URL is complete, the 2nd URL has to be launched. The completion of the file is indicated by a string "end of log". Each log takes a minimum of 15 minutes. Since the duration is not certain, I didn't use sleep commanlet.

Ok so its rather simple, You use Select-String to search a text file for a string. You do this in a while block so while Select-String returns false (-Quiet makes the cmdlet return a bool true or false if the value exists in the file) Wait the amount of time and then check again.
While ($(Select-String -Path 'C:\Logs\File1.txt' -Pattern 'end of log' -quiet) -eq $False)
{
Write-Host "No end of log in file 1 waiting.."
Sleep -Seconds 3
}
Since your first script takes 15minutes to finish, You might want to make it wait longer between checks since if its to frequent it might slow down the first script.

Related

Way to get some sort of schedule in TCL without blocking on-going code

I need some sort of schedule thing to schedule a task to happen at x:y (12:00 for example) in Tcl.
The scenario is a router using Openwrt with Tcl 8.6.10 with limited RAM and storage where I have some sort of IRC client "bot" (using socket to connect). The "bot" was just a barebone that I modify to suit my needs. Most of the things work fine, except that I don't have way to schedule easily things. I wanted something like how eggdrop has "bind time" where the bind thing is "bind time flag "cron-style string" caller".
The "bot" scheme is like:
Main Tcl script:
<info+code to connect to IRC>
<while loop>
<some code in case of IRC disconnection>
<list of files with tcl code aka sub-scripts>
<usage of source based from a list of the filenames>
<code for error handling>
<end of while loop>
The list of files is source filelist.tcl, where filelist.tcl is a set var {filename1.tcl filename2.tcl...}. The filenamex.tcl has some basic code to respond to IRC server or IRC input from channels and reply to channels.
I can make some sort of schedule if I base a execution like if {[clock format [clock seconds] -format "%H:%M"]=="12:00"} {code to execute} and hopefully wait for a server ping/pong but that can lead to repeated code inside of the if body.
I been looking around and found a package called cron but I don't know how to use it correctly because there are not many examples and I don't know to use vwait properly and I don't want vwait to hang the bot waiting for a value to change. I also read about tcl threads for maybe parallel execution.
So I need some code inside of a sub-script that looks like (a package cron style):
#beginning of file
#add a task specifying hour and minute
task-at "12:00" proccaller
proc procname {optional} {
<some code to be executed at specific hour+time>
}
#end of file
I also don't know how to use after command to use it.
How can I accomplish I want?
Thanks for the replies and yes, it would help if I study event loops and coroutine, which probably comes next.
Some time has passed since I posted the question and kinda sorted the thing by creating a sub-script in a folder named scripts with the following structure:
#beginning of the script
if {![file exists executed]} {set executed "no"}
#the following clock instruction returns for example: Tuesday 22:14
switch -glob -- [clock format [clock seconds] -format "%A %H:%M"] {
"*12:00" - "*12:01" {
#Basic example of sending a message to the irc channel when it's midday
if {$executed=="no"} {
puts $fd "PRIVMSG #CODE :It's midday right now."
flush $fd
set executed "yes"
}
}
#...more time comparisions and code
default {set executed "no"}
}
#end of script
And the script is almost the top of the list of scripts to be loaded so if I wish to send some command down stream at giving time, the command can be executed.
There is double timings because the "bot" reacts, at least at minimum, to the irc server's ping which happens each 90 seconds and it may skip some minutes.
This is not an answer but an unproper workaround.

Simple program that reads and writes to a pipe

Although I am quite familiar with Tcl this is a beginner question. I would like to read and write from a pipe. I would like a solution in pure Tcl and not use a library like Expect. I copied an example from the tcl wiki but could not get it running.
My code is:
cd /tmp
catch {
console show
update
}
proc go {} {
puts "executing go"
set pipe [open "|cat" RDWR]
fconfigure $pipe -buffering line -blocking 0
fileevent $pipe readable [list piperead $pipe]
if {![eof $pipe]} {
puts $pipe "hello cat program!"
flush $pipe
set got [gets $pipe]
puts "result: $got"
}
}
go
The output is executing go\n result:, however I would expect that reading a value from the pipe would return the line that I have sent to the cat program.
What is my error?
--
EDIT:
I followed potrzebie's answer and got a small example working. That's enough to get me going. A quick workaround to test my setup was the following code (not a real solution but a quick fix for the moment).
cd /home/stephan/tmp
catch {
console show
update
}
puts "starting pipe"
set pipe [open "|cat" RDWR]
fconfigure $pipe -buffering line -blocking 0
after 10
puts $pipe "hello cat!"
flush $pipe
set got [gets $pipe]
puts "got from pipe: $got"
Writing to the pipe and flushing won't make the OS multitasking immediately leave your program and switch to the cat program. Try putting after 1000 between the puts and the gets command, and you'll see that you'll probably get the string back. cat has then been given some time slices and has had the chance to read it's input and write it's output.
You can't control when cat reads your input and writes it back, so you'll have to either use fileevent and enter the event loop to wait (or periodically call update), or periodically try reading from the stream. Or you can keep it in blocking mode, in which case gets will do the waiting for you. It will block until there's a line to read, but meanwhile no other events will be responded to. A GUI for example, will stop responding.
The example seem to be for Tk and meant to be run by wish, which enters the event loop automatically at the end of the script. Add the piperead procedure and either run the script with wish or add a vwait command to the end of the script and run it with tclsh.
PS: For line-buffered I/O to work for a pipe, both programs involved have to use it (or no buffering). Many programs (grep, sed, etc) use full buffering when they're not connected to a terminal. One way to prevent them to, is with the unbuffer program, which is part of Expect (you don't have to write an Expect script, it's a stand-alone program that just happens to be included with the Expect package).
set pipe [open "|[list unbuffer grep .]" {RDWR}]
I guess you're executing the code from http://wiki.tcl.tk/3846, the page entitled "Pipe vs Expect". You seem to have omitted the definition of the piperead proc, indeed, when I copy-and-pasted the code from your question, I got an error invalid command name "piperead". If you copy-and-paste the definition from the wiki, you should find that the code works. It certainly did for me.

Parse through text file and write out data

I'm working on the first steps towards creating a powershell script that will read through printer logs (probably using get-WMI cmdlet), and parse through the logs. Afterwards, I plan on having the script output to a .txt file the name of the printer, a counter of the number of times a printer was used (if possible), and specific info found in the logs.
In order to do this, I've decided to try working backwards. Below is a small portion of what the logs will look like:
10 Document 81, A361058/GPR0000151814_1: owned by A361058 was printed on R3556 via port IP_***.***.***.***. Size in bytes: 53704; pages printed: 2 20130219123105.000000-300
10 Document 80, A361058/GPR0000151802_1: owned by A361058 was printed on R3556 via port IP_***.***.***.***. Size in bytes: 53700; pages printed: 2
Working backwards and just focusing on parsing first, I'd like to be able to specifically get the "/GRP", "R3446 (in general, R** as this is the printer name)", and get a counter that shows how often a specific printer appeared in the log files.
It has been a while since I last worked with Powershell, however at the moment this is what I've managed to create in order to try accomplishing my goal:
Select-String -Path "C:\Documents and Settings\a411882\My Documents\Scripts\Print Parse Test.txt" -Pattern "/GPR", " R****" -AllMatches -SimpleMatch
The code does not produce any errors, however I'm also unable to get any output to appear on screen to see if I'm capturing the /GRP and printer name. At the moment I'm trying to just ensure I'm gathering the right output before worrying about any counters. Would anyone be able to assist me and tell me what I'm doing wrong with my code?
Thanks!
EDIT: Fixed a small error with my code that was causing no data to appear on screen. At the moment this code outputs the entire two lines of test text instead of only outputting the /GPR and server name. The new output is the following:
My Documents\Scripts\Print Parse Test.txt:1:10 Document 81, A361058/GPR0000151814_1: owned by A361058 was printed on
R3556 via port IP_***.***.***.***. Size in bytes: 53704; pages printed: 2
20130219123105.000000-300
My Documents\Scripts\Print Parse Test.txt:2:10 Document 80, A361058/GPR0000151802_1: owned by A361058 was printed on
R3556 via port IP_***.***.***.***. Size in bytes: 53700; pages printed: 2
I'd like to try having it eventually look something like the following:
/GPR, R****, count: ## (although for now I'm less concerned about the counter)
You can try this. It only returns a line when /GPR (and "on" from "printed on") is present.
Get-Content .\test.txt | % {
if ($_ -match '(?:.*)(/GPR)(?:.*)(?<=on\s)(\w+)(?:.*)') {
$_ -replace '(?:.*)(/GPR)(?:.*)(?<=on\s)(\w+)(?:.*)', '$1,$2'
}
}
Output:
/GPR,R3556
/GPR,R3556
I'm sure there are better regex versions. I'm still learning it :-)
EDIT this is easier to read. The regex is still there for extraction, but I filter out lines with /GPR first using select-string instead:
Get-Content .\test.txt | Select-String -SimpleMatch -AllMatches -Pattern "/GPR" | % {
$_.Line -replace '(?:.*)(/GPR)(?:.*)(?<=on\s)(\w+)(?:.*)', '$1,$2'
}
I generally start with an example of the line I'm matching, and build a regex from that, substituting regex metacharacters for the variable parts of the text. This makes makes the regex longer, but much more intuitive to read later.
Assign the regex to a variable, and then use that variable in subsequent code to keep the messy details of the regex from cluttering up the rest of the code:
[regex]$DocPrinted =
'Document \d\d, \w+/(\D{3})[0-9_]+: owned by \w+ was printed on (\w+) via port IP_[0-9.]+ Size in bytes: \d+; pages printed: \d+'
get-content <log file> |
foreach {
if ($_ -match $DocPrinted)
{
$line -match $docprinted > $null
$matches
}
}

TCL - How to print on the screen th messages that where printed during execution of exec command?

Say I have want to execute a script or and executable file by printing runtime the output of execution.
When I do:
set log [exec ./executable_file]
puts $log
Then it waits a long time and then prints everything at once. But I want runtime printing. How can I do this?
Not perfect (as it require writing to external file):
set log [exec executable_file | tee log.txt >#stdout]
The output will be displayed immediately, at the same time, saved to 'log.txt'. If you don't care about saving the output:
set log [exec executable_file >#stdout]
Use open "| ..." and asyncronous linewise reading from the returned descriptor, like this:
proc ReadLine fd {
if {[gets $fd line] < 0} {
if {[chan eof $fd]} {
chan close $fd
set ::forever now
return
}
}
puts $line
}
set fd [open "| ./executable_file"]
chan configure $fd -blocking no
chan event $fd readable [list ReadLine $fd]
vwait forever
See this wiki page for more involved examples.
In a real program you will probably already have an event loop running so there would be no need for a vwait specific to reading the output of one command.
Also if you need to collect the output, not just [puts] each line after it has been read, you will pobably need to create a global (usually namespaced) variable, initialize it to "", pass its name as another argument to the callback procedure (ReadLine here) and append the line to that variable's value.
May be you can launch another process in background before your executable, like tail -f logfile.txt, and outputting the results of your executable in this logfile.txt ?

Windows command to get service status?

I need to know the status of a service at the end of my batch script which restarts services using "net stop thingie" and "net start thingie".
In my most favorite ideal world, I would like to e-mail the state to myself, to read on cold winter nights, to reassure myself with the warmth and comfort of a server that I know is running right.
Just for you to know, I'm using a Windows server 2003 platform, and a batch file seemed the best choice. I don't mind using something else, and would be very open to suggestions, but just for the sake of knowledge (as a zombie craves brains, I thought, why not inflate my own), is there a command that allows me to check on the status of a service, in command line?
Should I just redirect the output of the command to a file?
Where the hell are my pants? (Gosh, I really do hope the humor inserted in this will not insult anyone. It's Wednesday morning, and humor I do need too :P)
[Edit:] The solution I used is (no longer) available for download from --link redacted--
It is used as a task set to be executed during the night, and checking my e-mail in the morning, I see whether or not the service has correctly restarted.
Have you tried sc.exe?
C:\> for /f "tokens=2*" %a in ('sc query audiosrv ^| findstr STATE') do echo %b
4 RUNNING
C:\> for /f "tokens=2*" %a in ('sc query sharedaccess ^| findstr STATE') do echo %b
1 STOPPED
Note that inside a batch file you'd double each percent sign.
You can call net start "service name" on your service. If it's not started, it'll start it and return errorlevel=0, if it's already started it'll return errorlevel=2.
Using pstools - in particular psservice and "query" - for example:
psservice query "serviceName"
look also hier:
NET START | FIND "Service name" > nul
IF errorlevel 1 ECHO The service is not running
just copied from:
http://ss64.com/nt/sc.html
If PowerShell is available to you...
Get-Service -DisplayName *Network* | ForEach-Object{Write-Host $_.Status : $_.Name}
Will give you...
Stopped : napagent
Stopped : NetDDE
Stopped : NetDDEdsdm
Running : Netman
Running : Nla
Stopped : WMPNetworkSvc
Stopped : xmlprov
You can replace the ****Network**** with a specific service name if you just need to check one service.
Using Windows Script:
Set ComputerObj = GetObject("WinNT://MYCOMPUTER")
ComputerObj.Filter = Array("Service")
For Each Service in ComputerObj
WScript.Echo "Service display name = " & Service.DisplayName
WScript.Echo "Service account name = " & Service.ServiceAccountName
WScript.Echo "Service executable = " & Service.Path
WScript.Echo "Current status = " & Service.Status
Next
You can easily filter the above for the specific service you want.
Well i see "Nick Kavadias" telling this:
"according to this http://www.computerhope.com/nethlp.htm it should be NET START /LIST ..."
If you type in Windows XP this:
NET START /LIST
you will get an error, just type instead
NET START
The /LIST is only for Windows 2000... If you fully read such web you would see the /LIST is only on Windows 2000 section.
Hope this helps!!!
my intention was to create a script which switches services ON and OFF (in 1 script)
net start NameOfSercive 2>nul
if errorlevel 2 goto AlreadyRunning
if errorlevel 1 goto Error
...
Helped a lot!! TYVM z666
but when e.g. service is disabled(also errorlevel =2?)it goes to "AlreadyRuning"and never comes to
if errorlevel 1 goto Error ?!!
i wanted an output for that case ...
:AlreadyRunning
net stop NameOfSercive
if errorlevel 1 goto Error
:Error
Echo ERROR!!1!
Pause
my 2 Cents, hope this helps
Maybe this could be the best way to start a service and check the result
Of course from inside a Batch like File.BAT put something like this example but just replace "NameOfSercive" with the service name you want and replace the REM lines with your own code:
#ECHO OFF
REM Put whatever your Batch may do before trying to start the service
net start NameOfSercive 2>nul
if errorlevel 2 goto AlreadyRunning
if errorlevel 1 goto Error
REM Put Whatever you want in case Service was not running and start correctly
GOTO ContinueWithBatch
:AlreadyRunning
REM Put Whatever you want in case Service was already running
GOTO ContinueWithBatch
:Error
REM Put Whatever you want in case Service fail to start
GOTO ContinueWithBatch
:ContinueWithBatch
REM Put whatever else your Batch may do
Another thing is to check for its state without changing it, for that there is a much more simple way to do it, just run:
net start
As that, without parameters it will show a list with all services that are started...
So a simple grep or find after it on a pipe would fit...
Of course from inside a Batch like File.BAT put something like this example but just replace "NameOfSercive" with the service name you want and replace the REM lines with your own code:
#ECHO OFF
REM Put here any code to be run before check for Service
SET TemporalFile=TemporalFile.TXT
NET START | FIND /N "NameOfSercive" > %TemporalFile%
SET CountLines=0
FOR /F %%X IN (%TemporalFile%) DO SET /A CountLines=1+CountLines
IF 0==%CountLines% GOTO ServiceIsNotRunning
REM Put here any code to be run if Service Is Running
GOTO ContinueWithBatch
:ServiceIsNotRunning
REM Put here any code to be run if Service Is Not Running
GOTO ContinueWithBatch
:ContinueWithBatch
DEL -P %TemporalFile% 2>nul
SET TemporalFile=
REM Put here any code to be run after check for Service
Hope this can help!! It is what i normally use.
Well I'm not sure about whether you can email the results of that from a batch file. If I may make an alternate suggestion that would solve your problem vbscript. I am far from great with vbscript but you can use it to query the services running on the local machine. The script below will email you the status of all of the services running on the machine the script gets run on. You'll obviously want to replace the smtp server and the email address. If you're part of a domain and you run this script as a privileged user (they have to be an administrator on the remote machine) you can query remote machines as well by replacing localhost with the fqdn.
Dim objComputer, objMessage
Dim strEmail
' If there is an error getting the status of a service it will attempt to move on to the next one
On Error Resume Next
' Email Setup
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Service Status Report"
objMessage.From = "service_report#noreply.net"
objMessage.To = "youraddress#example.net"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.net"
'Server port (typically 25)
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Set objComputer = GetObject("WinNT://localhost")
objComputer.Filter = Array("Service")
For Each aService In objComputer
strEmail = strEmail &chr(10) & aService.Name & "=" & aService.Status
Next
objMessage.TextBody = strEmail
objMessage.Configuration.Fields.Update
objMessage.Send
Hope this helps you! Enjoy!
Edit: Ahh one more thing a service status of 4 means the service is running, a service status of 1 means it's not. I'm not sure what 2 or 3 means but I'm willing to bet they are stopping/starting.
according to this http://www.computerhope.com/nethlp.htm it should be NET START /LIST but i can't get it to work on by XP box. I'm sure there's some WMI that will give you the list.
Ros the code i post also is for knowing how many services are running...
Imagine you want to know how many services are like Oracle* then you put Oracle instead of NameOfSercive... and you get the number of services like Oracle* running on the variable %CountLines% and if you want to do something if there are only 4 you can do something like this:
IF 4==%CountLines% GOTO FourServicesAreRunning
That is much more powerfull... and your code does not let you to know if desired service is running ... if there is another srecive starting with same name... imagine:
-ServiceOne
-ServiceOnePersonal
If you search for ServiceOne, but it is only running ServiceOnePersonal your code will tell ServiceOne is running...
My code can be easly changed, since it reads all lines of the file and read line by line it can also do whatever you want to each service... see this:
#ECHO OFF
REM Put here any code to be run before check for Services
SET TemporalFile=TemporalFile.TXT
NET START > %TemporalFile%
SET CountLines=0
FOR /F "delims=" %%X IN (%TemporalFile%) DO SET /A CountLines=1+CountLines
SETLOCAL EnableDelayedExpansion
SET CountLine=0
FOR /F "delims=" %%X IN (%TemporalFile%) DO #(
SET /A CountLine=1+CountLine
REM Do whatever you want to each line here, remember first and last are special not service names
IF 1==!CountLine! (
REM Do whatever you want with special first line, not a service.
) ELSE IF %CountLines%==!CountLine! (
REM Do whatever you want with special last line, not a service.
) ELSE (
REM Do whatever you want with rest lines, for each service.
REM For example echo its position number and name:
echo !CountLine! - %%X
REM Or filter by exact name (do not forget to not remove the three spaces at begining):
IF " NameOfService"=="%%X" (
REM Do whatever you want with Service filtered.
)
)
REM Do whatever more you want to all lines here, remember two first are special as last one
)
DEL -P %TemporalFile% 2>nul
SET TemporalFile=
REM Put here any code to be run after check for Services
Of course it only list running services, i do not know any way net can list not running services...
Hope this helps!!!

Resources