Page 1 of 1

Using the output of a function within a function

Posted: Mon Jan 07, 2013 4:31 am
by sekou2331
I have a function that creates a list of IP addresses. I want to take that function and have another function read the list and do something. Maybe I do not need a Function at all but I still want to take the output and run it against another script. the code is below.

Code: Select all

 
	
	function Computers {
	
	$NY = 1..255 | Foreach-Object { "109.19.53.$_" }
	$CHI = 1..255 | Foreach-Object { "109.1.18.$_" }
	$BOS = 1..255 | Foreach-Object { "109.1.15.$_" }
	$SAN = 1..255 | Foreach-Object { "109.35.6.$_" }
	
	$AllofDesk = $SAN , $NY, $CHI, $BOS | Format-List 
	}
	
	function DeskInstalls{
	Computers |  
	ForEach-Object{ 
		
		if (Test-Path "$_C$Program Fi*Adobe") {
			
		
	$OS=(Get-wmiobject win32_operatingsystem -ComputerName $_ | Select-Object caption).Caption
	$Comptr=(Get-wmiobject Win32_ComputerSystem -ComputerName $_).Name
	$username=(Get-wmiobject Win32_ComputerSystem -ComputerName $_).UserName
	. Get-Item "$_C$Program Fi*AdobeAcrobat 9.0AcrobatAcrobatInfo.exe" 
	
		}
		
			}  |  Select @{N='ComputerName';E={$Comptr}}, @{N='OS';E={$OS}}, @{N='UserName';E={$username}}, @{N='Name';E={$_.Name}}, @{N='Version';E={$_.VersionInfo.ProductVersion}} | Format-Table -AutoSize
			
	}
	
	DeskInstalls 
	
	[Code]

Using the output of a function within a function

Posted: Mon Jan 07, 2013 7:37 am
by jvierra
You need to spend some time studying how to design functions and scripts. YOu are just taking some arbitrary commandline code and wrapping it in a function block. This may work but it is not very useful.

function Create-ComputerIPs {
. 1..255 | Foreach-Object { "109.19.53.$_" }
. 1..255 | Foreach-Object { "109.1.18.$_" }
. 1..255 | Foreach-Object { "109.1.15.$_" }
. 1..255 | Foreach-Object { "109.35.6.$_" }
}

$results=Create-Comp;uterIPs

Get-wmiobject win32_operatingsystem -ComputerName (Get-ComputerIPs)| Select-Object caption

This demos how the functioninteracts with the environment.

To really understand how to do this you need to clealry define the purpose of what you are doing. If you just want a reusable list then use $results


Get-wmiobject win32_operatingsystem -ComputerName $results

Create a function that does everything you want to one IP then call it with an array of IPs.

Using the output of a function within a function

Posted: Tue Jan 08, 2013 11:39 pm
by CermakPOI
If you want to use function output, you should not pipe the output into format list, since this is a string format.
You should use it a natively as possible to preserve the object properties.
Your function does not return anything, right? Thats because you'rs saving the outoput into a variable thats gone after the function call. Use something like this (See the usefull name?) to list your computer IPs.

function List-ComputerIPs {

$NY = 1..255 | Foreach-Object { "109.19.53.$_" }
$CHI = 1..255 | Foreach-Object { "109.1.18.$_" }
$BOS = 1..255 | Foreach-Object { "109.1.15.$_" }
$SAN = 1..255 | Foreach-Object { "109.35.6.$_" }

return @($SAN , $NY, $CHI, $BOS)
}

$MyIPList = List-ComputerIPs

You might also considering changing the main loop from a piped foreach to a real foreach, and creating a Object array, that makes the handling of your complex wmi-call easier and the data is not just displayed but also (re-) usable.
I would also use the Version instead of the Caption:
Version : 6.1.7601

$MyList = @()
ForEach ($IPNow in $MyIPList ) {
$objAdobe = "" | Select-Object OS,Comptr,username,Item
if (Test-Path "$IPNow C$Program Fi*Adobe") {

$objAdobe.OS=(Get-wmiobject win32_operatingsystem -ComputerName $IPNow | Select-Object Version).Version

$objAdobe.Comptr=(Get-wmiobject Win32_ComputerSystem -ComputerName $IPNow ).Name

$objAdobe.username=(Get-wmiobject Win32_ComputerSystem -ComputerName $IPNow ).UserName
$objAdobe.Item = . Get-Item "$_C$Program Fi*AdobeAcrobat 9.0AcrobatAcrobatInfo.exe"
$MyList += $objAdobe
}
}
$MyList

Using the output of a function within a function

Posted: Thu Jan 10, 2013 3:21 am
by sekou2331
your script does not output anything. I have been working on this and i am trying to gather informtion on different plug-ins and software version. I wrote the script below as a full function. it outputs the information in a way that i need but from my understanding it will not be (re-) usable. It porduces th output below. It gives to me by computer. So it gives the information of all software on one computer outputs the information then gives me the next computers information. Please keep in mind that i am still fairly new at this and teaching myself by reading so I may have missed a step somewhere.

Code: Select all

	
	function Computers {
	
	$NY = 1..255 | Foreach-Object { "109.19.53.$_" }
	$CHI = 1..255 | Foreach-Object { "109.1.18.$_" }
	$BOS = 1..255 | Foreach-Object { "109.1.15.$_" }
	$SAN = 1..255 | Foreach-Object { "109.35.6.$_" }
	
	$AllofDesk = $SAN , $NY, $CHI, $BOS | Format-List 
	}
	foreach ($ip in $pinglist)  
			{$result = Get-WmiObject Win32_PingStatus -filter "address='$IP'and BufferSize=32 and Timeout=2000"
					if ($result.statuscode -eq 0)	
	{ 
	"$iP" |ForEach-Object{ 
		
		if (Test-Path "$_C$Program Fi*") {
			
		
	$OS=(Get-wmiobject win32_operatingsystem -ComputerName $_ | Select-Object caption).Caption
	$Comptr=(Get-wmiobject Win32_ComputerSystem -ComputerName $_).Name
	$username=(Get-wmiobject Win32_ComputerSystem -ComputerName $_).UserName
	. Get-Item "$_C$Program Fi*Software version
	. Get-Item "$_C$Program Fi*Software version
	. Get-Item "$_C$Program Fi*Software version
	. Get-Item "$_C$Program Fi*Software version
	. Get-Item "$_C$Program Fi*Software version
	. Get-Item "$_C$Program Fi*Software version
	. Get-Item "$_C$Program Fi*Software version
	. Get-Item "$_C$Program Fi*Software version
	. Get-Item "$_C$Program Fi*Software version
		}
		
			}  |  Select @{N='ComputerName';E={$Comptr}}, @{N='OS';E={$OS}}, @{N='UserName';E={$username}}, @{N='Name';E={$_.Name}}, @{N='Version';E={$_.VersionInfo.ProductVersion}}, LastWriteTime| Format-Table -AutoSize 
	           	 	
		} 
	
	       }	  
	
	              } 
	
	
	
	
	Computers
	
	
	[OUTPUT]
	ComputerName OS                                UserName   Name                        Version              LastWriteTime
	------------ --                                --------   ----                        -------              -------------
	Comput     Microsoft Windows XP Professional ITGUser Software                9.4.0.8  11/5/2012 1:49:08 PM
	
	
	
	ComputerName OS                                UserName   Name                        Version              LastWriteTime
	------------ --                                --------   ----                        -------              -------------
	Comput2     Microsoft Windows XP Professional ITGUser Software                5.4.0.8 Build 23839  11/5/2012 1:49:08 PM

Using the output of a function within a function

Posted: Sat Jan 12, 2013 3:54 am
by sekou2331
torpedro,
I got an output from your script when not using the function part. So my question is why does it not work with the list of IP's in memory. Am I missing something?