Petri IT Knowledgebase Forums
 

Petri.co.il forums Home Forums Start Page Forums Frequently Asked Questions FAQ Member List Members List
Go Back   Petri IT Knowledgebase Forums > Windows Scripting > Powershell
Petri.co.il is happy to award auglan the title of Most Valuable Member !!!
Register Calendar Calendar Search Petri IT Knowledgebase Forums Search Todays Posts Today's Posts Mark Forums Read

Notices

Little powershell script that needs help :)

Little powershell script that needs help :)

this thread has 4 replies and has been viewed 3696 times

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1  
Old 24th June 2008, 16:42
XtaZee XtaZee is offline
Casual
It's not a coincidence
 
 Join Date: Jan 2005
  6 month star 12 month star
 Posts: 71
 Reputation: XtaZee is on a distinguished road (33)
Default Little powershell script that needs help :)

I've posted a powershell script that enable or disable a user account

I've been working on that script a little more and got into a dead end once again with WMI

This time the script gets a parameter a computer name
The script will then search all looged in users, disable them in AD and then will log off the machine

the problem is that it works on Vista client but not on Windows 2003 or XP

I've looked up WMI classes on XP seems to be ok ...

Code:
# PowerShell Script 
# Find logged-in usernames diable the account and logoff
# By XtaZee
# 
#Requirements
# Client Requires Windows Vista or Windows XP. 
# Server Requires Windows Server 2008 or Windows Server 2003. 
# MOF Declared in Cimwin32.mof.
# DLL Requires Cimwin32.dll.
# Namespace Defined in \root\cimv2.

param($strComputer)

#Filter UserNames result
function Get-RelevantUser($user)
{
	$irelevantUsers = New-Object -TypeName System.Collections.ArrayList;
	$irelevantUsers.AddRange(("ANONYMOUS LOGON","Administrator","LOCAL SERVICE","NETWORK SERVICE","SYSTEM"));
	$match = [regex]::Match($user.Antecedent, "Name=`"(?<name>.*)`"");
	$userName = $match.Groups["name"].Value; 
	if (-not $irelevantUsers.Contains($userName))
	{
		return $userName;
	}
}
#Find username in Active Directory
function get-dn ($UserName)
{
 	$ADroot = [ADSI]''
 	$ADsearcher = new-object System.DirectoryServices.DirectorySearcher($ADroot)
	$ADsearcher.filter = "(&(objectClass=user)(sAMAccountName= $UserName))"
	$ADuser = $ADsearcher.FindOne() #need to make sure that there is only one object - can use findall with array
return $ADuser.path
}

#Find Loggedin UserNames
$userArray = get-WmiObject -computer $strComputer Win32_LoggedOnUser | Foreach-Object { Get-RelevantUser($_) } | Sort-Object -Unique 

Foreach ( $objItem in $userArray) 
	{
	$ADpath = get-dn $userArray
#Enable = False | Disable = True the account
	$account=[ADSI]$ADpath
	$account.psbase.invokeset("AccountDisabled", "true")
	$account.setinfo()
	$userArray

#Logoff or reboot remote comuter 
	$objServerOS = gwmi win32_operatingsystem -computer $strComputer 
#logoff
	$objServerOS.Win32Shutdown(4)

#Reboot
#	in case of error while try to reboot enable both following command
#	$objServer.psbase.Scope.Options.EnablePrivileges = $true
#	$objServerOS.reboot()
	}

this is the problematic line
Code:
$objServerOS.Win32Shutdown(4)
4 is forced logoff


Thanks!
  #2  
Old 25th June 2008, 00:49
tonyyeb's Avatar
tonyyeb tonyyeb is offline
Moderator
 
 Join Date: Dec 2004
  6 month star 12 month star
 Location: Hull, UK
 Posts: 2,182
 Reputation: tonyyeb is a jewel in the roughtonyyeb is a jewel in the roughtonyyeb is a jewel in the rough (283)
Default Re: Little powershell script that needs help :)

What error do you get?
__________________
Server 2000 MCP
Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

** Remember to give credit where credit is due and leave reputation points To grant some reputation points to the user that helped you, just click on the little Yin-Yang icon on the right of the user's answer and follow the prompt. where appropriate **
  #3  
Old 25th June 2008, 08:46
XtaZee XtaZee is offline
Casual
It's not a coincidence
 
 Join Date: Jan 2005
  6 month star 12 month star
 Posts: 71
 Reputation: XtaZee is on a distinguished road (33)
Default Re: Little powershell script that needs help :)

Quote:
Originally Posted by tonyyeb View Post
What error do you get?

here it is:

Exception calling "Win32Shutdown" : "Generic failure "
At line 51, position 29
$objServerOS.Win32Shutdown(4)
  #4  
Old 25th June 2008, 10:36
tonyyeb's Avatar
tonyyeb tonyyeb is offline
Moderator
 
 Join Date: Dec 2004
  6 month star 12 month star
 Location: Hull, UK
 Posts: 2,182
 Reputation: tonyyeb is a jewel in the roughtonyyeb is a jewel in the roughtonyyeb is a jewel in the rough (283)
Default Re: Little powershell script that needs help :)

Do you have permission to invoke that command?
__________________
Server 2000 MCP
Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

** Remember to give credit where credit is due and leave reputation points To grant some reputation points to the user that helped you, just click on the little Yin-Yang icon on the right of the user's answer and follow the prompt. where appropriate **
  #5  
Old 25th June 2008, 16:13
XtaZee XtaZee is offline
Casual
It's not a coincidence
 
 Join Date: Jan 2005
  6 month star 12 month star
 Posts: 71
 Reputation: XtaZee is on a distinguished road (33)
Default Re: Little powershell script that needs help :)

Quote:
Originally Posted by tonyyeb View Post
Do you have permission to invoke that command?
I am running it with Admin, although I've did managed to run it on some machines...
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable or Enable user account via powershell script XtaZee Powershell 8 23rd December 2009 16:55
PowerShell 1.0 Released To Web Wired Powershell 4 24th January 2009 02:05
PowerShell 2.0 Community Technology Preview Wired Powershell 0 20th March 2008 11:17
PowerShell - SQL scripting Lemons SQL Server Scripting / Automation 0 31st December 2007 04:34
For those messing around with PowerShell m80arm Powershell 4 24th November 2006 13:25


All times are GMT +3. The time now is 16:35.

Steel Blue 3.5.4 vBulletin Style ©2006 vBEnhanced
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
 

Valid XHTML 1.0!   Valid CSS!

Copyright 2005 Daniel Petri