Solved

Powershell ise

Posted on 2014-11-19
17
231 Views
Last Modified: 2014-11-22
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
0
Comment
Question by:kingcastle
  • 7
  • 7
  • 2
17 Comments
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40453678
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
 

Author Comment

by:kingcastle
ID: 40453696
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 40453707
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:kingcastle
ID: 40453711
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
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40453747
So, you are expecting an email with status and not receiving it?
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40453754
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
 

Author Comment

by:kingcastle
ID: 40453766
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
 

Author Comment

by:kingcastle
ID: 40453825
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 40453852
. .\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
 
LVL 69

Expert Comment

by:Qlemo
ID: 40453860
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
 

Author Comment

by:kingcastle
ID: 40453879
Would just be a ref description I would myself
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40453935
No, not that easy, the way the script is written. Requires major rework.
0
 

Author Comment

by:kingcastle
ID: 40453966
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
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40453983
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
 

Author Comment

by:kingcastle
ID: 40458379
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 40459327
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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article outlines the process to identify and resolve account lockout in an Active Directory environment.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question