Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

PowerShell - write to run constantly?


  • Please log in to reply
13 replies to this topic

#1 teatrap

teatrap

  • Members
  • 121 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:49 AM

Posted 17 December 2013 - 11:03 AM

I'm just learning some PowerShell at the moment but something that I'm curious about (though I can't actually write anything at the moment.... ) Is whether something can be written in PowerShell to constantly run. 

 

Say write something so that every 10 minutes a folder is backed up - or would this have to be done using the Task Manager? 

 

Or having something that looks at the dates of files within a folder and backs up the ones that are newer than a certain date, would that have to be run each time or would you be able to write something that would just be a background process? 

 

Just curious, haven't really got my head around what the main use of the program is and whether it's something that can be used to write programs that run constantly or just used to administer certain tasks manually etc.... 

 

Hope that makes sense. 


Operating system_Windows 8.1 Pro with Media Center (9600)
CPU_Intel® Core™ i7-4700MQ CPU @ 2.40GHz
Motherboard_Hewlett-Packard (1966)
RAM_12063mb
Hard Drive_931gb (HGST HTS541010A9E680)
Graphics card_Intel® HD Graphics 4600
Sound card_IDT High Definition Audio CODEC


BC AdBot (Login to Remove)

 


#2 Roodo

Roodo

  • Members
  • 760 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:11:49 PM

Posted 17 December 2013 - 11:37 AM

You could do a while loop To run something until the condition is met or never met.



#3 teatrap

teatrap
  • Topic Starter

  • Members
  • 121 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:49 AM

Posted 17 December 2013 - 11:39 AM

So while date.year > 1990 or something to that effect....? Does this sap much system memory / is it something that's often done with PowerShell or is it bending it's uses a bit? 

 

Just trying to get my head around it... cheers


Operating system_Windows 8.1 Pro with Media Center (9600)
CPU_Intel® Core™ i7-4700MQ CPU @ 2.40GHz
Motherboard_Hewlett-Packard (1966)
RAM_12063mb
Hard Drive_931gb (HGST HTS541010A9E680)
Graphics card_Intel® HD Graphics 4600
Sound card_IDT High Definition Audio CODEC


#4 Roodo

Roodo

  • Members
  • 760 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:11:49 PM

Posted 17 December 2013 - 11:50 AM

That would be a loop running forever. That would be just another process running then depending on

the tasks would depend on system resource usage. You could write a script to run loops inside loops.



#5 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:49 PM

Posted 17 December 2013 - 02:02 PM

Say write something so that every 10 minutes a folder is backed up - or would this have to be done using the Task Manager?

Task Manager was designed *precisely* to accomplish this task. The system already has a process set up to launch things on a schedule for you, why not use that system? (No reason to reinvent the wheel here)

So while date.year > 1990 or something to that effect....? Does this sap much system memory / is it something that's often done with PowerShell or is it bending it's uses a bit?

Yes, this will consume a whole core.
 

That would be a loop running forever. That would be just another process running then depending on
the tasks would depend on system resource usage. You could write a script to run loops inside loops.

Loops inside loops would still consume a whole core, there is no savings there.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#6 Roodo

Roodo

  • Members
  • 760 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:11:49 PM

Posted 17 December 2013 - 02:44 PM

I didn't mean to suggest that.



#7 teatrap

teatrap
  • Topic Starter

  • Members
  • 121 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:49 AM

Posted 17 December 2013 - 03:23 PM

Cheers both, sos I'm pretty loose with this stuff.... A whole core is a bit expensive! 

 

OK I guess its not possible to do what I was thinking without making some kind of extra program that would keep the script running, or something to that effect. 

 

So this example isn't one thats very good for PowerShell then I guess Billy? 

 

Thanks. 


Operating system_Windows 8.1 Pro with Media Center (9600)
CPU_Intel® Core™ i7-4700MQ CPU @ 2.40GHz
Motherboard_Hewlett-Packard (1966)
RAM_12063mb
Hard Drive_931gb (HGST HTS541010A9E680)
Graphics card_Intel® HD Graphics 4600
Sound card_IDT High Definition Audio CODEC


#8 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:49 PM

Posted 17 December 2013 - 05:38 PM

OK I guess its not possible to do what I was thinking without making some kind of extra program that would keep the script running, or something to that effect.

Why is the task scheduler not sufficient for your needs here?

So this example isn't one thats very good for PowerShell then I guess Billy?

Powershell is just fine. Have a powershell script that configures the task scheduler to run itself (or another script) to run at the schedule you want.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#9 teatrap

teatrap
  • Topic Starter

  • Members
  • 121 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:49 AM

Posted 17 December 2013 - 06:04 PM

 

Why is the task scheduler not sufficient for your needs here?

 

 

 

 

Ummm Well at the moment I don't have exact needs, these are more just questions to get a feel... What I would like to do is have some scripts that can be triggered by users (so it would need some kind of basic GUI to go over the top of it) and then I'd like to make some (for thing's such as backing up) that would run in the back ground... 

 

 

 

So this example isn't one thats very good for PowerShell then I guess Billy?

Powershell is just fine. Have a powershell script that configures the task scheduler to run itself (or another script) to run at the schedule you want.

 

Ha OK... So how does that work? I mean is the PowerShell script set to start at log in and commences as soon as the computer starts or is there something that triggers it? 

 

Nice one :)


Operating system_Windows 8.1 Pro with Media Center (9600)
CPU_Intel® Core™ i7-4700MQ CPU @ 2.40GHz
Motherboard_Hewlett-Packard (1966)
RAM_12063mb
Hard Drive_931gb (HGST HTS541010A9E680)
Graphics card_Intel® HD Graphics 4600
Sound card_IDT High Definition Audio CODEC


#10 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:49 PM

Posted 17 December 2013 - 11:13 PM

Ummm Well at the moment I don't have exact needs, these are more just questions to get a feel... What I would like to do is have some scripts that can be triggered by users (so it would need some kind of basic GUI to go over the top of it) and then I'd like to make some (for thing's such as backing up) that would run in the back ground...

Ah, I'm not sure there's a way to do GUIs from Powershell. It is a shell scripting language, not a general programming language. On the other hand, you can technically call any .NET API using it, so you could technically create something like a WinForms app or similar.

That said, I'd strongly recommend *not* attempting to do that :)

Ha OK... So how does that work? I mean is the PowerShell script set to start at log in and commences as soon as the computer starts or is there something that triggers it?

No, nothing to do with log in. Just register it as a task scheduler job.

Start here -> http://technet.microsoft.com/en-us/library/jj649816.aspx

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#11 teatrap

teatrap
  • Topic Starter

  • Members
  • 121 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:49 AM

Posted 18 December 2013 - 06:18 AM

Cheers Billy - What I meant was writing thing's in PowerShell then making a GUI in something else, that would trigger the PowerShell scripts. Like a little interface with buttons for 'Back up Documents' etc etc.... 

 

I thought that you said you had a PowerShell script to configure the Task Scheduler to run itself, one that started the Task Scheduler..... But I guess you mean a script that's triggered by task scheduler? 

 

cheers


Operating system_Windows 8.1 Pro with Media Center (9600)
CPU_Intel® Core™ i7-4700MQ CPU @ 2.40GHz
Motherboard_Hewlett-Packard (1966)
RAM_12063mb
Hard Drive_931gb (HGST HTS541010A9E680)
Graphics card_Intel® HD Graphics 4600
Sound card_IDT High Definition Audio CODEC


#12 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:49 PM

Posted 18 December 2013 - 04:34 PM

Like a little interface with buttons for 'Back up Documents' etc etc....


If that's what you're doing then I don't see what Powershell gains you. You're already running code for your app, you might as well have your app do the work too.

Powershell is a scripting language -- it is designed to be efficient at scripting things, not huge applications.

I thought that you said you had a PowerShell script to configure the Task Scheduler to run itself, one that started the Task Scheduler..... But I guess you mean a script that's triggered by task scheduler?

I mean both. Your script can be something like (pseudocode):

if (<# There is a switch -intask on the command line #>) ( <#
    Do your main script
#> ) else ( <#
    Register this script with the task scheduler with whatever the input command line was, with -astask prepended
#> )
or similar.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#13 teatrap

teatrap
  • Topic Starter

  • Members
  • 121 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:49 AM

Posted 18 December 2013 - 04:36 PM

 If that's what you're doing then I don't see what Powershell gains you. You're already running code for your app, you might as well have your app do the work too.

Powershell is a scripting language -- it is designed to be efficient at scripting things, not huge applications.

 

Yeah that's fair - I've been having a hunt round the last couple of day's and I'm going to start learning Java... I'm guessing that thing's along these lines can be achieved using this! 


Operating system_Windows 8.1 Pro with Media Center (9600)
CPU_Intel® Core™ i7-4700MQ CPU @ 2.40GHz
Motherboard_Hewlett-Packard (1966)
RAM_12063mb
Hard Drive_931gb (HGST HTS541010A9E680)
Graphics card_Intel® HD Graphics 4600
Sound card_IDT High Definition Audio CODEC


#14 SteveOC

SteveOC

  • Members
  • 94 posts
  • OFFLINE
  •  
  • Local time:05:49 AM

Posted 20 December 2013 - 05:26 AM

As I understand it, ShowUI (a module available from CodePlex) will give you access to GUI functionality from within Powershell scripts - i.e. pop-up boxes, clickable buttons etc if that is what you want.

 

Steve O.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users