none
Powershell Logoff Script

    Question

  • I've created a script to execute at logoff, via the User Policy of the local GPO, which relies on a drive mapping to complete successfully. The script executes fine when I run it while logged on, however execution during logoff is not working as intended - the logfile states the drive was not mapped when it ran. I've stepped through the script and everything works as it should, so I am really puzzled as to why this is happening.

    Thursday, December 13, 2012 7:23 PM

Answers

  • If you redirect the profile folders the favorites will alsways be on the network and available from anywhere.

    You are trying to do too much when the sessiopn is shutting down so it may not work.

    Start by running a session trace during the excution and look at the trace log.  You are getting an error and you have no error handling to catch it.


    ¯\_(ツ)_/¯

    Thursday, December 13, 2012 9:22 PM
  • In a perfect world, the re-direction would have already been implemented. Unfortunately where I work it has not been implemented. I'm just doing what was asked of me. I'm not sure error handling will help. The script executes fine, but during logoff it reports the drive is not mapped, so I have to assume the mapping is being truncated prior to the script running, which IMO makes no sense as the logoff script should run before anything else (in a perfect world). Thanks for your help I will try what you have suggested.

    If you do not persist the mapping it will be gone by the timeyour script executes.

    The issue is not sc5ripting and not about what you are bieing told to do.  It is an issue of not understanding how WIndows handles these things.

    Drives, by default, are not mapped permanently.  The logoff script is guaranteed to run after the tempoarary mappings have been torn down.  That is how it works.

    I think some one in your admin group has sent you on a snipe hunt and are now chuckling at how gullible you were.  We used to do this kind of thinkg to newbie techs all of the time. It is a way to make them think about the task. This issue has also been a question on the certification tests.

    Change your logon script to check for the mapping then, if it does not exist, set a "permanent" mapping.   YOu will then not need to check.

    Drive mapping shouild be set by Group Policy preferences with WS2003R2 and later or by installing RSAT for WS2003.

    Logon scripts szhgould be kept to no more than a couple of dozen lines.  Long logon scripts indicate a lack of training in WIndows.  In Windows 95 and earlier we had to use big scripts.  This hasn't been necessary since WIndows 2000 introduced Active Directory Group Policy. That is more than 13 years ago.  Most admins today are barely old enough to remember Windows 2000.


    ¯\_(ツ)_/¯

    Thursday, December 13, 2012 10:27 PM

All replies

  • I've created a script to execute at logoff, via the User Policy of the local GPO, which relies on a drive mapping to complete successfully. The script executes fine when I run it while logged on, however execution during logoff is not working as intended - the logfile states the drive was not mapped when it ran. I've stepped through the script and everything works as it should, so I am really puzzled as to why this is happening.

    You cannot map a drive or do many other things during logoff.  The script runs after the session has been shut down.  Many programs will not work in a logoff script.

    Why would you want to map a drive during logoff?


    ¯\_(ツ)_/¯

    Thursday, December 13, 2012 8:10 PM
  • The drive is mapped during the logon. The script only checks for the presence of the drive mapping.

    I have also tried running it as a logon script but that didn't work either.

    • Edited by gdbear65 Thursday, December 13, 2012 8:37 PM
    Thursday, December 13, 2012 8:35 PM
  • The drive is mapped during the logon. The script only checks for the presence of the drive mapping.

    I have also tried running it as a logon script but that didn't work either.

    YOu would have to post your script since what you are asking is very unclear.


    ¯\_(ツ)_/¯

    Thursday, December 13, 2012 8:40 PM
  • This script copies the logged-on user's favorites to K:
    #
    #Get logon name of current user
    $LoginName = Get-WmiObject -class Win32_ComputerSystem
    $UserID = $LoginName.UserName.Split("\")
    $UserName = $UserID[1]
    #
    #confirm mapping to K: drive, output status message if not true
    #
    $MappedDrive = get-WmiObject Win32_MappedLogicalDisk -filter "DeviceID = 'K:'"
    if ($MappedDrive -ne $null) {
        $FavFolder = $env:USERPROFILE + "\Favorites"
    # test for presence of log file, create if it does not exist, copy favorites and log result
        $LogFileName = ($UserName + "FavBup.Log")
        $LogFileVerify = Test-Path "$env:USERPROFILE\$LogFileName" -PathType Leaf
        if ($LogFileVerify -eq $true) {
            $LogFile = "$env:USERPROFILE\$LogFileName"
        New-Item -path K:\ -name $UserName -type directory -force
           Copy-Item $FavFolder -Destination K:\$UserName -Recurse -Force -PassThru | out-file -filepath $LogFile
          } else {
            New-Item -path K:\ -name $UserName -type directory -force
            $LogFile = New-Item -itemtype file -path "$env:USERPROFILE\$LogFileName"
            Copy-Item $FavFolder -Destination K:\$UserName -Recurse -Force -PassThru | out-file -filepath $LogFile
          }

    } else {
     $LogFileName = ($UserName + "FavBup.Log")
        $LogFile = New-Item -itemtype file -path "$env:USERPROFILE\$LogFileName" -Force
     $LogStatus = "k: drive is not mapped"
     out-file -filepath $LogFile -InputObject $LogStatus
        }


    this is not the production version of the script, the production version copies the favorites to the users' personal drives, but I have to do some UAT testing, so I have modified it slightly from the original - functionally it is the same
    • Edited by gdbear65 Thursday, December 13, 2012 9:01 PM more info
    Thursday, December 13, 2012 8:55 PM
  • If you redirect the profile folders the favorites will alsways be on the network and available from anywhere.

    You are trying to do too much when the sessiopn is shutting down so it may not work.

    Start by running a session trace during the excution and look at the trace log.  You are getting an error and you have no error handling to catch it.


    ¯\_(ツ)_/¯

    Thursday, December 13, 2012 9:22 PM
  • In a perfect world, the re-direction would have already been implemented. Unfortunately where I work it has not been implemented. I'm just doing what was asked of me. I'm not sure error handling will help. The script executes fine, but during logoff it reports the drive is not mapped, so I have to assume the mapping is being truncated prior to the script running, which IMO makes no sense as the logoff script should run before anything else (in a perfect world). Thanks for your help I will try what you have suggested.
    Thursday, December 13, 2012 9:37 PM
  • In a perfect world, the re-direction would have already been implemented. Unfortunately where I work it has not been implemented. I'm just doing what was asked of me. I'm not sure error handling will help. The script executes fine, but during logoff it reports the drive is not mapped, so I have to assume the mapping is being truncated prior to the script running, which IMO makes no sense as the logoff script should run before anything else (in a perfect world). Thanks for your help I will try what you have suggested.

    If you do not persist the mapping it will be gone by the timeyour script executes.

    The issue is not sc5ripting and not about what you are bieing told to do.  It is an issue of not understanding how WIndows handles these things.

    Drives, by default, are not mapped permanently.  The logoff script is guaranteed to run after the tempoarary mappings have been torn down.  That is how it works.

    I think some one in your admin group has sent you on a snipe hunt and are now chuckling at how gullible you were.  We used to do this kind of thinkg to newbie techs all of the time. It is a way to make them think about the task. This issue has also been a question on the certification tests.

    Change your logon script to check for the mapping then, if it does not exist, set a "permanent" mapping.   YOu will then not need to check.

    Drive mapping shouild be set by Group Policy preferences with WS2003R2 and later or by installing RSAT for WS2003.

    Logon scripts szhgould be kept to no more than a couple of dozen lines.  Long logon scripts indicate a lack of training in WIndows.  In Windows 95 and earlier we had to use big scripts.  This hasn't been necessary since WIndows 2000 introduced Active Directory Group Policy. That is more than 13 years ago.  Most admins today are barely old enough to remember Windows 2000.


    ¯\_(ツ)_/¯

    Thursday, December 13, 2012 10:27 PM
  • In a perfect world, the re-direction would have already been implemented. Unfortunately where I work it has not been implemented. I'm just doing what was asked of me. I'm not sure error handling will help. The script executes fine, but during logoff it reports the drive is not mapped, so I have to assume the mapping is being truncated prior to the script running, which IMO makes no sense as the logoff script should run before anything else (in a perfect world). Thanks for your help I will try what you have suggested.

    If you do not persist the mapping it will be gone by the timeyour script executes.

    The issue is not sc5ripting and not about what you are bieing told to do.  It is an issue of not understanding how WIndows handles these things.

    Drives, by default, are not mapped permanently.  The logoff script is guaranteed to run after the tempoarary mappings have been torn down.  That is how it works.

    I think some one in your admin group has sent you on a snipe hunt and are now chuckling at how gullible you were.  We used to do this kind of thinkg to newbie techs all of the time. It is a way to make them think about the task. This issue has also been a question on the certification tests.

    Change your logon script to check for the mapping then, if it does not exist, set a "permanent" mapping.   YOu will then not need to check.

    Drive mapping shouild be set by Group Policy preferences with WS2003R2 and later or by installing RSAT for WS2003.

    Logon scripts szhgould be kept to no more than a couple of dozen lines.  Long logon scripts indicate a lack of training in WIndows.  In Windows 95 and earlier we had to use big scripts.  This hasn't been necessary since WIndows 2000 introduced Active Directory Group Policy. That is more than 13 years ago.  Most admins today are barely old enough to remember Windows 2000.


    ¯\_(ツ)_/¯


    Wow! Ok, thanks again.
    Friday, December 14, 2012 1:09 AM