Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:58:08+00:00 2026-06-01T12:58:08+00:00

im writing a script to get services from local and remote machines. I’ve had

  • 0

im writing a script to get services from local and remote machines. I’ve had to split the wmi call for local and remote machines (remote machines require different credentials). I want to output them as System.Object. How do i create a function for the output of system.object?

heres the code i have so far:

$objServicecol = @()

    # how do i get AddService object back 
    Function AddServiceObjects
    {
            ForEach ($Service in $Services)
            {
                $objService = New-Object System.Object
                $objService | Add-Member -MemberType NoteProperty -Name SystemName -Value $Services.SystemName 
                $objService | Add-Member -MemberType NoteProperty -Name Name -Value $Services.Name 
                $objService | Add-Member -MemberType NoteProperty -Name StartMode -Value $Services.StartMode
                $objService | Add-Member -MemberType NoteProperty -Name StartName -Value $Services.StartName
                $objService | Add-Member -MemberType NoteProperty -Name Status -Value $Services.Status
                #$objServiceCol += $objService
                AddServiceObjects += $objService
            }
        }

    # Executes local WMI 
    If ($Servers -contains "localhost") 
    {
        $Services = Get-WMIObject Win32_Service -ComputerName "localhost" | Select-Object SystemName, Name, StartMode, StartName, Status
        AddServiceObjects $Services
    }
...#execute remote wmi...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:58:10+00:00Added an answer on June 1, 2026 at 12:58 pm

    In .NET, System.Object is the root of the inheritance hierarchy so any type you output can be treated as a System.Object. You might want to consider creating a psobject instead of a System.Object in your loop (and simplify it):

    Function AddServiceObjects($Services)
    {
        ForEach ($service in $Services)
        {
            New-Object psobject -Property @{
                SystemName = $service.SystemName
                Name       = $service.Name 
                StartMode  = $service.StartMode
                StartName  = $service.StartName
                Status     = $service.Status }
         }
    }
    

    Note that by virtue of not assigning the New-Object output to a variable it will get output from the function – one object for every iteration through the loop. Also note that your foreach iteration variable was $service but your were referencing the global $Services in your New-Object command.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a script in PHP to get data from the twitter API,
I'm writing a bash script to get data from procmail and produce an output
I'm writing an R script to get some database data and then do stuff
I'm writing a script that parses the pure-ftpwho -s command to get a list
I'm writing a Vim script. How can I get the word under the cursor
I'm writing script in remote.ini The script looks like on 1:start:{ server some.irc.server server
I am writing a script to get the details of all the scheduled tasks
I am writing a batch script and get a Allowed memory size of 134217728
I'm writing a bash script to get some podcasts. The problem is that some
I am writing a BHO, the code using IHTMLDocument2::get_scripts to get all script body

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.