header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Subject: Help with PENDING Windows File/Rename Operations
Prev Next
You are not authorized to post a reply.

Author Messages
svillarUser is Offline
New Member
New Member
Posts:3

07/20/2008 10:37 PM  

Hi Guys,

 

I'm learning powershell, and I'm currently trying to make a script to be able to do File Rename/Delete operations in Windows in the NEXT reboot, using the registry at: "hklm:\SYSTEM\CurrentControlSet\Control\Session Manager"

FileRenameOperations.

 

Could anyone one give me somecode from where I could start? I have no idea of how I could accomplish this.

 

Thanks,

 

Dominic

 

halr9000User is Offline
CLI Addict
CLI Addict
Posts:273


07/21/2008 8:02 AM  
Well it's just a matter of learning what to put in the registry keys. Once you have read up and know what needs to be in there, it's pretty easy to write to the registry from within powershell. Here are examples for the local registry. Remote is a bit more involved but perfectly doable.

43# cd "hklm:\system\currentcontrolset\control\session manager"
HKLM:\system\currentcontrolset\control\session manager
44# gi FileRenameOperations


   Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\system\currentcontrolset\con
trol\session manager

SKC  VC Name                           Property
---  -- ----                           --------
  0   0 FileRenameOperations           {}


45# Get-ItemProperty FileRenameOperations
46# ls


   Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\system\currentcontrolset\con
trol\session manager

SKC  VC Name                           Property
---  -- ----                           --------
  0   1 AppCompatibility               {AppCompatCache}
 15   0 AppPatches                     {}
  0   6 DOS Devices                    {AUX, MAILSLOT, NUL, PIPE, PRN, UNC}
  0  23 Environment                    {APR_ICONV_PATH, CLASSPATH, ComSpec, CYGWIN, FP_NO...
  0   3 Executive                      {AdditionalCriticalWorkerThreads, AdditionalDelaye...
  0   0 FileRenameOperations           {}
  0   2 kernel                         {ObUnsecureGlobalNames, obcaseinsensitive}
  0  21 KnownDLLs                      {advapi32, comdlg32, DllDirectory, gdi32, imagehlp...
  1  15 Memory Management              {ClearPageFileAtShutdown, DisablePagingExecutive, ...
  0  13 Power                          {AcProcessorPolicy, DcProcessorPolicy, AcPolicy, D...
  0   2 SFC                            {ProgramFilesDir, CommonFilesDir}
  1   6 SubSystems                     {Debug, Kmode, Optional, Posix, Required, Windows}
  0   1 Throttle                       {PerfEnablePackageIdle}


47# Get-ItemProperty sfc

ProgramFilesDir                                CommonFilesDir
---------------                                --------------
C:\Program Files?                              C:\Program Files\Common Filesw

Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)
Author, TechProsaic (http://halr9000.com)
svillarUser is Offline
New Member
New Member
Posts:3

07/21/2008 8:22 AM  
Thanks halr9000 for the reply.

Question: Before writing new values in the FileRenameOperations in the registry, do I need to format the values before writing them?

Using a Function like this:

function formatStr
{
param ( [string]$str )
if($str)
{ return "\??\"+$str }
else { return $str }
}

What is the \??\ used for? :S

Thanks,

Shauki
halr9000User is Offline
CLI Addict
CLI Addict
Posts:273


07/21/2008 8:29 AM  
Where did you get that function from?

Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)
Author, TechProsaic (http://halr9000.com)
svillarUser is Offline
New Member
New Member
Posts:3

07/21/2008 8:32 AM  

In mi previous posts I asked for help renaming/deleting a file in the registry in the next reboot, so far I have this approach, But I do not want to used it, what alternatives do I have?

Differentes ways of accomplishing the same task? Maybe symplify it! :S


$regpath = "hklm:\SYSTEM\CurrentControlSet\Control\Session Manager"

function formatStr
{
    param ( [string]$str )
    if($str)
    { return "\??\"+$str }
    else { return $str }
}

function newFileRenameOperation
{
    param
    (
    [string]$sFile = $(Throw "Missing: source file"),
    [string]$dFile = $null
    )
    $new = @(formatStr($sFile); formatStr($dFile))
    $null = New-ItemProperty -Path $regpath -Name PendingFileRenameOperations -PropertyType MultiString -Value $new
}

function addFileRenameOperation
{
    param
    (
    [string]$sFile = $(Throw "Missing: source file"),
    [string]$dFile = $null
    )
    $add = $pending.PendingFileRenameOperations + @(formatStr $sFile; formatStr $dFile)
    $null = Set-ItemProperty -Path $regpath -Name PendingFileRenameOperations -Value $add
}

switch($Args.Length)
{
    1 # DELETE THE FILE
    {
        $fullname = (Get-ChildItem $ArgsΎ] -ErrorAction SilentlyContinue).FullName
        if(!$fullname) { Write-Host "Archivo"$ArgsΎ]"no existe!" }
        else { #
            $pending = Get-ItemProperty -Path $regpath -Name PendingFileRenameOperations -ErrorAction SilentlyContinue
            if(!$pending)
            {
                newFileRenameOperation $fullname
                Write-Host `n$fullname "sera borrado."`n
            }
            else
            {
                addFileRenameOperation $fullname
                Write-Host `n$fullname will be deleted."`n
            }
        }
    }
    2 #MOVE/RENAME THE FILE
    {
        $fullname1 = (Get-ChildItem $ArgsΎ] -ErrorAction SilentlyContinue).FullName
        if(!$fullname1) { Write-Host "Archivo"$ArgsΎ]"Dont exists!" }
        else {
           #Save the absolute file path in the new file name
            if(!(Resolve-Path $ArgsΏ] -ErrorAction SilentlyContinue))
            {
                $fullname2 = $ErrorΎ].TargetObject
            }
   
            else { Write-Host "File exists, pick another."
                exit }
            $pending = Get-ItemProperty -Path $regpath -Name PendingFileRenameOperations -ErrorAction SilentlyContinue
            if(!$pending) # If PendingFileRenameOperations does not exists,we create it
            {
                newFileRenameOperation $fullname1 $fullname2
                Write-Host `n$fullname1`n"will be renamed a"`n$fullname2`n
            }
            else #If the value exists, add the value at the end of the collection
            {
                addFileRenameOperation $fullname1 $fullname2
                Write-Host `n$fullname1`n"will be renamed a"`n$fullname2`n
            }
        }
    }

 

Thanks!

 

 

You are not authorized to post a reply.
Forums > Using PowerShell > General PowerShell > Help with PENDING Windows File/Rename Operations



ActiveForums 3.7
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer