Running a powershell script with bat commands

I am trying to get the contents of a text file (a list of computers) and run a bat script against it and save it all to a PS1 file.
imy2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KenMcFCommented:
Can you post an example of what you are trying to do? What is the batch file doing and what is the output?
imy2000Author Commented:
I have a text file containing a list of computers. I have a bat file that contains some scripts that will get information from on each computer.  I want a bat file that will get the contents of my text file and run the command against my computer list.
I have tried: FOR /f %a in (computers.txt) do test.bat but it is returning the data from my pc, repeatedly.
imy2000Author Commented:
to be more specific, I have a bat file that contains scripts that runs just fine; the scripts give me information on the computer I run it on. I want to be able to run that bat file against a list of computers instead of going to each individual computer.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

KenMcFCommented:
ok, that make more sence. I thoght you wanted to convert some BAT script into powershell. You could put these scripts on a central file share or copy them to each computer. Then use PSExec to run the script remotely. PSexec support reading a text file for a list of computer to run against.

http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

imy2000Author Commented:
I've already downloaded PS Tools. I am looking to try and find a script that will do what I want.
KenMcFCommented:
It will depend on wht the bat files are doing.
imy2000Author Commented:
THey are getting system info for each pc in the list
shajidaliCommented:
It would be easier to have the powershell itself collect the required information for you. Using the Batch will only complicate things. Can you provide what is the information you are trying to collect? Generally what a multi-line script does powershell can do the same without much coding.

In Powershell we have foreach command which can be used to pass the name of each server and it will collect what you want and write the output to the file.

If you can provide some example of what information you are trying to collect I can provide you with some sample code.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Depending on the information you need about each PC, the script needs to run locally. With batch files, you can only achieve that by using psexec or similar, as already mentioned, and that isn't the worse solution. En contraire, it is an often used approach. Since you have the batch file already, I would take that.

With PowerShell (2.0) you have remoting options, allowing for direct execution of code remotely; or use WMI to query remote machines (which is almost the same then if you use WSH via VBScript or JScript).
imy2000Author Commented:
what I have so far:
if using a bat file:
FOR /f %%a in (c:\scripts\computers.txt) DO CALL c:\scripts\test.bat %%a
So say for instance the scripts in the bat file are getting the IP Address and Windows OS version. The bat file works if I go to each pc and run it on the PC. ( I understand that PSEXEC is an option but I want to complete this (hopefully) as a secondary (if it is not too consuming)) I want to get this file to now run on a list of PCs that I select.  When I fun it with the FOR command, it looks like it is returning the same information from the PC I am running it on.
WIth Powershell, I did something like "get-content computers.txt | .\test.bat", no success.
KenMcFCommented:
That will not work becuase that bat file needs to run locally on each computer. When you run it will the for loop it is running on your computer.  You will need to use something like psexec or WMI. WMI is easy to use in powershell to get information like you are looking for off of remote computers.
Chris DentPowerShell DeveloperCommented:

Batch files won't take pipeline input. You'd have to do:

Get-Content Computers.txt | ForEach-Object { .\test.bat $_ }

How is the batch file getting the bits of information you're after?

If we stick to the two you have in the example, the IP address and OS version, I would use:

Get-Content Computers.txt | Select-Object `
  @{n='OperatingSystem';e={ (([ADSISearcher]"(&(objectCategory=computer)(name=$_))").FindOne()).Properties["operatingsystem"][0] }},
  @{n='IP';e={ [Net.Dns]::GetHostEntry($_).AddressList }}

You might substitute [ADSISearcher] for Get-QADComputer, or Get-ADComputer, but the principal is the same.

If you're getting more then avoiding chatting with the computer itself may be more difficult.

Chris
imy2000Author Commented:
what would be the command for Powershell WMI?
KenMcFCommented:
get-wmiobject -class win32_operatingsystem -computername COMPUTERNAME

there are other classes depending on what info you need.
shajidaliCommented:
To get the computer OS Version you can run

Get-Content -Path C:\Servers.txt |
ForEach-Object { "Querying $_" ;Get-WmiObject -Class win32_operatingsystem -ComputerName $_ }
 
As I mentioned you just need this one line, Just put server name in the servers.txt (one per line) and server info you need will be available to you. There are lot of WMI class and you can get lot of info by using the right class.


Get-Content -Path C:\Servers.txt | 
ForEach-Object { "Querying $_" ;Get-WmiObject -Class win32_operatingsystem -ComputerName $_ }

Open in new window

imy2000Author Commented:
this code: Get-Content Computers.txt | ForEach-Object { .\test.bat $_ } produces the same results that I have. THe script is running but returning the same information.  SO if I have five computer names in the tst file, it returns my computer's info 5 times. I have info to get OS, etc.  The test.bat is calling several scripts and writing the output to a text file. So, in essence, a .bat fie only works on the computer it is running on? I need to change my file to a .ps1 and it will run remotely?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
It's not surprising that the simple call of the batch file with a computer name doesn't do anything more than without name. It has been said several times - in that constellation the batch file works only locally on each PC.
Converting it into a PS1 might or might not help, that depends on how you do it. Using PS2(!) features, you can execute remote code in PowerShell. Using WMI, you can query remote PCs, as shown above.
Since only you know what the batch file does, we cannot tell you in more details what to do.
Chris DentPowerShell DeveloperCommented:

I still think it's completely unnecessary to get in touch with the PCs at all, but that assumes the output is limited to the fields described and AD is in use. AD has operating systems / service pack versions, DNS has IP addresses.

Putting that aside, it would be better to drop the batch file entirely and do this in PowerShell 2. Define what the batch file does, or tell us what you need to see, then we can tell you how to do it.

But interfacing with a batch file that we know nothing about is kind of hard work.

Chris
imy2000Author Commented:
I rewrote the code in PS1. it works. As I said before, it was simple bat file that called scripts; i just changed the language.
shajidaliCommented:
As an additional Note I would like to inform you that you can very well use Windows Management Framework Core package (Windows PowerShell 2.0 and WinRM 2.0). With Remote Powershell you can connect to the server from your workstation and run any script from your machine but the affect will be as if running locally on the server.

http://support.microsoft.com/kb/968930
http://www.computerperformance.co.uk/powershell/powershell_remote.htm#Introduction_to_PowerShell_Remote
imy2000Author Commented:
The quick PS1 I wrote is running correctly.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
imy2000Author Commented:
I solved my own problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.