PowerShell for Windows Admins

Dec 19 2009   7:13AM GMT

Using a script



Posted by: Richard Siddaway
Operating System

 

We have seen how to use our script to access information on the local machine. Ideally we want to be accessing this information from many machines across our enterprise. If we save the script to get-os.ps1 we can use it in a few ways.

if we just want information on a handful of computers we can pipe them into the script like this.

".", "127.0.0.1", "rslaptop01" | foreach {"`n $_"; .\Get-OS.ps1 -computer $_ }

I’m running these scripts from the folder where I saved the script which is why I need ./ to represent the path of the current folder.  $_ is the object on the pipeline and `n is the way a new line is thrown.  A ; is used as a line terminator in PowerShell. It is only really used when we are typing at the prompt like this.

Import-Csv ./computers.csv | foreach { "`n"; $_.Computer ; .\Get-OS.ps1 -computer $_.Computer}

If we have a lot of computers – and we want to use the list in a number of scripts we can create a csv file. use a single column with a header of computer like this

Computer
.
localhost
127.0.0.1
rslaptop01

We can input computer names or IP addresses.

Now we know how to access information about our machine’s OS its time to see what else we can do.

Comment on this Post

Leave a comment: