IT tutorials
 
Windows
 

Windows Server 2008 : Creating and Running a PowerShell Script - Running a Script Against Multiple Computers

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/28/2013 1:45:19 AM

Occasionally, you need to run a single script against multiple computers. One way of doing so is to read a list of the computers from a text file.

This script shows how to read the computer list from a text file and then run the script against each computer in the list. Although this script records the BIOS information for each computer in the list, you can use it to do any other task by modifying the content of the foreach loop.

The full script is presented here, with some comments on the lines in the following table.

$computers = get-content "c:\data\computerlist.txt"
"BIOS Information as of " + (get-date).tostring('MMM-dd-yyyy')
| out-file c:\data\computerbios.txt
foreach ($computer in $computers){
"Computer name: " + $computer -computername $computer
| out-file c:\data\computerbios.txt -append
get-wmiobject win32_bios | out-file c:\data\computerbios.txt -append
}

For this example, the computerlist.txt file has the following two lines:

DC1
DC2

Tip

This script reads from a file named computerlist.txt file. You can create the script manually or automatically. 


Script Lines to Run Command Against Multiple ComputersComments
$computers = get-content "c:\data\
computerlist.txt"

This command reads the list of computer names from a file named computerlist.txt and places them in an array named $computers. You can view the array contents by entering $computers.

Note

If the file doesn’t exist, the script fails.

"BIOS Information as of " +
(get-date).tostring('MMM-dd-yyyy')
| out-file c:\data\computerbios.txt

Next, a file header line is added, indicating what the file contains. The get-date cmdlet is used to add the month and year to the header, identifying when it was created.

Note

The -append switch is not used, so any existing data in the file is deleted.

foreach ($computer in $computers){
"Computer name: " + $computer |
out-file c:\data\computerbios.txt
-append
get-wmiobject win32_bios
-computername $computer | out-file
c:\data\computerbios.txt -append
}

The foreach command loops through the $computers array. For each computer name in the array, it outputs data. First, it outputs the name of the computer. Next, it uses the get-wmiobject cmdlet to retrieve information on the BIOS for the computer identified as $computer. It does this for each computer name retrieved from the computerlist.txt file and stored in the $computers variable.
You can view the file with this command:
PS C:\> notepad c:\data\
computerbios.txt

Figure 1 shows a screen shot of the opened file.

Figure 1. File created by script

Tip

This scripted used get-wmiobject, and there are thousands of commands you can execute using get-wmiobject. You can’t know them all. However, Marc van Orsouw (AKA The PowerShell Guy) wrote a great script known as the PowerShell WMI Explorer. You can read about it and download it from http://thepowershellguy.com/blogs/posh/archive/2007/03/22/powershell-wmi-explorer-part-1.aspx. Figure 2 shows a screenshot of the WMI Explorer in action.

Figure 2. PowerShell WMI Explorer
 
Others
 
- Windows Server 2012 : Preparing for deploying domain controllers (part 3) - Existing forest domain controller deployment
- Windows Server 2012 : Preparing for deploying domain controllers (part 2) - New forest domain controller deployment
- Windows Server 2012 : Preparing for deploying domain controllers (part 1) - AD DS deployment scenarios
- Windows Server 2012 : Windows PowerShell automation (part 2) - Disconnected sessions
- Windows Server 2012 : Windows PowerShell automation (part 1) - Background jobs, Scheduled jobs
- Windows 7 : Making and Ending a Dial-Up Connection
- Windows 7 : Configuring a Dial-Up Internet Connection (part 2) - Adjusting Dial-Up Connection Properties
- Windows 7 : Configuring a Dial-Up Internet Connection (part 1) - Creating a New Dial-Up Connection
- Windows 7 : Installing a Modem for Dial-Up Service
- Windows 7 : Connection Technologies - Analog Modem, ISDN, DSL, Cable Modem, Satellite Service, Wireless and Cellular Service
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us