Announcement

Collapse
No announcement yet.

If "access denied"

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

  • If "access denied"

    Hi all

    I want to delete some files and folders on various servers, and instead of deleting them manually i want to use a batch file.

    Now as part of that script i want build in error control for when access is denied appears. If access is denied, i wan to take ownership using "TAKEOWN".

    when trying to delete a folder to which you have no access, you will get the return= Access Is Denied.

    I tried using If "Access Is Denied.", but that obviously does not work.
    Anyone any idea?
    Last edited by Killerbe; 27th November 2008, 14:00.
    [Powershell]
    Start-DayDream
    Set-Location Malibu Beach
    Get-Drink
    Lay-Back
    Start-Sleep
    ....
    Wake-Up!
    Resume-Service
    Write-Warning
    [/Powershell]

    BLOG: Therealshrimp.blogspot.com

  • #2
    Re: If "access denied"

    RD or RMDIR (and there are some more inbuilt cmd.exe commands) does not raise an error by returning an error number.

    A workaround is to choose one of the FOUR samples from the batch below.

    Code:
    @echo off
    
    :: sample 1
    set "sFolder=d:\test1"
    
    If exist "%sFolder%" (
     RMDIR /s /q "%sFolder%" 2>nul &&(
       echo/success
       )||(
       echo/failed
     )
    ) Else (echo/not found "%sFolder%")
    
    pause
    
    :: sample 2
    set "sFolder=d:\test2"
    
    If exist "%sFolder%" (
     RMDIR /s /q "%sFolder%" 2>nul
     If exist "%sFolder%" (
       echo/failed
       ) ELSE (
       echo/success
     )
    ) Else (echo/not found "%sFolder%")
    
    pause
    
    :: sample 3 capturing the error message (uses a temp-file)
    set "sFolder=d:\test3"
    
    RMDIR /s /q "%sFolder%" 2>"%temp%.\stdERR"
    (Set /p errDesc=<"%temp%.\stdERR")
    Del "%temp%.\stdERR"
    
    If DEFINED errDesc (
      echo/Failed
      ) ELSE (
      echo/success
    )
    :: reading the message comparing strings  = OS language depended!..
    IF /i "%errDesc%"=="Access is denied." (
      echo/ERROR Description - %errDesc%
    )
    
    Set "errDesc=" & pause
    
    :: sample 4 (same as sample 3, w/out using a temp-file)
    set "sFolder=d:\test4"
    
    FOR /F "tokens=*" %%* IN (
      'RMDIR /s /q "%sFolder%" 2^>^&1 1^>NUL'
      ) DO Set "errDesc=%%*" & goto :exitFor
    :exitFor
    
    If DEFINED errDesc (
      echo/Failed - [%errDesc%]
      ) ELSE (
      echo/success
    )
    :: reading the message comparing strings  = OS language depended!..
    IF /i "%errDesc%"=="Access is denied." (
      echo/ERROR Description - %errDesc%
    )
    
    pause
    \Rems
    Last edited by Rems; 28th November 2008, 22:41. Reason: added the 3rd and 4th sample

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: If &quot;access denied&quot;

      Thanks Rems
      That's valuable information.
      This is what i got so far:
      Code:
      set "Tar1=\\TestPC01\C$\Documents and Settings"
      Set "Tar2=\\Testpc02\C$\Documents and Settings"
      
      Rem Username that needs to be deleted
      
      echo Enter the name of the user profile which needs to be removed:
       echo.
        Set Profile=
         Set /P Profile=" Profile: "
      
      If Not Exist "%Tar1%"\"%Profile%",(
       Echo The profile of %Profile% does not exist on TestPC01
        GoTo TAR2
      )Else,(
       RMDIR /S /Q "%Tar1%"\"%Profile%"
        Echo Deleting Profile of %Profile% on TestPC01.
        )
      
      If Not Exist "%Tar1%"\"%Profile%",(
       Echo The profile of %Profile% has successfully been remove from TestPC01
      )Else,(
       Echo Could not delete the profile of "%Profile%" on TestPC01
       )
      
      :TAR2
      
      If Not Exist "%Tar2%"\"%Profile%",(
       Echo The profile of %Profile% does not exist on TestPC02
        Goto End
      )Else,(
       RMDIR /S /Q "%Tar2%"\"%Profile%"
        Echo Deleting profile on TestPC02.
        )
      
      If Not Exist "%Tar2%"\"%Profile%",(
       Echo The profile of %Profile% has successfully been remove from TestPC02
      )Else,(
       Echo Could not delete the profile of %Profile% on TestPC02
       )
      
      :End
      Looking at your examples, it seems i have typed to much.
      Thanks for the hints, really appreciate it.
      [Powershell]
      Start-DayDream
      Set-Location Malibu Beach
      Get-Drink
      Lay-Back
      Start-Sleep
      ....
      Wake-Up!
      Resume-Service
      Write-Warning
      [/Powershell]

      BLOG: Therealshrimp.blogspot.com

      Comment


      • #4
        Re: If &quot;access denied&quot;

        Originally posted by Killerbe View Post
        If access is denied, i wan to take ownership using "TAKEOWN".
        Seeing as you'll be removing the folder anyway, then it won't harm anything if you take ownership before trying to delete the first time around. Maybe not the most graceful solution, but it should work...
        Gareth Howells

        BSc (Hons), MBCS, MCP, MCDST, ICCE

        Any advice is given in good faith and without warranty.

        Please give reputation points if somebody has helped you.

        "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

        "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

        Comment


        • #5
          Re: If &quot;access denied&quot;

          Thanks for the replies, but now i want to take it one step further.
          I want to query an OU for computers, and use the output.
          In order to do that i need only the "commen name" to be in the output.
          I have tried:
          DSQUERY, but the Output is "Discriptive Name" (DN)
          LDIFDE, Out put is also "Discriptive Name" (DN)
          ADFIND (joeware), this does display the "Common Name" but also displays the "DN".
          Example:
          Using Server: Server.domain.prefix:389
          Directory: Windows Server 2003

          dn:CN=PC01,OU=OUNAME,DC=DOMAIN,DC=PREFIX
          >CN: PCO1

          The only output i need is "PCO1" (and so on for every computer in that ou).
          So i can use the output in the script using FOR /F
          [Powershell]
          Start-DayDream
          Set-Location Malibu Beach
          Get-Drink
          Lay-Back
          Start-Sleep
          ....
          Wake-Up!
          Resume-Service
          Write-Warning
          [/Powershell]

          BLOG: Therealshrimp.blogspot.com

          Comment


          • #6
            Re: If &quot;access denied&quot;

            I've also looked into how to do that for my own scripts. I'll post here if I find anything, could you please do the same?
            Gareth Howells

            BSc (Hons), MBCS, MCP, MCDST, ICCE

            Any advice is given in good faith and without warranty.

            Please give reputation points if somebody has helped you.

            "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

            "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

            Comment


            • #7
              Re: If &quot;access denied&quot;

              Originally posted by Killerbe View Post
              I want to query an OU for computers, and use the output.

              The only output i need is "PCO1" (and so on for every computer in that ou).
              So i can use the output in the script using FOR /F
              Code:
              @echo off
              Setlocal EnableDelayedExpansion
              
              Set "strOU=ou=Workstations,OU=OUNAME,DC=DOMAIN,DC=PREFIX"
              
              For /f "skip=1 TOKENS=1*" %%* in (
                'dsquery.exe computer "%strOU%" ^| dsget.exe computer -samid'
                ) Do >nul (echo/%%* | FindStr /i /v /r "^dsget.$") &&(
                Set "strComputer=%%*" & Set "strComputer=!strComputer:$=!"
                >nul (ping.exe -n 1 -w 300 !strComputer! | Find /i "TTL=") &&(
              
                echo/!strComputer!
              
                )||(echo/not connectable !strComputer!)
              )
              
              echo/&echo/&echo/the last computer in the list was %strComputer%
              pause
              \Rems
              Last edited by Rems; 1st December 2008, 22:59. Reason: changed 'Find' to 'FindStr'-regular expression

              This posting is provided "AS IS" with no warranties, and confers no rights.

              __________________

              ** Remember to give credit where credit's due **
              and leave Reputation Points for meaningful posts

              Comment


              • #8
                Re: If &quot;access denied&quot;

                Rems

                The script you wrote works and displays the common names as expected.

                But as far as understanding the script i can only follow untill you pipe the data from DSQUERY to DSGET.

                Could you please explain the script a bit so i could understand and so bennefit a bit more from the help you have provided?
                [Powershell]
                Start-DayDream
                Set-Location Malibu Beach
                Get-Drink
                Lay-Back
                Start-Sleep
                ....
                Wake-Up!
                Resume-Service
                Write-Warning
                [/Powershell]

                BLOG: Therealshrimp.blogspot.com

                Comment


                • #9
                  Re: If &quot;access denied&quot;

                  This is what i got thill now.
                  It works but it is using a text file
                  Code:
                  ::2008::
                  ::This Batch file remove the local and roaming profiles of a designated user::
                  ::---::
                  
                  Echo "This Batch removes the local and roaming profiles of a designated user on the Development servers"
                   @Echo off
                  :: Set variable pointing to Mikado Development servers
                  
                  :: Username that needs to be deleted
                  
                  echo Enter the name of the user profile which needs to be removed:
                   echo.
                    Set Profile=
                     Set /P Profile=" Profile: "
                  
                  For /F "Tokens=1" %%a in (C:\Batch\Computer.txt) Do (
                   If Not Exist "%%aC$\Documents and Settings\%Profile%",(
                    Echo The profile of %Profile% does not exist on %%a.
                   )Else,(
                    RMDIR /S /Q "%%aC$\Documents and Settings\%Profile%"
                     Echo Deleting Profile of %Profile% on %%a.
                    )
                   )
                  
                  For /F "Tokens=1" %%a in (C:\Batch\Computer.txt) Do (
                   If Not Exist "%%aC$\Documents and Settings\%Profile%",(
                    Echo The profile of %Profile% has successfully been remove from %%a
                   )Else,(
                    Echo Could not delete the profile of "%Profile%" on %%a.
                   )
                  )
                  
                  :: Fileserver
                  :: Deletes the roaming profile of the user on the file server. 
                  
                  :Roam
                  
                  Set "roam=\\server01\Home$"
                  
                  If Not Exist "%roam%"\"%Profile%",(
                   Echo The profile of %Profile% does not exist on %roam%.
                    Goto End
                  )Else,(
                   RMDIR /S /Q "%Roam%"\"%Profile%"
                    Echo Deleting profile on %Roam%.
                   )
                  
                  If Not Exist "%Roam%"\"%Profile%",(
                   Echo The profile of %Profile% has successfully been remove from %Roam%.
                  )Else,(
                   Echo Could not delete the profile of %Profile% on %Roam%. 
                  )
                   
                  :End
                  Pause
                  [Powershell]
                  Start-DayDream
                  Set-Location Malibu Beach
                  Get-Drink
                  Lay-Back
                  Start-Sleep
                  ....
                  Wake-Up!
                  Resume-Service
                  Write-Warning
                  [/Powershell]

                  BLOG: Therealshrimp.blogspot.com

                  Comment


                  • #10
                    Re: If &quot;access denied&quot;

                    I added a ping test to the batch in my previous post, this will shortens the delay when trying to connect to a computer that is not powered-on. (EDIT Dec. 1st.. I made some other modifications to the script, improved way to filter the last line out).
                    btw, also added two more samples in my first reply!

                    Originally posted by Killerbe View Post
                    Rems

                    The script you wrote works and displays the common names as expected.
                    But as far as understanding the script i can only follow untill you pipe the data from DSQUERY to DSGET.
                    Could you please explain the script a bit so i could understand and so bennefit a bit more from the help you have provided?
                    The command dsget.exe computer -samid outputs the sAMAccountname
                    In the batch, Dsget receive input from a Dsquery output
                    http://forums.petri.com/showthread.p...9397#post29397

                    I enabled "DelayedExpansion" for this batch because most computer sAMAccountName have $ at the end, therefore there must be a string substitution that will get rid of the $ added to the batch - but it would be in the For-Do Loop.

                    When DelayedExpansion is enabled, the command processor interprets an exclamation point (!) as an environment variable expands only when used. By default, the command processor expands variables when it reads a line of text, with DelayedExpansion enabled, you can get the current variable value at execute time. It will be possible to "Set" a variable and "Expand" it if the commands are on te same line or in the same section (grouped commands between parentheses).
                    We should use the exclamation marks to expand variables only for the variables that are defined between the parentheses untill after the last closing parenthese, then use percentage signs again for all the defined variables.

                    On this line Set "strComputer=%%*" & Set "strComputer=!strComputer:$=!" - the "$" that is in the computername is being substituded (:$=) for nothing (=empty). It uses the !variable! syntax.
                    (Instead of enabling "DelayedExpansion" it is also possible to call a :subroutine and expand the variable the normal way in the subroutine, you can add the additional commands there, after that is done the batch returns back to Loop to get to the next computer name)

                    For the line For /f "skip=1 TOKENS=1*" %%* in (
                    where skip=1 means that the first text line must skipped from the output, =the output that is returned from the command line that is between (' and ').
                    The command >nul (echo/%%* | FindStr /i /v /r "^dsget.$") &&(
                    will skip every line containing the text "dsget<space>", in this case it will skip the dsget message at the last line.

                    Of course, when using the computername to connect to the computer, aging/scavenging must be enabled (and still... during testing the batch, it although happened that the script on my network was trying to connect to the wrong computer).

                    \Rems
                    Last edited by Rems; 1st December 2008, 22:55.

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment

                    Working...
                    X