Using the output of a function within a function

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Using the output of a function within a function

Post 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]
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Using the output of a function within a function

Post 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.
User avatar
CermakPOI
Posts: 41
Last visit: Wed Jan 31, 2024 2:57 am

Using the output of a function within a function

Post 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
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Using the output of a function within a function

Post 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
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Using the output of a function within a function

Post 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?
This topic is 11 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked