Author Posts

April 20, 2018 at 1:21 pm

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.

April 20, 2018 at 2:07 pm

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. 🙂

April 20, 2018 at 2:14 pm

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.

April 21, 2018 at 12:40 pm

.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')

April 23, 2018 at 11:21 am

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

April 23, 2018 at 12:38 pm

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

April 23, 2018 at 12:51 pm

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

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