具有特定参数的PSObject错误

时间:2014-01-16 21:17:01

标签: powershell

我有一个powershell脚本,可以对远程服务器上的组成员身份进行一些简单的审核。输出是预期的,除了一组的情况。

这个脚本有两个参数,一个用于检入AD的OU和一个要检查的组名。 OU参数返回服务器名称列表,组名称是返回成员的组。这一切都很好,除了一个案例,备份操作员。

param([parameter(mandatory=$true)][string]$region,[string]$group)

### Debug flag for viewing output when running the script. 
$DEBUG = 1

$self = $myinvocation.mycommand.name

function cmdopts {
    if ($DEBUG) {
       write-host "$self running with options"
       write-host "Region: $region"
       write-host "Group: $group"
    }
 }

 ### Function to handle custom messages to the user. 
 function usageRegion {
     # Removed for brevity
 }

 function usageGroups {
     # Removed for brevity
 }

 ### Cleanup from previous run of the script.
 function cleanup {
    # Removed for brevity
 }

 #### Function to load powershell modules at runtime
 function loadmod { 
     param([string]$name)
     if ( -not(get-module -name $name)) {
         if (get-module -listavailable| where-object { $_.name -eq $name}) {
             import-module -name $name
             $true
         } else { 
             $false
         }
     } else { 
        $true
     }
  }


 ### Main()
 cmdopts

 #### Validate commandline options
 if ( "cnr","nwr","swr","ner","ser","emr","lar","apr" -notcontains $region ) {
     usageRegion
     exit
 }

 if ( "Administrators","Backup Operators","Event Log Readers","Hyper-V Administrators","Power Users",
"Print Operators","Remote Desktop Users" -notcontains $group) {
    usageGroups
    exit
 } else {
     ### We are creating three files for each run, previous runs need to be cleaned up before we start. 
     cleanup

    ### The ActiveDirectory module is a dependency for this script, we use it to get a list of machine names from AD for the OU.
    if ( loadmod -name "activedirectory" ) {
        write-host "Loading ActiveDirectory powershell module..." -foregroundcolor green
    } else {
        write-host "Sorry, you do not have the ActiveDirectory powershell module installed." -foregroundcolor yellow
        write-host "The script cannot contnue." -foregroundcolor yellow
        exit
    } 

    ### Get the list of servers from AD for the OU specified by the user. 
    get-adcomputer -f * -searchbase "ou=$region,ou=servers,dc=domain,dc=com" | select name | out-file "c:\scripts\ps\$region.srvtmp.txt" -append

    ### We need to fix some format issues with the file before continuing

    # Removed for brevity, cleans up the file output from get-adcomputer and sets variable $srvlist
    $srvlist = gc "c:\scripts\ps\$region.srvlist.txt"

    # Store for the return
    $store = @()

    # Fix the group string for the filename
    $filestring = $group
    $filestring = $filestring.replace(' ', '')
    $filestring = $filestring.tolower()

    foreach ( $srv in $srvlist ) {
        if ( $srv -eq "bustedserver" ) {
        # This box hangs and does not tear down WMI when it can't complete, timeout does not work
            write-host "skipping $srv"
        } else {
            $response = test-connection $srv -count 1 -quiet

            ### This does not work super well, might have to try a custom function
            if ($response -eq $false ) {    
                write-host "$srv was offline during test" -foregroundcolor darkmagenta
            } else {
                 write-host "Checking $group on " -nonewline; write-host $srv -foregroundcolor cyan

                 $groupinfo = new-object PSObject

                 $members = gwmi -computer $srv -query "SELECT * FROM Win32_GroupUser WHERE GroupComponent=`"Win32_Group.Domain='$srv',Name='$group'`"" 

                 $members = $members | sort-object -unique

                 $count = 0
                 if ($members -ne $null) {
                     add-member -inputobject $groupinfo -membertype noteproperty -name "Server" -value $srv
                     add-member -inputobject $groupinfo -membertype noteproperty -name "Group" -value $group

                     foreach ($member in $members) {
                         $count += 1
                         $data = $member.partcomponent -split "\,"
                         $domain = ($data[0] -split "=")[1]
                         $name = ($data[1] -split "=")[1]
                         $line = ("$domain\$name").replace("""","")
                         add-member -inputobject $groupinfo -membertype noteproperty -name "Member $count" -value $line
                     }
                  }

                 if ($DEBUG) {
                    write-host $groupinfo
                 }

                $store += $groupinfo
           }
       }
    }
 }

 #$store | export-csv -path "$HOME\desktop\$region-$filestring-audit.csv" -notype
 $store

如果我针对管理员或远程桌面用户等组运行此脚本,则输出如下所示。

Server: SERVER1
Group: Remote Desktop Users
Member1: GroupName1
Member2: GroupName2
Member3: GroupName3

如果我针对群组备份操作员运行此脚本,即使有很多,我也只会获得第一个群组。在debug write-host语句中,它将显示所有组。打印商店时,它只显示第一个商店。即使有两个或更多,它也会打印......

Server: SERVER1
Group: Backup Operators
Member1: GroupName1

有关为什么会针对“备份操作员”特别针对“备份操作员”而不是其他操作员的任何想法,我们将不胜感激。

0 个答案:

没有答案