Caynadian
Caynadian

Reputation: 779

Calling W32TM from PowerShell Script Gives RPC Error

I am trying to do (what I thought was) a simple script to get all machines on the network to do a time resync (I know they would do this anyway on their own). I created the following script:

Import-Module ActiveDirectory
$list = Get-ADComputer -filter * -SearchBase "OU=Computers,OU=CaymanPort,DC=CaymanPort,DC=com"
foreach ($computer in $list) {
  echo $computer.name

  if (Test-Connection -Cn $computer.name -BufferSize 16 -Count 1 -ea 0 -quiet ) {
    w32tm /resync /nowait /computer:$computer.name
  } else {
    echo DOWN!
  }
}

But when I run it, I get the same "The following error occurred: The RPC server is unavailable. (0x800706BA)" error for every machine. If I run the same command from the Powershell CLI, it works fine???

I tried using the invoke-command cmdlet to run the w32tm command on the remote machine but that requires that I have WinRM enabled for every computer (which I don't).

I am running the script on a Windows 7 64bit machine and targeting a combination of Server 2003, 2008, and Windows 7 x32/x64 machines. Any idea why it fails from a script but not the command line?

Upvotes: 0

Views: 1982

Answers (1)

Vasili Syrakis
Vasili Syrakis

Reputation: 9631

Usually this kind of thing would occur because $computer.name is being parsed as $computer+".name" even though it might come out properly from the echo command.

My advice is to create a [string] and assign the value of $computer.name to it like so:

[string]$compname = $computer.name

cmd /c w32tm /resync /nowait /computer:$compname

Upvotes: 3

Related Questions