0

I have a PowerShell script, that run's at user log in on one VM (00000281): Get-Content C:/sample.txt -TotalCount 1) | Set-Content C:\sample.txt

To execute the script automatically I have created a .cmd file and placed it in the following folder:

C:\Users\vs_domadmin\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

The above VM(00000281) resides in my infra VM's cluster which contains four other Vm's (infra servers). I have a total of four servers, which I would like that script to run as well.

00000281, 00000282, 00000283, 00000284

My script is running for the moment only on the 00000281. Here is the trick: I need that script from 00000281 to execute also on the rest of the infra servers 00000282, 00000283, 00000284, knowing that the script will be stored only on my first infra server 000002821. I believe that giving the script some conditions and outlining the path of the other 3 VMs in VM (00000281) should do the work correct>

TylerH
  • 20,799
  • 66
  • 75
  • 101
Manu2287
  • 27
  • 5

2 Answers2

1

VM 00000282, 00000283, 00000284 is considered as a remote machine w.r.t the current user in vm 00000281.

You have to run the script in the machine itself using the powershell Remote process of the same machine by configuring the 4 machines to run remote powershell script.

This done by enabling every machine to run the powershell remote service WinRM

For step by step details:

How to Run PowerShell Commands on Remote Computers

Modify the script in vm 00000281, add this as last line:

Invoke-Command -ComputerName 00000282, 00000283, 00000284 -FilePath path_to_script_file.ps1  

In this way you run concurrently the same script in all machines.

Login user should have a permission in these machines.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • This answer works just fine, if you have WinRM installed on all the servers. The previous solution identified also works, without in the need to install WinRm. However I prefer the invoke command solution as it is clear, shorter codes and effective. Thank you – Manu2287 Oct 11 '16 at 16:55
0

Solution identified:

$File = "C:\sample.txt"
Get-Content $File -TotalCount 1 | Set-Content $File
"00000282", "00000283", "00000284" | % {
    $FileUnc = "\\$($_)\$($File.Replace(':', '$'))"
    Get-Content $FileUnc -TotalCount 1 | Set-Content $FileUnc
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Manu2287
  • 27
  • 5
  • This code may solve your problem but it's not of any use to anyone else since you didn't share the rest of the script in your question that at the very least maps VM locations to those arbitrary number strings you have. – TylerH Sep 28 '22 at 14:27