Link to home
Start Free TrialLog in
Avatar of imy2000
imy2000

asked on

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.
Avatar of KenMcF
KenMcF
Flag of United States of America image

Can you post an example of what you are trying to do? What is the batch file doing and what is the output?
Avatar of imy2000
imy2000

ASKER

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.
Avatar of imy2000

ASKER

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.
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

Avatar of imy2000

ASKER

I've already downloaded PS Tools. I am looking to try and find a script that will do what I want.
It will depend on wht the bat files are doing.
Avatar of imy2000

ASKER

THey are getting system info for each pc in the list
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.
Avatar of Qlemo
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).
Avatar of imy2000

ASKER

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.
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.

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
Avatar of imy2000

ASKER

what would be the command for Powershell WMI?
get-wmiobject -class win32_operatingsystem -computername COMPUTERNAME

there are other classes depending on what info you need.
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

Avatar of imy2000

ASKER

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?
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.

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
Avatar of imy2000

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of imy2000
imy2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of imy2000

ASKER

I solved my own problem.