Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Expand Powershell Script

Posted on 2010-11-22
10
Medium Priority
?
1,173 Views
Last Modified: 2012-05-10
Hello,

I built a Powershell script last week (with the very fine help from this site!!!) which could use a little improvement. So maybe anyone can help me with this?

At least the script should do the following:
- Test if a server from an input-file is available
- Query which operatings system is used
- Query which service pack level is applied
- Query if the OS is 32/64 Bit
- Query if SNMP service is installed
- Query if a user "testuser" is member of the local admin group
- Write out all the information to a csv file for Excel import

I hope I don´t ask too much !?! Any help would be great!
The basic code is attached...

Regards
Steffen

Get-Content -path "w:\servers.txt" | ForEach-Object {

  $ComputerName = $_

  Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName | Select-Object `
    @{n='Computer Name';e={ $ComputerName }},
    @{n='Operating System';e={ $_.Caption }},
    @{n='Servicepack';e={ $_.ServicePackMajorVersion }},
    @{n='SNMP';e={ Get-WmiObject Win32_service -ComputerName $ComputerName -Filter "Name='SNMP'" | Select-Object -ExpandProperty Name }},
    @{n='Architecture';e={ Get-WmiObject Win32_Processor -ComputerName $ComputerName | Select-Object -ExpandProperty Caption }}

} | Export-Csv "w:\SNoutfile1.csv" -NoTypeInformation

Open in new window

0
Comment
Question by:SSR-IS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 34188362
Hey :)

The first bit can use Test-Connection.

TestUser, is this a local user? Or a Domain User?

Chris
Get-Content -path "w:\servers.txt" | ForEach-Object {

  $ComputerName = $_

  # Test if a server from an input-file is available (ping)
  If (Test-Connection $ComputerName -Quiet -Count 2) {

    Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName | Select-Object `
      @{n='Computer Name';e={ $ComputerName }},
      @{n='Operating System';e={ $_.Caption }},
      @{n='Servicepack';e={ $_.ServicePackMajorVersion }},
      @{n='SNMP';e={ Get-WmiObject Win32_service -ComputerName $ComputerName -Filter "Name='SNMP'" | Select-Object -ExpandProperty Name }},
      @{n='Architecture';e={ Get-WmiObject Win32_Processor -ComputerName $ComputerName | Select-Object -ExpandProperty Caption }}

  }
} | Export-Csv "w:\SNoutfile1.csv" -NoTypeInformation

Open in new window

0
 

Author Comment

by:SSR-IS
ID: 34188579
Oh, the "TestUser" is a domain user.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 34188696

Cool. So, local groups (and users for that matter) are a bit of a pain, the interface isn't quite as advanced as would be nice. Fortunately, been there / done that :)

Modified to test the administrators group for that user account.

Chris
Function Get-AdminGroupMember {
  Param(
    [String]$ComputerName = $Env:ComputerName
  )

  ([ADSI]"WinNT://$ComputerName/Administrators").Members() | Select-Object `
    @{n='Name';e={ $_.GetType().InvokeMember('Name', 'GetProperty', $Null, $_, $Null) }},
    @{n='ADSPath';e={ $_.GetType().InvokeMember('ADSPath', 'GetProperty', $Null, $_, $Null) }},
    @{n='Class';e={ $_.GetType().InvokeMember('class', 'GetProperty', $Null, $_, $Null) }},
    @{n='Type';e={ "Direct" }}
}


Get-Content -path "w:\servers.txt" | ForEach-Object {

  $ComputerName = $_

  # Test if a server from an input-file is available (ping)
  If (Test-Connection $ComputerName -Quiet -Count 2) {

    Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName | Select-Object `
      @{n='Computer Name';e={ $ComputerName }},
      @{n='Operating System';e={ $_.Caption }},
      @{n='Servicepack';e={ $_.ServicePackMajorVersion }},
      @{n='SNMP';e={ Get-WmiObject Win32_service -ComputerName $ComputerName -Filter "Name='SNMP'" | Select-Object -ExpandProperty Name }},
      @{n='Architecture';e={ Get-WmiObject Win32_Processor -ComputerName $ComputerName | Select-Object -ExpandProperty Caption }},
      @{n='TestUserIsAdmin';e={ 
        If ((Get-AdminGroupMember $ComputerName) | Where-Object { $_.Name -Like "testuser" }) { $True } Else { $False } }}
  }
} | Export-Csv "w:\SNoutfile1.csv" -NoTypeInformation

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

Author Comment

by:SSR-IS
ID: 34188853
This works so far in my test environment. Thanks!

The was another thing with my further script. For some servers I got an access denied and sometimes "RPC Server not available". Is it possible to document this error messages in the csv file? Just to know why the connection didn´t work.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 34188906

Sure.

I added a note to catch ping failures as well. It "should" work :)

Chris
Function Get-AdminGroupMember {
  Param(
    [String]$ComputerName = $Env:ComputerName
  )

  ([ADSI]"WinNT://$ComputerName/Administrators").Members() | Select-Object `
    @{n='Name';e={ $_.GetType().InvokeMember('Name', 'GetProperty', $Null, $_, $Null) }},
    @{n='ADSPath';e={ $_.GetType().InvokeMember('ADSPath', 'GetProperty', $Null, $_, $Null) }},
    @{n='Class';e={ $_.GetType().InvokeMember('class', 'GetProperty', $Null, $_, $Null) }},
    @{n='Type';e={ "Direct" }}
}

Get-Content -path "w:\servers.txt" | ForEach-Object {

  $ComputerName = $_

  # Test if a server from an input-file is available (ping)
  If (Test-Connection $ComputerName -Quiet -Count 2) {

    $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName

    If ($?) {

      # If the command worked
 
      $OperatingSystem | Select-Object `
        @{n='Computer Name';e={ $ComputerName }},
        @{n='Status';e={ "OK" }},
        @{n='Operating System';e={ $_.Caption }},
        @{n='Servicepack';e={ $_.ServicePackMajorVersion }},
        @{n='SNMP';e={ Get-WmiObject Win32_service -ComputerName $ComputerName -Filter "Name='SNMP'" | Select-Object -ExpandProperty Name }},
        @{n='Architecture';e={ Get-WmiObject Win32_Processor -ComputerName $ComputerName | Select-Object -ExpandProperty Caption }},
        @{n='TestUserIsAdmin';e={ 
          If ((Get-AdminGroupMember $ComputerName) | Where-Object { $_.Name -Like "testuser" }) { $True } Else { $False } }}

    } Else 

      # If the command failed
      
      "" | Select-Object `
        @{n='ComputerName';e={ $ComputerName }},
        @{n='Status';e={ "WMI Connect Fail" }},
        "Operating System", ServicePack, SNMP, Architecture, TestUserIsAdmin

    }
  } Else {

    # If ping failed
  
    "" | Select-Object `
      @{n='ComputerName';e={ $ComputerName }},
      @{n='Status';e={ "Ping Fail" }},
      "Operating System", ServicePack, SNMP, Architecture, TestUserIsAdmin
  }
} | Export-Csv "w:\SNoutfile1.csv" -NoTypeInformation

Open in new window

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 34189011

Perhaps I should just add a quick note about this.

Export-Csv expects everything we return to have the same properties, otherwise it gets all confused and might hide stuff.

A property of the return object in this case would be a field like "Operating System". If this is not present in all of our different return values (the working one, WMI Conenct Fail, and Ping Fail) then things will disappear. Quite frustrating if you don't know about it.

Chris
0
 

Author Comment

by:SSR-IS
ID: 34189017
Thanks a lot Chris-Dent. I´ll give it a try in out test environment and then let it run in production!
I´ll give you some feedback.

Steffen
0
 

Author Comment

by:SSR-IS
ID: 34189214
Uh, I´m sorry, but I got some errors running the script:
Missing statement block after 'else' keyword.
At line:40 char:7

Missing statement after '=' in hash literal.
At line:41 char:47

Missing statement after '=' in hash literal.
At line:42 char:46

Missing statement after '=' in hash literal.
At line:51 char:45

Missing statement after '=' in hash literal.
At line:52 char:37

Unexpected token '}' in expression or statement.
At line:55 char:1
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 34189236

Apologies. Missed a {, I can't see another, I think the first error caused the remainder.

Yell if not, of course :)

Chris
Function Get-AdminGroupMember {
  Param(
    [String]$ComputerName = $Env:ComputerName
  )

  ([ADSI]"WinNT://$ComputerName/Administrators").Members() | Select-Object `
    @{n='Name';e={ $_.GetType().InvokeMember('Name', 'GetProperty', $Null, $_, $Null) }},
    @{n='ADSPath';e={ $_.GetType().InvokeMember('ADSPath', 'GetProperty', $Null, $_, $Null) }},
    @{n='Class';e={ $_.GetType().InvokeMember('class', 'GetProperty', $Null, $_, $Null) }},
    @{n='Type';e={ "Direct" }}
}

Get-Content -path "w:\servers.txt" | ForEach-Object {

  $ComputerName = $_

  # Test if a server from an input-file is available (ping)
  If (Test-Connection $ComputerName -Quiet -Count 2) {

    $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName

    If ($?) {

      # If the command worked
 
      $OperatingSystem | Select-Object `
        @{n='Computer Name';e={ $ComputerName }},
        @{n='Status';e={ "OK" }},
        @{n='Operating System';e={ $_.Caption }},
        @{n='Servicepack';e={ $_.ServicePackMajorVersion }},
        @{n='SNMP';e={ Get-WmiObject Win32_service -ComputerName $ComputerName -Filter "Name='SNMP'" | Select-Object -ExpandProperty Name }},
        @{n='Architecture';e={ Get-WmiObject Win32_Processor -ComputerName $ComputerName | Select-Object -ExpandProperty Caption }},
        @{n='TestUserIsAdmin';e={ 
          If ((Get-AdminGroupMember $ComputerName) | Where-Object { $_.Name -Like "testuser" }) { $True } Else { $False } }}

    } Else {

      # If the command failed
      
      "" | Select-Object `
        @{n='ComputerName';e={ $ComputerName }},
        @{n='Status';e={ "WMI Connect Fail" }},
        "Operating System", ServicePack, SNMP, Architecture, TestUserIsAdmin

    }
  } Else {

    # If ping failed
  
    "" | Select-Object `
      @{n='ComputerName';e={ $ComputerName }},
      @{n='Status';e={ "Ping Fail" }},
      "Operating System", ServicePack, SNMP, Architecture, TestUserIsAdmin
  }
} | Export-Csv "w:\SNoutfile1.csv" -NoTypeInformation

Open in new window

0
 

Author Comment

by:SSR-IS
ID: 34194727
Hi Chirs,

the script was running very fine last night! As you expected, after a wmi- or ping connection error, the CSV-file missed the servername. But this was no problem at all because by importing it into my Excel-Listíng the lines matched.

Thanks a lot for your help! I might come up with some SNMP-Installion-Script questions later today ;-))
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question