Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to rewite this vb wmi script in powershell

294 views
Skip to first unread message

George

unread,
Dec 7, 2008, 6:42:03 AM12/7/08
to
Hello everyone,

Any ideas about how to re-write this in powershell? Especially what means
"impersonationLevel=impersonate" and "\root\cimv2"?

http://msdn.microsoft.com/en-us/library/aa394601.aspx

Set objWMIService = GetObject("winmgmts:" _ &
"{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2")

regards,
George

Marco Shaw [MVP]

unread,
Dec 7, 2008, 9:29:47 AM12/7/08
to

To get a listing, you simply need to do:
get-wmiobject win32_scheduledjob

Note: That you can only list jobs that are also created via WMI. If the
Windows Task Scheduler was used, you cannot view these via WMI.
However, you can view WMI created jobs via the Windows Task Scheduler,
but you should not modify them via the UI.

See also:
http://www.eggheadcafe.com/software/aspnet/32911381/gwmi-win32scheduledjob-.aspx

Marco

--
*Microsoft MVP - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition (due December
15th, 2008)
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com

Shay Levy [MVP]

unread,
Dec 8, 2008, 2:23:27 AM12/8/08
to
Hi George,


ImpersonationLevel: http://msdn.microsoft.com/en-us/library/aa393852(VS.85).aspx


\root\cimv2 is the default namespace for the schema classes. These are the
primary classes for working with Windows-based operating systems.
You can list the classes for this namespace using the -list parameter:

PS > Get-WMIObject -list


To get just the win32_* classes:

Get-WMIObject -list | where {$_.name -like "win32_*"}

HTH


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar

G> Hello everyone,
G>
G> Any ideas about how to re-write this in powershell? Especially what
G> means "impersonationLevel=impersonate" and "\root\cimv2"?
G>
G> http://msdn.microsoft.com/en-us/library/aa394601.aspx
G>
G> Set objWMIService = GetObject("winmgmts:" _ &
G> "{impersonationLevel=impersonate}!\\" _ & strComputer &
G> "\root\cimv2")
G>
G> regards,
G> George


George

unread,
Dec 8, 2008, 6:01:03 AM12/8/08
to
Cool, Shay!

Do you have any ideas about how to re-write this following script into
powershell? Sorry for asking this stupid question, my knowledge about
vbscript is very limited. I like powershell actually -- but from search
Google I found vbscript samples are more than those of powershell. :-)

Set objWMIService = GetObject("winmgmts:" _ &

George

unread,
Dec 8, 2008, 6:07:01 AM12/8/08
to
Sorry Shay, one more question. I am very confused about the meaning of
ImpersonationLevel=impersonate. I just get an Win32 WMI service instance, why
I need to set impersonate level? Could you give me a sample why this setting
is useful please? :-)

regards,
George

George

unread,
Dec 8, 2008, 6:28:03 AM12/8/08
to
Thanks Marco,


Two simple and stupid questions after reading your comments. :-)

1.

How to generate such time stamp string like this automatically
"********143000.000000-420"?

I want to generate a time stamp which is 12:00pm in current time zone. Is
there any automatically way to generate information like -420?

(i.e. when I deliver the script to my friends in other time zone, I want the
script to automatically generate what should be -420 in their time zone other
than calculate manually.)

2.

> Note: That you can only list jobs that are also created via WMI. If the
> Windows Task Scheduler was used, you cannot view these via WMI.
> However, you can view WMI created jobs via the Windows Task Scheduler,
> but you should not modify them via the UI.

A little confused. Could you review whether my current understandings are
correct?

- In Windows Task Scheduler, I can change and see tasks setup by Windows
Task Scheduler?
- In Windows Task Scheduler, I can see tasks setup by WMI, but can not modify?
- From get-wmiobject win32_scheduledjob, I can only see tasks scheduled by
wmi and can not see the ones scheduled by Windows Task Scheduler?


regards,
George

Vadims Podans

unread,
Dec 8, 2008, 7:01:01 AM12/8/08
to
This is simple script to create scheduled job:
$TS = [WMIClass] "Win32_ScheduledJob"
$starttime = "********143000.000000-120"
$TS.Create("notepad.exe",$starttime,$true,16,$true,"Test")

about impersonation - in powershell it is not required, because it is
enabled by default:

[vPodans] $TS.PSBase.Scope.Options


Locale :
Username :
Password :
SecurePassword :
Authority :
Impersonation : Impersonate
Authentication : Unchanged
EnablePrivileges : False
Context : {}
Timeout : 10675199.02:48:05.4775807

[vPodans] $ts.PSBase.Scope.Options
--
WBR Vadims Podans
PowerShell blog: www.sysadmins.lv

George

unread,
Dec 8, 2008, 7:36:02 AM12/8/08
to
Thanks Vadims,

About impersonation setting, I am still confused. My confusion is I just run
the script, and the script should use the same account (privilege) which is
used to run it, why needs impersonation setting here? Could you show me a
sample about the practical usage of impersonation please?

regards,
George

Vadims Podans

unread,
Dec 8, 2008, 8:21:01 AM12/8/08
to
Impersonation is the ability of a thread to execute in a security context
that is different from the context of the process that owns the thread (from
MSDN).

If Impersonation level is set to Impersonate, then script will be run in
current user security context, which launch script. And this is enabled in
PowerShell by default. you can change this level (for example, set to
Anonymous or System. And system will try to authenticate you as anonymous
user or LocalSystem user). More information about this can be found here:
http://msdn.microsoft.com/en-us/library/ms691341(VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa378832(VS.85).aspx

In practise I never used other impersonation levels. Only one thing what you
perhaps will use - SeSecurity privileges.


--
WBR Vadims Podans
PowerShell blog: www.sysadmins.lv

George

unread,
Dec 8, 2008, 8:32:03 AM12/8/08
to
Thanks Vadims,

I checked again about the script you written before. You never set any user
account which will be used to run the job. Once I use your script to schedule
a job which runs daily, which accounts will it be run under?

--------------------


$TS = [WMIClass] "Win32_ScheduledJob"
$starttime = "********143000.000000-120"
$TS.Create("notepad.exe",$starttime,$true,16,$true,"Test")

--------------------

regards,
George

Vadims Podans

unread,
Dec 8, 2008, 9:07:01 AM12/8/08
to
About your last question:
The Win32_ScheduledJob class is derived from CIM_Job. On Windows 2000, tasks
started through Win32_ScheduledJob run under the LocalSystem account.
However, in Windows XP Service Pack 1 (SP1) and Windows Server 2003, such
tasks run under the NetworkServiceHost Account. (from MSDN)

Seems like that there is no way to change security account under which job
will run. And remember that Impersonation Level is required only when you
access object.


--
WBR Vadims Podans
PowerShell blog: www.sysadmins.lv

George

unread,
Dec 9, 2008, 2:41:00 AM12/9/08
to

Thanks Vadims,

1.

> About your last question:
> The Win32_ScheduledJob class is derived from CIM_Job. On Windows 2000, tasks
> started through Win32_ScheduledJob run under the LocalSystem account.
> However, in Windows XP Service Pack 1 (SP1) and Windows Server 2003, such
> tasks run under the NetworkServiceHost Account. (from MSDN)

Could you share links which mentions the account which scheduled job is run
at please? :-)

2.

>
> Seems like that there is no way to change security account under which job
> will run. And remember that Impersonation Level is required only when you
> access object.
> --

Great description! Let me clarify, I think you mean if I set
impersonationLevel=impersonate, then inside the implementation code of the
job itself, I could impersonate to other account other than
localsystm/NetworkServiceHost? And if in my code I do not implement any
impersonation code, the scheduled job will use the account
localsystm/NetworkServiceHost to access other resources? :-)

regards,
George

0 new messages