How to create an collection/array/object/thingy from a bunch of objects for eventual export to CSV.


this follow-up earlier post (how output bunch of variables table.) can ouput data single computer need to same list of computers csv. in more general terms how bunch of $computer objects single collection/array/object/thingy clever name $computers?

# http://ss64.com/ps/get-wmiobject-win32.html  # http://social.technet.microsoft.com/forums/en-us/da54b6ab-6941-4e45-8697-1d3236ba2154/powershell-number-of-cpu-sockets-wmi-query?forum=winserverpowershell  # http://serverfault.com/questions/10328/determine-cpu-processors-vs-sockets-though-wmi  # http://social.technet.microsoft.com/forums/windowsserver/en-us/8443fcfd-5a0b-4c3d-bda7-26df83d2ee92/how-to-output-a-bunch-of-variables-into-a-table?forum=winserverpowershell    param(   	[parameter(mandatory=$false,valuefrompipeline=$true)]   	[string]$computername = $env:computername,    	[parameter(mandatory=$false,valuefrompipeline=$false)]   	[validatescript(  			{  			if ( $_ -ne $null )	{ test-path $_ }  			}  	)]  	[string]$computerlistfile  )      function get-computer  {  	param(   		[string]$computername  	)    	$win32_pingstatus			= $null  	$win32_pingstatus_result	= $null  	$win32_operatingsystem	= $null  	$win32_processor			= $null  	$win32_physicalmemory		= $null  	$win32_computersystem	= $null  	$win32_bios				= $null  	$computer				= $null    	$win32_pingstatus = "select * win32_pingstatus address = '$computername'"  	$win32_pingstatus_result = get-wmiobject -query $win32_pingstatus    	if ( $win32_pingstatus_result.protocoladdress )  	{  		"$computername ping succeeded."  			$win32_operatingsystem	=			get-wmiobject win32_operatingsystem		-computer $computername -erroraction silentlycontinue  		if ( $win32_operatingsystem -eq $null)  		{  			"$computername wmi failed."  		} else {  			"$computername wmi succeeded."  				$win32_processor		= [object[]]$(get-wmiobject win32_processor			-computer $computername)  				$win32_physicalmemory	= [object[]]$(get-wmiobject win32_physicalmemory	-computer $computername)  				$win32_computersystem	= 			get-wmiobject win32_computersystem		-computer $computername  				$win32_bios				=			get-wmiobject win32_bios				-computer $computername  			$computer = new-object -type psobject -property @{  				name											= $win32_operatingsystem.csname  				win32_bios_serialnumber							= [string]$win32_bios.serialnumber  				win32_computersystem_manufacturer				= [string]$win32_computersystem.manufacturer  				win32_computersystem_model						= [string]$win32_computersystem.model  				#win32_computersystem_numberoflogicalprocessors	= [int32]$win32_computersystem.numberoflogicalprocessors  				#win32_computersystem_numberofprocessors		= [int32]$win32_computersystem.numberofprocessors  				win32_computersystem_totalphysicalmemory		= [long]$win32_computersystem.totalphysicalmemory  				win32_computersystem_totalphysicalmemory_gb		= [float]($win32_computersystem.totalphysicalmemory / (1024*1024*1024))  				win32_operatingsystem_caption					= [string]$win32_operatingsystem.caption  				win32_operatingsystem_csname					= [string]$win32_operatingsystem.csname  				#win32_operatingsystem_osarchitecture			= [string]$win32_operatingsystem.osarchitecture  				#win32_operatingsystem_serialnumber				= [string]$win32_operatingsystem.serialnumber  				win32_operatingsystem_servicepackversion		= [string]$win32_operatingsystem.servicepackmajorversion + "." + [string]$win32_operatingsystem.servicepackminorversion  				win32_physicalmemory_capacity					= ($win32_physicalmemory | foreach-object { $_.capacity } | measure-object -sum).sum  				win32_physicalmemory_capacity_gb				= (($win32_physicalmemory | foreach-object { $_.capacity } | measure-object -sum).sum / (1024*1024*1024))  				win32_processor_count							= [int]$win32_processor.count  				win32_processor_numberofcores					= [string]$win32_processor[0].numberofcores  				win32_processor_numberoflogicalprocessors		= [string]$win32_processor[0].numberoflogicalprocessors  				#win32_processor_description					= [string]$win32_processor[0].description  				win32_processor_manufacturer					= [string]$win32_processor[0].manufacturer  				win32_processor_name							= [string]$win32_processor[0].name  			} ## end new-object  			$computer  		}  	} else {  			"$computername ping failed."   	}    	$computernamemgmt = $computername + "-mgmt"	  	$win32_pingstatus = "select * win32_pingstatus address = '$computernamemgmt'"  	$win32_pingstatus_result = get-wmiobject -query $win32_pingstatus    	if ( $win32_pingstatus_result.protocoladdress )  	{  			"$computernamemgmt ping succeded."   	} else {  			"$computernamemgmt ping failed."  	}    }    ""  "$(get-date -format o) starting script $($myinvocation.mycommand.name)"    	if ( $computerlistfile -eq $null -or $computerlistfile.length -eq 0 )  	{  		"processing computer $computername"  		""  		get-computer( $computername )  	} else {  		"processing computer list $computerlist"  		""  		$computerlist = get-content $computerlistfile  		$computers = @{}  		$results = @()  		"-----"  		foreach( $computerlistmember in $computerlist )  		{  			"$(get-date -format o) $computerlistmember"  #			get-computer( $computerlistmember ) | add-member -inputobject $computers -membertype noteproperty -name $_  # http://social.technet.microsoft.com/forums/windowsserver/en-us/e7d602a9-a808-4bbc-b6d6-dc78079aafc9/powershell-to-ping-computers  #			$compuers += new-object psobject -property $props  #			$computers += new-object psobject -property get-computer( $computerlistmember )  			get-computer( $computerlistmember )  			"-----"  		}  	}	    ""  "$(get-date -format o) ending script $($myinvocation.mycommand.name)"

if try this:

get-computer( $computerlistmember ) | add-member -inputobject $computers -membertype noteproperty -name $_

i following, though $_.name not null.

add-member : cannot bind argument parameter 'name' because null.  @ <path script>get-hardware_memory_osversion_cpu_cores_ver04_sanitized.ps1:111 char:107  +             get-computer( $computerlistmember ) | add-member -inputobject $computers -membertype noteproperty -name <<<<  $_      + categoryinfo          : invaliddata: (:) [add-member], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerrornullnotallowed,microsoft.powershell.commands.addmembercommand  

or if try this:

$computers += new-object psobject -property get-computer( $computerlistmember )

i this:

new-object : cannot bind parameter 'property'. cannot convert "get-computer" value of type "system.string" type "system.collections.hashtable".  @ <path script>get-hardware_memory_osversion_cpu_cores_ver04_sanitized.ps1:114 char:47  +             $computers += new-object psobject -property <<<<  get-computer( $computerlistmember )      + categoryinfo          : invalidargument: (:) [new-object], parameterbindingexception      + fullyqualifiederrorid : cannotconvertargumentnomessage,microsoft.powershell.commands.newobjectcommand  

hi aenagy,

if want combine computers' information single array, , add property computername in output, please try script below, make little modification of function get-computer, pleaese make sure account running script has admin permission of remote computers, or need privide cridentials in get-wmiobject, also note haven't tested:

$output = @()#to output information of computers foreach( $computername in $computerlist ){#loop computers  $win32_pingstatus = "select * win32_pingstatus address = '$computername'" $win32_pingstatus_result = get-wmiobject -query $win32_pingstatus if ( $win32_pingstatus_result.protocoladdress ) 	{ 		"$computername ping succeeded." 			$win32_operatingsystem	=			get-wmiobject win32_operatingsystem -computer $computername -erroraction silentlycontinue 		if ( $win32_operatingsystem -eq $null) 		{ 			"$computername wmi failed." 		}                  else { 			"$computername wmi succeeded." 				$win32_processor		= [object[]]$(get-wmiobject win32_processor -computer $computername) 				$win32_physicalmemory	= [object[]]$(get-wmiobject win32_physicalmemory -computer $computername) 				$win32_computersystem	= 			get-wmiobject win32_computersystem -computer $computername 				$win32_bios				=			get-wmiobject win32_bios -computer $computername 			$computer = new-object -type psobject -property @{                                 computername                                        = $computername  #add property computername                 			        name						    = $win32_operatingsystem.csname 				win32_bios_serialnumber				    = [string]$win32_bios.serialnumber 				win32_computersystem_manufacturer		    = [string]$win32_computersystem.manufacturer 				win32_computersystem_model			    = [string]$win32_computersystem.model 				#win32_computersystem_numberoflogicalprocessors	    = [int32]$win32_computersystem.numberoflogicalprocessors 				#win32_computersystem_numberofprocessors	    = [int32]$win32_computersystem.numberofprocessors 				win32_computersystem_totalphysicalmemory	    = [long]$win32_computersystem.totalphysicalmemory 				win32_computersystem_totalphysicalmemory_gb	    = [float]($win32_computersystem.totalphysicalmemory / (1024*1024*1024)) 				win32_operatingsystem_caption			    = [string]$win32_operatingsystem.caption 				win32_operatingsystem_csname			    = [string]$win32_operatingsystem.csname 				#win32_operatingsystem_osarchitecture		    = [string]$win32_operatingsystem.osarchitecture 				#win32_operatingsystem_serialnumber		    = [string]$win32_operatingsystem.serialnumber 				win32_operatingsystem_servicepackversion	    = [string]$win32_operatingsystem.servicepackmajorversion + "." + [string]$win32_operatingsystem.servicepackminorversion 				win32_physicalmemory_capacity			    = ($win32_physicalmemory | foreach-object { $_.capacity } | measure-object -sum).sum 				win32_physicalmemory_capacity_gb		    = (($win32_physicalmemory | foreach-object { $_.capacity } | measure-object -sum).sum / (1024*1024*1024)) 				win32_processor_count				    = [int]$win32_processor.count 				win32_processor_numberofcores			    = [string]$win32_processor[0].numberofcores 				win32_processor_numberoflogicalprocessors	    = [string]$win32_processor[0].numberoflogicalprocessors 				#win32_processor_description			    = [string]$win32_processor[0].description 				win32_processor_manufacturer			    = [string]$win32_processor[0].manufacturer 				win32_processor_name				    = [string]$win32_processor[0].name 			} ## end new-object 			$output+=$computer #combine "$computer" "$output" 		} 	}  else { 			"$computername ping failed."  }  } $output

if have feedback on our support, please click here.

best regards,

anna

technet community support





Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Group Policy Event ID 1058 Error Code 1326 (The user name or password is incorrect)

Suspicious event log Event ID: 4905

DCOM received error "2147746132" from...