Announcement

Collapse
No announcement yet.

Little powershell script that needs help :)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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 sigpic where appropriate **

    Comment


    • #3
      Re: Little powershell script that needs help

      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)

      Comment


      • #4
        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 sigpic where appropriate **

        Comment


        • #5
          Re: Little powershell script that needs help

          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...

          Comment

          Working...
          X