[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 79
  • Last Modified:

execute powerhsell script on multipl vm

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 runniing 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 3vms in VM (00000281) should do the work correct? As am very new to powershell can someone please help?
0
Maniman man
Asked:
Maniman man
  • 3
  • 3
1 Solution
 
oBdACommented:
Just use the UNC for the remote machines:
$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
}

Open in new window

1
 
Maniman manAuthor Commented:
I have tried to do so, but as Im new to powershell am not sure how to use the above. Could you please verify and advise?
$File = "C:\sample.txt"

Get-Content $File -TotalCount 1 | Set-Content $File
"00000282", "00000283", "00000284" | %
{
      $FileUnc = "\\$($_)\C$\C:\cldscripts\macs_reference.txtC$\($File.Replace(':', '$'))"
      Get-Content $FileUnc -TotalCount 1 | Set-Content $FileUnc
}

Is the above syntax correct?
0
 
oBdACommented:
No.
Assuming that the file you want to process has the same name and location on all four servers, all you need to do is change the path in line 1, nothing else.
1
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Maniman manAuthor Commented:
ok so assuming file I want to process is C:\sample.txt, which is the same location for all four servers,the syntax should look like below correct?

$File = "C:\sample.txt"

Get-Content $File -TotalCount 1 | Set-Content $File
"00000282", "00000283", "00000284" | %
{
      $FileUnc = "\\$($_)\C$\C:\sample.txtC$\($File.Replace(':', '$'))"
      Get-Content $FileUnc -TotalCount 1 | Set-Content $FileUnc
}

Is the above syntax correct?
0
 
oBdACommented:
Still no.
As I said: change the path in line 1, nothing else (and if the file is actually C:\sample.txt, then there's nothing at all to change).
1
 
Maniman manAuthor Commented:
It worked, thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now