Log in

Sharing the ways technology transforms teaching and learning.

Our Affiliates: ISTE * NEISTE * NHCTO


  • 04 Nov 2010 10:10 AM
    Message # 456867

    Use this topic to post useful scripts for others, in PowerShell, VBS, .bat, PHP ... whatever has worked well for you in making IT administration easier for you! Please be sure to include information about how to run the script, if special command-line parameters are needed, etc.

  • 04 Nov 2010 10:42 AM
    Reply # 456879 on 456867

    Windows PowerShell 2.0: I have learned PowerShell on the fly this year in times of absolute need. I wanted to post some of the resources that I have found.

    I have used PowerShell mainly for tasks with user folders;  comparing home folders to Active Users, for example (to determine if home folders are missing, or conversely, to know which home folders belong to inactive students and should be deleted).

  • 04 Nov 2010 12:20 PM
    Reply # 456950 on 456867
    Pamela McLeod wrote:

    Use this topic to post useful scripts for others...

    Here is a script which I use every day. We have a current issue where something in Windows 7 is deleting students' home folders on login. I run this script every hour or two and it tells me which of the students no longer have home folders on the server. I then restore their home folder manually with Previous Versions (Volume Shadow Services) and re-set security on their folder. Prior to this script, I was comparing entire lists of classes to the home folder share to see what was missing.

    This script relies on the ActiveDirectory module, and I have pre-mapped the student home folder share to a mapped drive (S:). I have highlighted the areas which are specific to my environment. I keep this script in a folder on my Win 7 workstation and I run it from within PowerShell ISE.

    Import-Module ActiveDirectory
    #list students who don't have home folders

    $timestamp = Get-Date

    # 1. get users from AD Students container, loop through them
    $theUsers = Get-ADUser -Filter {(Enabled -eq "True")} -SearchBase "OU=StudentAccounts,OU=Students,OU=Accounts,DC=alton,DC=school,DC=com"
    write-host $timestamp $theUsers.count enabled users found
    $folders = Get-ChildItem S:\

    # 2. for each user, does the home folder exist?
    foreach ($user in $theUsers)
        #write-host $user.Name , $user.sAMAccountName
        $userHome = $user.sAMAccountName
        #does the home folder exist?
        $userFolderExists = Test-Path S:\$userHome
        if ($userFolderExists -eq "True") {
        } else {
            write-host No home folder for user $userHome , $user.Name -fore darkblue

  • 07 Nov 2010 4:49 PM
    Reply # 458619 on 456867

    In honor of the end of daylight savings for this year, I want to post this PowerShell command which I used to quickly check the time of my servers without having to remote into them:

    Get-WmiObject Win32_LocalTime -computerName SERVERNAME | Select-Object -expandProperty Hour

    if you type just this command, you'll see the different outputs available - I cared only about the hour today:

    Get-WmiObject Win32_LocalTime -computerName ALTONTS5

  • 18 Nov 2010 11:54 AM
    Reply # 465368 on 456867

    I use this script in Windows to log all logins in my environment. It is applied as a login script for all users. A hidden share needs to be set up on a server with write access by everyone. My share has sharing permissions set to Full Control by Everyone, then NTFS permissions set to Modify for Authenticated Users.

    The output of the script looks like this (one line per login):

    Logon ; 8/23/2009 12:20:38 PM ; COMPUTERNAME ; USERNAME ;

    The script:

    ' Logs.vbs
    ' VBScript Logon script.
    ' This program demonstrates how to log information to a log file.
    ' Modified by Pam McLeod, Alton School District. Originally from:
    ' ----------------------------------------------------------------------
    ' Copyright (c) 2003 Richard L. Mueller
    ' Hilltop Lab web site - http://www.rlmueller.net
    ' Version 1 - March 26, 2003
    ' Version 1.1 - January 25, 2004 - Modify error trapping.
    ' You have a royalty-free right to use, modify, reproduce, and
    ' distribute this script file in any way you find useful, provided that
    ' you agree that the copyright owner above has no warranty, obligations,
    ' or liability for such use.

    Option Explicit

    Dim objFSO, objLogFile, objNetwork, objShell, strText, intAns
    Dim intConstants, intTimeout, strTitle, intCount, blnLog
    Dim strUserName, strComputerName, strIP, strShare, strLogFile

    strShare = "\\servername\logshare$"
    strLogFile = "login_history1.log"
    intTimeout = 20

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject("Wscript.Network")
    Set objShell = CreateObject("Wscript.Shell")

    strUserName = objNetwork.UserName
    strComputerName = objNetwork.ComputerName
    strIP = Join(GetIPAddresses())

    ' Log date/time, user name, computer name, and IP address.
    If objFSO.FolderExists(strShare) Then
      On Error Resume Next
      Set objLogFile = objFSO.OpenTextFile(strShare & "\" _
        & strLogFile, 8, True, 0)
      If Err.Number = 0 Then
        ' Make three attempts to write to log file.
        intCount = 1
        blnLog = False
        Do Until intCount = 3
          objLogFile.WriteLine "Logon ; "  & Now & " ; " _
            & strComputerName & " ; " & strUserName & " ; " & strIP
          If Err.Number = 0 Then
            intCount = 3
            blnLog = True
            intCount = intCount + 1
            If Wscript.Version > 5 Then
              Wscript.Sleep 200
            End If
          End If
        On Error GoTo 0
        If blnLog = False Then
          strTitle = "Logon Error"
          strText = "Log cannot be written."
          strText = strText & vbCrlf _
            & "Another process may have log file open."
          intConstants = vbOKOnly + vbExclamation
          intAns = objShell.Popup(strText, intTimeout, strTitle, _
        End If
        On Error GoTo 0
        strTitle = "Logon Error"
        strText = "Log cannot be written."
        strText = strText & vbCrLf & "User may not have permissions,"
        strText = strText & vbCrLf & "or log folder may not be shared."
        intConstants = vbOKOnly + vbExclamation
        intAns = objShell.Popup(strText, intTimeout, strTitle, intConstants)
      End If
      Set objLogFile = Nothing
    End If

    ' Clean up and exit.
    Set objFSO = Nothing
    Set objNetwork = Nothing
    Set objShell = Nothing


    Function GetIPAddresses()
    ' Based on a Michael Harris script, modified by Torgeir Bakken
    ' Returns array of IP Addresses as output
    ' by ipconfig or winipcfg...
    ' Win98/WinNT have ipconfig (Win95 doesn't)
    ' Win98/Win95 have winipcfg (WinNt doesn't)
    ' Note: The PPP Adapter (Dial Up Adapter) is
    ' excluded if not connected (IP address will be
    ' and included if it is connected.
      Dim sh, fso, env, workfile, ts, data, index, n, arIPAddress, parts

      set sh = createobject("wscript.shell")
      set fso = createobject("scripting.filesystemobject")
      Set Env = sh.Environment("PROCESS")
      if Env("OS") = "Windows_NT" then
        workfile = Env("TEMP") & "\" & fso.gettempname
        sh.run "%comspec% /c ipconfig >" & Chr(34) _
          & workfile & Chr(34),0,true
        'winipcfg in batch mode sends output to
        'filename winipcfg.out
        workfile = "winipcfg.out"
        sh.run "winipcfg /batch" ,0,true
      end if
      set sh = nothing
      set ts = fso.opentextfile(workfile)
      data = split(ts.readall,vbcrlf)
      set ts = nothing
      fso.deletefile workfile
      set fso = nothing
      arIPAddress = array()
      index = -1
      for n = 0 to ubound(data)
        if instr(data(n),"IP Address") then
          parts = split(data(n),":")
          'if trim(parts(1)) <> "" then
          if instr(trim(parts(1)), "") = 0 then
            index = index + 1
            ReDim Preserve arIPAddress(index)
            arIPAddress(index)= trim(cstr(parts(1)))
          end if
        end if
      GetIPAddresses = arIPAddress
    End Function


  • 13 Mar 2011 5:57 PM
    Reply # 544974 on 458619
    Pamela McLeod wrote:

    In honor of the end of daylight savings for this year, I want to post this PowerShell command which I used to quickly check the time of my servers without having to remote into them:

    I updated this script to read in a .csv file with a list of servers - so I can query the time of all servers in one script. The .csv file should be in the format (I'm including the first 3 lines of my file):

    The updated script is
    #This script runs against a list of servers in a CSV file, which should have the columns Type,Name,Virtual
    #It checks the time (hour only) of the server
    $CSVPath = "C:\Users\pmcleod\Documents\Scripts\Servers.csv"
    $TheServers = Import-Csv -Path $CSVPath | Sort-Object -Property Virtual,Type,Name

    forEach ($Svr in $TheServers)
        Get-WmiObject Win32_LocalTime -computerName $Svr.name | Select-Object -expandProperty Hour | Format-Table -Property $Svr.Name, Hour   

NHSTE (New Hampshire Society for Technology in Education) © 2024

Powered by Wild Apricot Membership Software