Solved

execute powerhsell script on multipl vm

Posted on 2016-10-11
6
49 Views
Last Modified: 2016-10-11
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
Comment
Question by:Maniman man
  • 3
  • 3
6 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41838258
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
 

Author Comment

by:Maniman man
ID: 41838524
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
 
LVL 83

Expert Comment

by:oBdA
ID: 41838538
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Maniman man
ID: 41838552
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
 
LVL 83

Expert Comment

by:oBdA
ID: 41838564
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
 

Author Closing Comment

by:Maniman man
ID: 41838625
It worked, thanks
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Teach the user how to use vSphere Update Manager to update the VMware Tools and virtual machine hardware version Open vSphere Client: Review manual processes for updating VMware Tools and virtual hardware versions: Create a new baseline group in vSp…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now