Convert from VB script to PowerShell

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #6013

    by tamilan at 2013-02-06 05:11:59

    Hi,
    We have the following VB script which is seheduled to run once a day on Server 2003 with the help of Windows server schedule tasks. When it runs, it looks for Local users profiles and delete any profiles which are NOT active. It also excludes certain profiles such as Admin, Citrix admin profile, etc.
    I would like to do the same on PowerShell but on Windows server 2008. The structure has changed on server 2008 with local profiles now saved under C:\Users\ and also under Registry in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList. For each locally stored profile a subkey is created whose name is set to the profile owner’s SID.

    I would like a PowerShell script to delete the inactive local profiles under C:\Users\ and also the registry sub-key under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList.
    Further like the script to delete temp files as well.

    Please see the VB script running on server 2003.
    ...........................................................
    Set sho = CreateObject("Wscript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")

    sProfilesLocation = "c:\documents and settings"
    sSvrName = sho.ExpandEnvironmentStrings("%computername%")
    bOnline = FALSE

    'Get the names of the profiles on this machine
    Set oProfiles = fso.GetFolder(sProfilesLocation)
    Set oSubFolders = oProfiles.SubFolders

    On Error Resume Next

    'Create an array of users currently logged onto this server
    Set mfSvr = CreateObject("MetaFrameCOM.MetaFrameServer",sSvrName) 'Create COM Object
    If Err.Number <> 0 Then 'Check error level is still zero
    WScript.Echo "Can't create MetaFrameFarm object on " & svrName & "."
    WScript.Echo "(" & Err.Number & ") " & Err.Description
    WScript.Echo ""
    WScript.Quit Err.Number
    End if

    mfSvr.Initialize 6, sSvrName 'Initialize COM Object

    ReDim aUsernames(int(mfSvr.Sessions.Count))
    x = 0

    For Each oSession In mfSvr.Sessions
    If Not lcase(trim(oSession.UserName)) = "" Then
    'write into array
    aUsernames(x) = lcase(trim(oSession.UserName))
    x = x + 1
    End If
    Next

    For Each Folder In oSubFolders
    sProfileFolder = sProfilesLocation & "\" & Folder.Name

    For each sUsername in aUsernames
    If lcase(trim(Folder.name)) = sUsername Then
    bOnline = TRUE
    End If
    Next

    If bOnline = FALSE Then
    Select Case lcase(Folder.Name)

    Case "default user"
    'do nothing
    Case "localservice"
    'do nothing
    Case "networkservice"
    'do nothing
    Case "all users"
    'do nothing
    Case "sshd_server"
    'do nothing
    Case "ctx_smauser"
    'do nothing
    Case Else
    fso.DeleteFolder sProfileFolder,true
    End Select
    End If
    bOnline = FALSE
    Next

    'Cleanup PCHEALTH Files
    sPCHealth = "c:\windows\PCHEALTH\HELPCTR\DataColl"

    For Each file In fso.GetFolder(sPCHealth).Files
    file.delete
    Next

    'Cleanup Windows Temp
    sTemp = "c:\windows\Temp"
    Set oTempFolders = fso.GetFolder(sTemp)

    For Each folder in oTempFolders.SubFolders
    folder.delete
    Next

    For Each file in fso.oTempFolders.Files
    file.delete
    Next

    'Wscript.echo "Completed"

    by Klaas at 2013-02-06 07:03:18

    Can you show us what you wrote in powershell so far and tell where it goes wrong?

    by Lembasts at 2013-02-06 12:54:30

    Im glad you are working on converting those old vbscripts to shiny new PS ones 🙂
    Here's a tip though. I also have powershell scripts that need to be executed via a scheduled task.
    Trouble is I cant find a way to call the PS script directly.
    So for my scheduled tasks I call a vbscript that calls a PS script like so:
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run("powershell.exe E:\scripts\scriptname.ps1")

    HTH

    by cookie.monster at 2013-02-06 14:37:40

    [quote="Lembasts"]Im glad you are working on converting those old vbscripts to shiny new PS ones 🙂
    Here's a tip though. I also have powershell scripts that need to be executed via a scheduled task.
    Trouble is I cant find a way to call the PS script directly.
    So for my scheduled tasks I call a vbscript that calls a PS script like so:
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run("powershell.exe E:\scripts\scriptname.ps1")

    HTH[/quote]

    Nonononononono : )

    Create a task. Configure it as needed. Add an action to start a program.

    Program: powershell.exe (or the full path to it)
    Add arguments: -noprofile -executionpolicy bypass -file "\\path\to\script.ps1"

    Run powershell.exe -? to see more details on parameters.

    Check off Run with highest privileges under the General tab if necessary. Ensure the account you are running with has the right permissions and configuration in the General tab.

    If that doesn't help, you will need to start troubleshooting your script. Does it run in a PowerShell session as the account you configured? Are you depending on a profile that isn't being loaded? Are you not importing modules/snapins (for PS v2)? Are you trying to work with COM objects in a non-interactive session (takes finagling, might be finicky)? Etc.

    by rasimmers at 2013-02-13 06:34:16

    Just thought it was worth mentioning that you should never just delete profile folders. A lot of programs actually use the registry profile list (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList) to process profiles which are listed by SID. I've seen issues with USMT, AV programs, etc. that do not handle the removal of profile folders. When you do a profile deletion from My Computer > Properties > Advanced (tab) > User Profiles , it deletes the folder and the registry entries. You can use DELPROF.exe or other utilities to do the removal or Group Policy to delete profiles on logoff. You don't really describe how the script is called, but it is important to delete the registry links to profiles.

    by selko at 2013-02-14 06:24:48

    Hello,

    i am using this:

    $RootPath="C:\Users"
    $ExceptionProfiles=@("$RootPath\Administrator","$RootPath\Public","$RootPath\Ctx_StreamingSvc","$RootPath\Default") | % { $_.ToString().ToLower() }

    $profiles = Get-WmiObject -Class Win32_UserProfile -Filter Special=$false

    #delete profiles
    $profiles | Where-Object { $ExceptionProfiles -notcontains $_.LocalPath.ToString().ToLower() } | % {
    "Deleting Profile "+$_.LocalPath
    $_.Delete()
    }

    This deletes also the RegKey and if Indexing service is enabled it will delete the index of the user

You must be logged in to reply to this topic.