locked
Powershell Script to Multiple Servers RRS feed

  • Question

  • I have this script that I would like to push to 15 servers in my lab to save a lot of time. When I go to server manager and select multiple servers and select powershell it just gives me 15 different windows. I would like to send the script below to all 15 servers. How can I go about doing this? I saw something about the invoke command, however, I don't completely understand it. It seems good for "get" data but not so much for sending it? Could someone please help me understand this a little better. I have lots of powershell scripts I am excited to try but sending it to one server at a time still seems to defeat the purpose.

    Servers are named NTASRV-01,NTASRV-02,NTASRV-03... and so on

    $env = $env:computername

    $a = $env.Substring($env.length -2, 2)

    $vhdname = "VHDD"

    $parent = "GoldenImage"

    $vhd1 = $vhdname + ($a + 1)

    $vhd2 = $vhdname + ($a + 2)

    $vhd3 = $vhdname + ($a + 3)

    $vhd4 = $vhdname + ($a + 4)

    New-VHD -ParentPath C:\VHD\$parent.vhd -Differencing -Path C:\vhd\$vhd1.vhd

    New-VHD -ParentPath C:\VHD\$parent.vhd -Differencing -Path C:\vhd\$vhd2.vhd

    New-VHD -ParentPath C:\VHD\$parent.vhd -Differencing -Path C:\vhd\$vhd3.vhd

    New-VHD -ParentPath C:\VHD\$parent.vhd -Differencing -Path C:\vhd\$vhd4.vhd

    $VMname = "VM"

    $SWname = "Primary"

    $VM1 = $VMname + ($a + 1)

    $VM2 = $VMname + ($a + 2)

    $VM3 = $VMname + ($a + 3)

    $VM4 = $VMname + ($a + 4)

    New-VM -Name $VM1 -VHDPath c:\vhd\$vhd1.vhd -SwitchName $SWname -MemoryStartupBytes 1GB

    New-VM -Name $VM2 -VHDPath c:\vhd\$vhd2.vhd -SwitchName $SWname -MemoryStartupBytes 1GB

    New-VM -Name $VM3 -VHDPath c:\vhd\$vhd3.vhd -SwitchName $SWname -MemoryStartupBytes 1GB

    New-VM -Name $VM4 -VHDPath c:\vhd\$vhd4.vhd -SwitchName $SWname -MemoryStartupBytes 1GB 

    Monday, October 20, 2014 12:01 AM

Answers

  • Here is approximately how you have to do this:

    function BuildRemotes{
        Param(
            $remotecomputer
        )
        
        $sb={
            $vhdname='VHDD'+$env:computername.Substring($env.length -2, 2)
            for*$i = 1; $i -le 4; $i++){
                $vhd=$vhdname$prefix$i
                New-VHD -ParentPath 'C:\VHD\GoldenImage.vhd' -Differencing -Path "C:\vhd\$vhd.vhd"
                $VM="VM$prefix$i"
                New-VM -Name $VM -VHDPath "c:\vhd\$vhd.vhd" -SwitchName Primary -MemoryStartupBytes 1GB 
            }
        }
    
        Invoke-Command -ScriptBlock $sb -ComputerName $remotecomputer
        
    }
    


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, October 20, 2014 1:05 AM
    • Marked as answer by Alexstraz Friday, November 14, 2014 4:23 AM
    Monday, October 20, 2014 1:04 AM

All replies

  • Here is approximately how you have to do this:

    function BuildRemotes{
        Param(
            $remotecomputer
        )
        
        $sb={
            $vhdname='VHDD'+$env:computername.Substring($env.length -2, 2)
            for*$i = 1; $i -le 4; $i++){
                $vhd=$vhdname$prefix$i
                New-VHD -ParentPath 'C:\VHD\GoldenImage.vhd' -Differencing -Path "C:\vhd\$vhd.vhd"
                $VM="VM$prefix$i"
                New-VM -Name $VM -VHDPath "c:\vhd\$vhd.vhd" -SwitchName Primary -MemoryStartupBytes 1GB 
            }
        }
    
        Invoke-Command -ScriptBlock $sb -ComputerName $remotecomputer
        
    }
    


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, October 20, 2014 1:05 AM
    • Marked as answer by Alexstraz Friday, November 14, 2014 4:23 AM
    Monday, October 20, 2014 1:04 AM
  • Could you explain the purpose for the Function and Param with the $remotecomptuer ? I kept getting errors when leaving it in and took it out. Again I'm super new to powershell. I took it out and did the below code and this successfully worked for me on all remote computers, however, I imagine I didn't do this the most efficient way. Also thank you for this example I understand the ForEach, Loop, and purpose of {} better.

    $sb={
            for($i = 1; $i -le 4; $i++){
                $env = $env:COMPUTERNAME
                $a = $env.Substring($env.length -2, 2)
                $vhdname = "VMHDD"+ $a
                $VMname = "VMM" + $a
                $vhd = $vhdname + $i
                New-VHD -ParentPath 'C:\VHD\GoldenImage.vhd' -Differencing -Path "C:\vhd\$vhd.vhd"
                $VM = $VMname + $i 
                New-VM -Name $VM -VHDPath "c:\vhd\$vhd.vhd" -SwitchName Primary -MemoryStartupBytes 1GB 
            }
        }
    Invoke-Command -ScriptBlock $sb -ComputerName (Get-Content C:\testcontent.txt)


    Monday, October 27, 2014 1:47 AM
  • The $remotecomputer was just for example. What you did was good and correct.

    I recommend starting with a good book on PowerShell to help clear up any confusion.


    ¯\_(ツ)_/¯

    Monday, October 27, 2014 2:08 AM