Reputation: 6391
I'm coming from a unix background where I've written some scripts using bash and bourne. But I need to write some scripts using powershell and I'm having a hard time finding information.
For example, in *nix, I can do man bash
and read all about how to use bash and I can do man some_command
to read about a specific command. So far, I found some powershell equivalents like get-command
to see all available commands, but getting and using objects is really confusing me.
For instance, I'm trying to create a new scheduled task using powershell and found some sample code here on SO. Here is a snippit:
$schedule = new-object -com Schedule.Service
$schedule.connect()
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks | select Name, LastRunTime
foreach ($t in $tasks) {
foreach ($a in $t.Actions) {
$a.Path
}
}
I understand what this script is doing, but without experience how would I know to do the following:
new-object -com Schedule.Service
.connect
method.getfolder
and .gettasks
objectA lot of the code seems ambiguous to me, so where would I find out the above information natively using powershell?
Upvotes: 1
Views: 1061
Reputation: 52619
So you found Get-Command
. That's a good start it will show you the available cmdlets. There may be even more available after importing snapins/modules. Use Get-PSSnapin -Registered
and Get-Module -ListAvailable
to see additional modules that may be imported to give you even more cmdlets.
The nice thing about PowerShell is that the creators built in an alias system. One of the goals of it was to make it easier to learn PowerShell when you have a bash/DOS background. For example if you type man Get-Process
it will give you the documentation for the Get-Process cmdlet
. To see all documentation for it use man Get-Process -Full
. man
doesn't actually exist, it is an alias for Get-Help
which has the same functionality as man
on UNIX/Linux. You can use the Get-Alias
cmdlet to show the registered alias' and their definitions.
The script you found is working with a COM
object. You can tell because of the -com
parameter that was used for New-Object
(which is actually short for -ComObject
). Unlike .NET objects, COM
objects are not built in to PowerShell however PowerShell has support for them the same way VBScript has support for them. The Get-Member
cmdlet will unveil both .NET and COM type object members (properties and methods). More about Get-Member
below.
The script you found uses the New-Object
cmdlet to create an instance of the COM
object named Schedule.Service
. There are two main ways to find out more information about this object. The first is that you can list its properties and methods directly within PowerShell using the Get-Member
cmdlet. This cmdlet works for both .NET and COM objects. It is an invaluable cmdlet that will show you what you can do with your objects. Use man
or Get-Help
Get-Member
to learn about it. In fact you can use Get-Member
to discover the object members you asked about such as the .connect
method. The second way is to look up the documentation for the object on MSDN which is Microsoft's developer documentation website. This is probably the best page for that particular object.
Upvotes: 2
Reputation: 2531
You're on the right track in that Get-Command *foo*
will list all Cmdlets containing the word foo, and Get-Help New-Object
will show you the help file for the New-Object cmdlet.
However, you then go straight into using COM objects, which far predate Powershell. COM programming is old and can be quite archaic. Powershell lets you interface with COM, but it's not really the "Powershell way" of doing things.
In Powershell 3, I was able to find Register-ScheduledJob
:
The Register-ScheduledJob cmdlet creates scheduled jobs on the local computer.
If possible I would say that is the preferred approach over using the COM interface, just because it's likely easier and more Powershelley.
Upvotes: 1
Reputation: 232
I am not familiar with powershell scripting but found this, maybe some reference to use:
On the first link are PowerShell Scripting Webcasts to find and more.
Scheduling Jobs with the Windows PowerShell API: http://msdn.microsoft.com/en-us/library/windows/desktop/jj150476%28v=vs.85%29.aspx
Guide to getting started with Windows PowerShell: http://technet.microsoft.com/library/ee221100.aspx
About Windows PowerShell, following help topics:
get-command : Gets information about cmdlets from the cmdlet code.
get-member : Gets the properties and methods of an object.
where-object : Filters object properties.
about_object : Explains the use of objects in Windows PowerShell.
about_remote : Tells how to run commands on remote computers.
Conceptual help files are named "about_", such as:
about_regular_expression.
The help commands also display the aliases of the cmdlets. These are alternate names or nicknames that are often easier to type. For example, the alias for the Invoke-Command cmdlet is "remote".
To get the aliases, type:
get-alias
Hopefully this will help a little.
Upvotes: 2
Reputation: 28174
The first hit on Google for "powershell create scheduled task" leads here, where one of the answers refers to the Schedule.Service
COM object. That object's documentation gives you a list of all the methods and properties of the object.
You can also use get-member
to discover all the methods & properties of any variable or object in your session.
$schedule = new-object -com Schedule.Service
TypeName: System.__ComObject#{2faba4c7-4da9-4013-9697-20cc3fd40f85}
Name MemberType Definition
---- ---------- ----------
Connect Method void Connect (Variant, Variant, Variant, Variant)
GetFolder Method ITaskFolder GetFolder (string)
GetRunningTasks Method IRunningTaskCollection GetRunningTasks (int)
NewTask Method ITaskDefinition NewTask (uint)
Connected Property bool Connected () {get}
ConnectedDomain Property string ConnectedDomain () {get}
ConnectedUser Property string ConnectedUser () {get}
HighestVersion Property uint HighestVersion () {get}
TargetServer Property string TargetServer () {get}
The Component Object Model is a core piece of Windows and there are hundreds if not thousands of COM objects available in default Windows installation for interacting with both the OS and other software installed (software can install its own set of objects as well). A lot of it can be replaced with .NET Framework assemblies and PowerShell modules, snap-ins and cmdlets now.
How do you discover COM objects? Usually via Google - running searches for the things you're trying to do, and typically you'll find someone has already posted something about similar, or your search will key off words in the object's own documentation online.
If you're using PowerShell 3, you don't need to use Schedule.Service
at all - there's a set of cmdlets for working with scheduled tasks. See New-ScheduledTask
for a starter.
If you're looking for a generic PowerShell tutorial, I usually point people at this one
Upvotes: 1