header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Script that runs commands for all computers listed on spreadsheet
Last Post 20 Sep 2010 04:21 PM by Marco Shaw. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Informative
ManUtd11User is Offline
New Member
New Member
Posts:2
Avatar

--
20 Sep 2010 08:57 AM
    Im new to powershell scripting and want to be able to run certain commands on all of the computers where there computer names would be listed in column a of the spreadsheet.

    here is what I have so far any help suggestions or code fixes are greatly appreciated :)
    _______________________________________________________________________
    $a = New-Object -comobject Excel.Application

    $a.Visible = $True

    $b = $a.Workbooks.Open("C:\Scripts\intachlist.xls")
    $c = $b.Worksheets.Item(1)

    $i = 1

    do
        {
            $d = $c.Cells.Item($i,1).Value()
            if ($d -ne $null)
                {
                 Get-WMIObject Win32_BIOS -computername $d
                 *The next 3 lines are the commands i want to run on each machine*
                winmgmt /resyncperf
                wmiadap -f
                restart-service winmgmt -Force
                 }
            $i++
        }
    while ($d -ne $null)
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    20 Sep 2010 02:37 PM

    Can't test, but try this:

    $excel = New-Object -ComObject Excel.Application

    $excel.Visible = $true

    $workBook = $excel.Workbooks.Open("C:\Scripts\intachlist.xls")
    $workSheet = $workBook.Worksheets.Item(1)

    $i = 1

    do
    {
        $computerName = $workSheet.Cells.Item($i, 1).Value()
       
        if ($computerName -ne $null)
        {
            Get-WMIObject Win32_BIOS -ComputerName $computerName
            $session = New-PSSession -Computername $computerName
            Invoke-Command -Session $session -ScriptBlock { Invoke-Expression "winmgmt /resyncperf; wmiadap -f; restart-service winmgmt -Force" }       
        }
       
        $i++
    } while ($d -ne $null)

    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    20 Sep 2010 04:21 PM
    Realize that you will need PowerShell v2 to have this, which will require remoting be setup properly on the remote end, and there could be an issue if you don't have admin rights on the remote machines...
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer