VMware Cloud Community
Proxmire
Contributor
Contributor
Jump to solution

Removing the Same Files Remotely from Many VMs

Hello everyone,

I was trying to remove the same file remotely from many VMs using the following PowerCLI script:

$a=Get-VM | %{

  foreach($nic in $_.Guest.Nics){
    Write-output $nic.IPAddress
  }
}
foreach($i in $a)
{$b=Get-WmiOBJECT -query "SELECT * From CIM_DataFile Where Name='C:\\test\\deleteme.exe'" -computername $i
$b.delete()

}

This works perfectly when I run the script on the VMs of the same host that I'm running the PowerCLI script from. When I Connect-viserver to another host however and try to run the script there, I receive the dreaded "RPC server is unavailable error (HRESULT: 0x800706BA)" for Get-WmiObject.  The VMs don't have Windows Firewall on and all have the TCP/IP, RPC and WMI services started, if that might eliminate any possibilities.

I'm also able to run other scripts that employ Get-wmiobject across all VMs such as scripts to change the DNS or the Netbios name across many machines on different hosts.

Any feedback would be greatly appreciated and please feel free to comment if you might know of any other ways that may remove files across several VMs in any other manner.  Installing PowerCLI to run the script locally on each host would be quite a painstaking process to do.

Thanks so much!

Best regards

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

To solve the problem with the quotes you can use double double quotes inside the string. Like this:

"del ""c:\documents and settings\text.exe"""


Or you can use single quotes for the string and double quotes inside:

'del "c:\documents and settings\text.exe"'
Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
9 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I do not know why your script gives an error. Maybe it tries to delete the file from a non Windows server?

I would suggest an other way to delete the files, using the PowerCLI Invoke-VMScript cmdlet. This cmdlet uses the VMware Tools to execute commands on the virtual machine. The advantage of this is that you don't need a network connection with the virtual machine. So this even works behind a firewall or if the virtual machine's network is not connected. Remember that you have to use PowerCLI in 32-bit mode to use the Invoke-VMScript cmdlet.

The script ask for credentials two times. The first time for an account with sufficient rights on the Windows servers. The second time for an account on the ESX servers. The script checks that it only tries to delete the file on VM's that are powered on and are running a Windows operating system.

$GuestCredential = Get-Credential
$HostCredential  = Get-Credential
Get-VM | `
  Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -like "*Windows*"} | `
  Invoke-VMscript -ScriptText "del c:\test\deleteme.exe" -ScriptType bat -HostCredential $HostCredential -GuestCredential $GuestCredential


Regards,

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Proxmire
Contributor
Contributor
Jump to solution

Hello Robert,

Thanks so much for your response.  All  of the servers I'm trying to delete the file from are running Windows 2003  servers, so I'm still unsure as to why it isn't working as well.  Still  baffled.  These servers are all also added to Active Directory if that should  make any difference or shed some light on the "RPC Server Unavailable"  error.

Your Invoke-VMScript looks great and I was very  excited to give it a go.  I did do a bit of research though and found  that to use the Invoke-VMScript cmdlet, it requires that PowerCLI or  Powershell be installed on the Guest OS of the VM I'm trying to connect  to or execute the script to.  If this is still the case, I'm afraid I  wouldn't be able to use it as I'm running the PowerCLI script on only  one VM to implement to the rest of them.  Installing PowerCLI on all of  of the VMs would be a lengthy process.

When I execute the script you've shared inputting my server and host credentials, I receive the following error message:

"Invoke-VMScript        Both host credential and  user/password have been specified. Credential and user/password pair are  mutually exclusive."

I've double checked that the credentials I entered were correct but I don't think that's the issue since anything I enter in for the credentials receives that error message.

Any additional advice or input you might be able to offer would be greatly appreciated.  Thanks very much for your time.

Best regards

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you only execute a BAT type script inside the guest you don't need to have PowerCLI installed.

Did you by any chance add a -User and/or -Password parameter to the Invoke-VMScript cmdlet ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

To fix the "The RPC Server is Unavailable" error you can start with checking that the "Remote Procedure Call (RPC)" service is started on the servers that give this error. Also you can check the following Microsoft Knowledge Base article: http://support.microsoft.com/kb/884564. And you can try the solutions in: Proven Solutions on How to Fix "The RPC Server Is Unavailable".

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Proxmire
Contributor
Contributor
Jump to solution

Hello again LucD,

I hadn't added either of the -User or -Password parameters to the Invoke-VMScript cmdlet and I executed the script exactly as Robert had shared it with me.  Should I try adding these?

Robert,

I was able to confirm that I have the RPC service started on all of the VMs.  Thank you for the link to the solutions page for the "RPC Server Unavailable" error.  I was able to go through what they've listed and I'm able to say that the VMs don't have Windows Firewall or an anti virus software setup, DCOM is indeed enabled, and I'm not so sure that the Registry is causing the issue.

I was able to try one thing though fellas,

1.) I transferred the VMs that were on the host where I could run the Remove File script successfully (host 1) over to the host where I was receiving the RPC Server Unavailable issue (host 2).

2.) I installed PowerCLI and the script on a VM of host 2 and executed the script.

3.) All of the VMs on host 2 were still receiving the RPC Server Unavailable error except the one VM that was running the script, and all of the VMs originally on host 1 that I transferred over, executed fine!

I'm not really sure what to make of this except that there might be something different in the settings or configurations on the VMs from host 1 and host 2.  I've compared the services running and have matched them all to no avail.

Thanks again for your efforts gentleman and anymore input you might be able to offer would be most welcomed.

Best regards

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, no. It's with the Credential parameters or with the User/Password parameters, but not both at the same time.

Can you compare the Security Profile of both hosts and see if there is a difference.

While you're at it perhaps also compare the Authentication Services and the Dns and Routing settings of both hosts.

The Invoke-VMScript uses port 902 (the default Vix port) to communicate with the guests.

Perhaps this port is blocked on server 2.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Proxmire
Contributor
Contributor
Jump to solution

Hello everyone,

The script Robert was able to share with me worked once I removed just a few things making it look a little like this:

Get-VM | `
  Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -like "*Windows*"} | `
  Invoke-VMscript -ScriptText "del c:\test\test.exe" -ScriptType bat

After reading LucD's post, I thought I'd try removing the credential parameters completely.  With these gone, once I connect to a specific host and execute the script, it prompts me for the guest OS credentials just once.  After I enter in those manually, the script performs admirably, removing the files across all the VMs regardless of the VM configuration.  The script I wrote was bunk, and using Invoke-VMscript from Robert definitely does the trick.

I did still have one last issue though which has been giving me a great deal of trouble.  I understand that this is a batch file script and so it doesn't parse spaces.  For example, when trying to delete a file in a filepath such as "c:\documents and settings\test.exe", I would get the "The system cannot find the file specified" message.

I've tried enclosing the filepath in quotations with something like:

"del "c:\documents and settings\text.exe""

as well as other variations on the theme but I can't seem to nail it.

Would you guys be able to help me overcome this last hurdle?  You've been tremendous help with this already.

Best regards

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

To solve the problem with the quotes you can use double double quotes inside the string. Like this:

"del ""c:\documents and settings\text.exe"""


Or you can use single quotes for the string and double quotes inside:

'del "c:\documents and settings\text.exe"'
Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Proxmire
Contributor
Contributor
Jump to solution

Hello everyone,

Thank you both so much for your help with this.  I was able to run the original script using the Invoke-VMScript cmdlet that Robert provided without the credential prompt, as I wasn't successful using those credential functions with my current settings.

Once I manually connected to the host using connect-viserver, running the script would prompt for the guest OS credentials and with those the script performed beautifully.

Robert was also able to solve my issue with running the BAT type script on a path file that has spaces by using the double double quotes.

Thanks again, you guys are brilliant.  Invoke-VMScript is extremely handy and I'll have to read up more on it's uses.

Best regards

Reply
0 Kudos