Renaming a file or folder on remote machine through WMI

This topic contains 6 replies, has 4 voices, and was last updated by  Ajay 1 month ago.

  • Author
    Posts
  • #99318

    Ajay
    Participant

    Hi,

    I am stuck in a problem for few days now, i need to create a script to rename a file or folder on remote machine in my environment. The problem, i cannot use psremoting as the service and ports required are blocked and cannot, i cannot use CIM_instance as well as the service is disabled or psexec tool. The only way i can reach remote machine is by WMI_Object method. I can get to the folder on remote machine but can't rename or delete it. The script i am using to get to the folder is below:-

    Get-WmiObject -Class win32_directory -namespace "root\cimv2" -ComputerName "remote machine" -Impersonation 3 | Where {$_.name -eq "C:\windows\SoftwareDistribution"}

    But i do not know how to rename this "software distribution" or delete it.

    I tried rename and delete method with this but did not work.

    Maybe i have create a new custom object but i do not know how to do that.

    Also, i want to do this task only by wmi object method.

    Could someone please help me?

    Thanks,
    Ajay Kumar.

  • #99321

    Joel Sallow
    Participant

    I believe if you're trying to invoke methods on WMI classes you need to use the static WMI methods, with syntax like the following:

    ([wmiclass]"root\cimv2:Win32_Process").Create('notepad.exe')

    You may, however, have to google for the specifics of how it's done with that specific class and method. I'm not too familiar with it. 🙂

    • #99324

      Ajay
      Participant

      Thanks! for your suggestion Joel.

      Yes, i came across this syntax but i am not able to find a way to use rename or delete method with this syntax.

  • #99408

    js
    Participant

    .delete() and .rename('filename') are instance methods (there's no static qualifier). Usually only the create type methods are static. This worked for me locally at least. Have to double the backslash if you use a wql (sql) filter. Or you could pipe to where, but it's much slower, since it's returning every directory. I just took a Pluralsight class on using wmi and cim in powershell. 🙂 'root\cimv2' is the default namespace, so you don't have to specify it.

    get-cimclass win32_directory | select -expand cimclassmethods
    
    Name                        ReturnType Parameters                                         Qualifiers
    ----                        ---------- ----------                                         ----------
    Rename                          UInt32 {FileName}                                         {Schema, ValueMap}
    Delete                          UInt32 {}                                                 {Schema, ValueMap}
    
    $obj = Get-WmiObject win32_directory -Filter "name = 'c:\\foo'"
    $obj.Delete()
    
    # or (need full path)
    $obj.Rename('c:\foo2')
    
    • #99463

      Ajay
      Participant

      Thanks! for your reply JS,

      But the query is not working, i used below command and it gave me an error:

      PS C:\WINDOWS\system32> $obj = Get-WmiObject win32_directory -Filter "name = 'c:\\windows\softwaredistribution'"
      
      $obj
      Get-WmiObject : Invalid query "select * from win32_directory where name = 'c:\\windows\softwaredistribution'"
      At line:1 char:8
      + $obj = Get-WmiObject win32_directory -Filter "name = 'c:\\windows\sof ...
      +        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
          + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
  • #99469

    Nick Springer
    Participant

    Look at path string. The backslash needs to be doubled up when used 🙂
    c:\\windows\softwaredistribution
    vs
    c:\\windows\\softwaredistribution

  • #99472

    Ajay
    Participant

    Yes!!! It worked. I am sorry, i did not realize i had to use double \\ everywhere.

    Thank you!! Nick..have a good day.

You must be logged in to reply to this topic.