![]() |
|
|
|||||||
| Petri.co.il is happy to award auglan the title of Most Valuable Member !!! |
| Register | Calendar |
Search |
Today's Posts |
Mark Forums Read |
| Notices |
|
|
Little powershell script that needs help :)this thread has 4 replies and has been viewed 3696 times
|
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||||||
|
||||||||
|
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) Thanks! |
|
#2
|
||||||||||
|
||||||||||
|
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 |
|
#3
|
||||||||
|
||||||||
|
here it is: Exception calling "Win32Shutdown" : "Generic failure " At line 51, position 29 $objServerOS.Win32Shutdown(4) |
|
#4
|
||||||||||
|
||||||||||
|
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 |
|
#5
|
||||||||
|
||||||||
|
I am running it with Admin, although I've did managed to run it on some machines...
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |