Powershell ise

Hi all

I downloaded a script from Microsoft that monitors server up and down via simple test-connection and it emails status.

Now the thing is it works perfectly when I run it through powershell ise but when I try to start it via powershell by running command .\scriptname it just returns me to the powershell prompt. No errors appear.

Any ideas?

Ta
kingcastleAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Can't believe this script is posted on a MS site [shaking head]. You need to change
  $tonotification = "user@domain.org",  
to
  $tonotification = @("user@domain.org",  "user2@domain.org"),
to mail to two people.

To run from (.cmd) batch file:
powershell.exe -noexit -command ". c:\start-monitor.ps1; start-monitor PC1"

Open in new window

Optionally remove the -noexit.
0
 
Rajitha ChimmaniCommented:
Why are you expecting an error and what is the error you are expecting? Post the script here and the expected output when you run it.
0
 
kingcastleAuthor Commented:
It's not that I'm expecting an error sorry it's just the script works out of the box in powershell ise but like I say when I run the exact same script from the powershell prompt it does nothing I.e. Just goes onto a new powershell line like a carriage return
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Please show the script. In this context there should be no difference between PS and PS ISE, but maybe some PS Host vars and functions are used to display things, and they won't work now.
0
 
kingcastleAuthor Commented:
Thanks, I found the script on the Microsoft website here
Server Reboot Monitor (Report Online/Offline Status) + Email (Updated Jul 2014)
https://gallery.technet.microsoft.com/scriptcenter/2d537e5c-b5d4-42ca-a23e-2cbce636f58d

I just copied that script to ise
0
 
Rajitha ChimmaniCommented:
So, you are expecting an email with status and not receiving it?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The script only defines the function Start-Monitor, but does not run it.
PS (without ISE) considers a script to define an own scope, that is everything defined in it will not get part of the shell after the script has been finished. The trick is to use the dot operator, which is kind of "include" or "run in my scope":
. .\ScriptName.Ps1
Start-Monitor PC1, PC2, PC3

Open in new window

PS ISE however runs the script you loaded into the editor, and keeps everything, so you can use what has been defined, calculated and so on.
0
 
kingcastleAuthor Commented:
Ah ok so even tho I was running from ps the command .\start-monitor pc1 it still wouldn't run.

Was this because I should have entered . .\start-monitor pc1

So I was missing a . Really?
0
 
kingcastleAuthor Commented:
Ale on your a star just tried what you said and it works perfect. I just going to add that to a windows batch file now to run from a shortcut so cheers for that.

You know the way that script checks computers do you know how I would add to it so that it would display the computer name but also a computer description I give it?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
. .\start-monitor pc1 does not work, because the script defines a function, and a function needs to be called after defining. There are scripts you can call that way, and it isn't difficult to change the script to do that, but I think that is a moot point.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Re: http:#a40453825, do you want to provide the description with the machine name, or read it from the machine itself?
The former requires a different parameter format (pairs of strings instead of single strings), the latter WMI or similar.
0
 
kingcastleAuthor Commented:
Would just be a ref description I would myself
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
No, not that easy, the way the script is written. Requires major rework.
0
 
kingcastleAuthor Commented:
Ok no worries at all.

How would you go about running it from batch file?
I tried "powershell.exe -noexit -file c:\start-monitor.ps1"
That opens powershell and loads the script into memory but I still have to enter
Start-monitor pc1 then after that manually

And maybe having it emailing two people rather than one?
0
 
kingcastleAuthor Commented:
works perfect great post Qlemo

I was going to use a good few of the scripts from MS script site to help with server side stuff but not sure now. is there any good sites out there that powershell scripts can be found and implemented by non powershell experts?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Usually MS Technet is a good source, but sometimes you find something less "optimal". http://blogs.technet.com/b/heyscriptingguy/, http://poshcode.org/ and http://powershell.com/ are other common sources.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.