Hi All,
I wrote a piece of code and I’d like for the $app results to come out a bit cleaner. It gets cut off if there are too many results. I’ve tried doing an Out-String, Format-Table, Format-List… but no dice
Below is my code. If you run it against an object that you have a lot of, say Microsoft, you’ll see what I’m referring to with the output being funky. I gave an example at the end by calling the function. I’m not looking for a flat out answer. Just a nudge in the right direction ![]()
Function Get-InstalledApp {
[cmdletbinding(SupportsShouldProcess=$true,ConfirmImpact='low')]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$App,
[string[]]$ComputerName,
[string]$ErrorLog = "C:\Users\$env:USERNAME\Desktop\AppError$(Get-date -Format MM.dd.yy).txt",
[switch]$LogErrors
)
begin { Write-Verbose "We will now begin collecting computers with the application that you have selected" }
process {
Write-Verbose "The process of collecting computers with your selected app will now begin"
Write-Verbose "Please use two asterisks if you're looking to filter multiple results. Example *Microsoft*"
TRY {
#Calling the -Class and -ComputerName parameter
$WMIObject_Params = @{
'Class'='Win32_Product'
'ComputerName'=$ComputerName
}
$EverythingOK = $true
Write-Verbose "Starting query now"
#Using Where-Object to do a wildcard search on apps you specify
$WMIResults = Get-CimInstance @WMIObject_Params |
Where-Object {$_.Name -like "*$App*"}
#Creating custom objects for a table
$ObjectProperties = @{
'ComputerName'=$ComputerName
'AppName'=$WMIResults.Name
}
$Object = New-Object -TypeName psobject -Property $ObjectProperties
Write-Output $Object
} CATCH {
$EverythingOK = $false
Write-Warning "The script failed. Please review the logs located on your desktop"
IF($LogErrors) {
#Specifying the first error that occurs in the error log
$ErrorOccured[0] | Out-File $ErrorLog }
Write-Verbose "The process is now complete. For any error messages, please go to your desktop and search for the ErrorReport log"
}#CATCH
}#Process
end{}
}#Function
Get-InstalledApp -App Microsoft -ComputerName $env:COMPUTERNAME