Get-ADComputer -filter * -property $ Variable

时间:2017-12-01 12:50:20

标签: powershell

真的让这个感到困惑。我有一个Windows表单,其中包含基于AD属性名称的复选框。一旦运行正常:

$ADVARIABLES = @()
Get-Variable -Name OBJ* | ?{ $_.Value.Checkstate -eq 1  } | %{ $ADVARIABLES += $_.Value.Name }

获取属性名称。

如果我跑:

Get-ADUser -Filter * -Property $ADVARIABLES | select $ADVARIABLES

我得到了我期望看到的那种结果。

如果我跑:

Get-ADComputer -Filter * -Property $ADVARIABLES | select $ADVARIABLES

我明白了:

  

Get-ADComputer:无法验证参数'Properties'的参数。参数为null或空。提供一个非null或空的参数,然后再次尝试该命令。

Get-AdUser复选框上的属性包括:

邮件,经理,部门,标题,CanonicalName,电话,移动,HomeDirectory

PC的属性是:

SamAccountName,CanonicalName,IPv4Address,Enabled,OperatingSystem

有什么想法吗? $ADVARIABLES标签上绝对没有流氓属性,这只是屁股中的皇室痛苦。

修改

这是完整的脚本。适用于AD用户,不适用于计算机:

# Adds .NET assemby's and turns on visual themes in standard PowerShell.
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()



# Creates the parent form
$ADREPORT               = New-Object System.Windows.Forms.Form
$ADREPORT.Location      = New-Object System.Drawing.Size( 270,175 )
$ADREPORT.Size          = New-Object System.Drawing.Size( 800,400 )
$ADREPORT.StartPosition = "CenterScreen"
$ADREPORT.BackColor     = "Lavender"
$ADREPORT.Font          = "Calibri, 8.5"


# This creates a label for the title
$ADHEADER               = New-Object System.Windows.Forms.Label
$ADHEADER.Location      = New-Object System.Drawing.Size( 30,20 )
$ADHEADER.Size          = New-Object System.Drawing.Size( 180,20 )
$ADHEADER.Font          = New-Object System.Drawing.Font( "Calibri",11,[System.Drawing.FontStyle]::Bold )
$ADHEADER.Text          = "Choose your attributes"
$ADREPORT.Controls.Add( $ADHEADER )


# This creates a label for the sub-header
$ADTEXT                 = New-Object System.Windows.Forms.Label
$ADTEXT.Location        = New-Object System.Drawing.Size( 30,50 )
$ADTEXT.Size            = New-Object System.Drawing.Size( 300,40 )
$ADTEXT.Text            = "From the list below, select as many attributes as you like to generate a CSV report:"
$ADREPORT.Controls.Add( $ADTEXT )


# Active Directory Users label
$OBJUSERTXT             = New-Object System.Windows.Forms.Label
$OBJUSERTXT.Location    = New-Object System.Drawing.Size( 20,100 )
$OBJUSERTXT.Size        = New-Object System.Drawing.Size( 80,20 )
$OBJUSERTXT.Text        = "USERS:"
$ADREPORT.Controls.Add( $OBJUSERTXT ) 

# Active Directory Users checkbox - Checked by default
$USERBOX                = New-Object System.Windows.Forms.Checkbox
$USERBOX.Location       = New-Object System.Drawing.Size( 100,98 )
$USERBOX.Size           = New-Object System.Drawing.Size( 20,20 )
$USERBOX.TabIndex       = 0
$USERBOX.Checked        = $TRUE
$ADREPORT.Controls.Add( $USERBOX )



# Active Directory Computers label
$PCXT                   = New-Object System.Windows.Forms.Label
$PCXT.Location          = New-Object System.Drawing.Size( 150,100 )
$PCXT.Size              = New-Object System.Drawing.Size( 80,20 )
$PCXT.Text              = "COMPUTERS:"
$ADREPORT.Controls.Add( $PCXT )

# Active Directory Computers checkbox
$OBJPCBOX               = New-Object System.Windows.Forms.Checkbox
$OBJPCBOX.Location      = New-Object System.Drawing.Size( 230,98 )
$OBJPCBOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJPCBOX.TabIndex      = 9
$ADREPORT.Controls.Add( $OBJPCBOX )



# SamAccountName label
$OBJ01TXT               = New-Object System.Windows.Forms.Label
$OBJ01TXT.Location      = New-Object System.Drawing.Size( 20,120 )
$OBJ01TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ01TXT.Text          = "Username:"
$ADREPORT.Controls.Add( $OBJ01TXT ) 

# SamAccountName checkbox
$OBJ01BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ01BOX.Location      = New-Object System.Drawing.Size( 100,118 )
$OBJ01BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ01BOX.TabIndex      = 1
$OBJ01BOX.Checked       = $TRUE
$OBJ01BOX.Name          = "SamAccountName"
$ADREPORT.Controls.Add( $OBJ01BOX )



# Full Name label
$OBJ02TXT               = New-Object System.Windows.Forms.Label
$OBJ02TXT.Location      = New-Object System.Drawing.Size( 20,140 )
$OBJ02TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ02TXT.Text          = "Full name:"
$ADREPORT.Controls.Add( $OBJ02TXT )

# Full Name checkbox
$OBJ02BOX               = New-Object System.Windows.Forms.Checkbox 
$OBJ02BOX.Location      = New-Object System.Drawing.Size( 100,138 )
$OBJ02BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ02BOX.TabIndex      = 2
$OBJ02BOX.Name          = "Name"
$ADREPORT.Controls.Add( $OBJ02BOX )



# First Name label
$OBJ03TXT               = New-Object System.Windows.Forms.Label
$OBJ03TXT.Location      = New-Object System.Drawing.Size( 20,160 )
$OBJ03TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ03TXT.Text          = "First name:"
$ADREPORT.Controls.Add( $OBJ03TXT )

# First Name checkbox
$OBJ03BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ03BOX.Location      = New-Object System.Drawing.Size( 100,158 )
$OBJ03BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ03BOX.TabIndex      = 3
$OBJ03BOX.Name          = "GivenName"
$ADREPORT.Controls.Add( $OBJ03BOX )




# Surname label
$OBJ04TXT               = New-Object System.Windows.Forms.Label
$OBJ04TXT.Location      = New-Object System.Drawing.Size( 20,180 )
$OBJ04TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ04TXT.Text          = "Surname:"
$ADREPORT.Controls.Add( $OBJ04TXT )

# Surname checkbox
$OBJ04BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ04BOX.Location      = New-Object System.Drawing.Size( 100,178 )
$OBJ04BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ04BOX.TabIndex      = 4
$OBJ04BOX.Name          = "Surname"
$ADREPORT.Controls.Add( $OBJ04BOX )




# Department label
$OBJ05TXT               = New-Object System.Windows.Forms.Label
$OBJ05TXT.Location      = New-Object System.Drawing.Size( 20,200 )
$OBJ05TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ05TXT.Text          = "Department:"
$ADREPORT.Controls.Add( $OBJ05TXT )

# Department checkbox
$OBJ05BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ05BOX.Location      = New-Object System.Drawing.Size( 100,198 )
$OBJ05BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ05BOX.TabIndex      = 5
$OBJ05BOX.Name          = "Department"
$ADREPORT.Controls.Add( $OBJ05BOX )




# Job Title label
$OBJ06TXT               = New-Object System.Windows.Forms.Label
$OBJ06TXT.Location      = New-Object System.Drawing.Size( 20,220 )
$OBJ06TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ06TXT.Text          = "Job Title:"
$ADREPORT.Controls.Add( $OBJ06TXT )

# Job Title checkbox
$OBJ06BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ06BOX.Location      = New-Object System.Drawing.Size( 100,218 )
$OBJ06BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ06BOX.TabIndex      = 6
$OBJ06BOX.Name          = "Title"
$ADREPORT.Controls.Add( $OBJ06BOX )




# Manager Label
$OBJ07TXT               = New-Object System.Windows.Forms.Label
$OBJ07TXT.Location      = New-Object System.Drawing.Size( 20,240 )
$OBJ07TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ07TXT.Text          = "Manager:"
$ADREPORT.Controls.Add( $OBJ07TXT )

# Manager checkbox
$OBJ07BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ07BOX.Location      = New-Object System.Drawing.Size( 100,238 )
$OBJ07BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ07BOX.TabIndex      = 7
$OBJ07BOX.Name          = "Manager"
$ADREPORT.Controls.Add( $OBJ07BOX )




# IP Address label
$OBJ18TXT               = New-Object System.Windows.Forms.Label
$OBJ18TXT.Location      = New-Object System.Drawing.Size( 20,280 )
$OBJ18TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ18TXT.Text          = "IP Address:"
$ADREPORT.Controls.Add( $OBJ18TXT )

# IP Address checkbox
$OBJ18BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ18BOX.Location      = New-Object System.Drawing.Size( 100,278 )
$OBJ18BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ18BOX.TabIndex      = 19
$OBJ18BOX.Name          = "IPv4Address"
$OBJ18BOX.Enabled       = $FALSE
$ADREPORT.Controls.Add( $OBJ18BOX )



# Home Drive Label
$OBJ09TXT               = New-Object System.Windows.Forms.Label
$OBJ09TXT.Location      = New-Object System.Drawing.Size( 150,120 )
$OBJ09TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ09TXT.Text          = "Home Drive:"
$ADREPORT.Controls.Add( $OBJ09TXT )

# Home Drive Checkbox
$OBJ09BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ09BOX.Location      = New-Object System.Drawing.Size( 230,118 )
$OBJ09BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ09BOX.TabIndex      = 10
$OBJ09BOX.Name          = "HomeDirectory"
$ADREPORT.Controls.Add( $OBJ09BOX )




# Email label
$OBJ10TXT               = New-Object System.Windows.Forms.Label
$OBJ10TXT.Location      = New-Object System.Drawing.Size( 150,140 )
$OBJ10TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ10TXT.Text          = "Email:"
$ADREPORT.Controls.Add( $OBJ10TXT )

# Email checkbox
$OBJ10BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ10BOX.Location      = New-Object System.Drawing.Size( 230,138 )
$OBJ10BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ10BOX.TabIndex      = 11
$OBJ10BOX.Name          = "Mail"
$ADREPORT.Controls.Add( $OBJ10BOX )



# Phone label
$OBJ11TXT               = New-Object System.Windows.Forms.Label
$OBJ11TXT.Location      = New-Object System.Drawing.Size( 150,160 )
$OBJ11TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ11TXT.Text          = "Phone:"
$ADREPORT.Controls.Add( $OBJ11TXT )

# Phone checkbox
$OBJ11BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ11BOX.Location      = New-Object System.Drawing.Size( 230,158 )
$OBJ11BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ11BOX.TabIndex      = 12
$OBJ11BOX.Name          = "OfficePhone"
$ADREPORT.Controls.Add( $OBJ11BOX )



# Mobile phone label
$OBJ12TXT               = New-Object System.Windows.Forms.Label
$OBJ12TXT.Location      = New-Object System.Drawing.Size( 150,180 )
$OBJ12TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ12TXT.Text          = "Mobile:"
$ADREPORT.Controls.Add( $OBJ12TXT )

# Mobile phone checkbox
$OBJ12BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ12BOX.Location      = New-Object System.Drawing.Size( 230,178 )
$OBJ12BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ12BOX.TabIndex      = 13
$OBJ12BOX.Name          = "MobilePhone"
$ADREPORT.Controls.Add( $OBJ12BOX )




# Created label
$OBJ13TXT               = New-Object System.Windows.Forms.Label
$OBJ13TXT.Location      = New-Object System.Drawing.Size( 150,200 )
$OBJ13TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ13TXT.Text          = "Creation Date:"
$ADREPORT.Controls.Add( $OBJ13TXT )

# Created checkbox
$OBJ13BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ13BOX.Location      = New-Object System.Drawing.Size( 230,198 )
$OBJ13BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ13BOX.TabIndex      = 14
$OBJ13BOX.Name          = "Created"
$ADREPORT.Controls.Add( $OBJ13BOX )



# Modified label
$OBJ14TXT               = New-Object System.Windows.Forms.Label
$OBJ14TXT.Location      = New-Object System.Drawing.Size( 150,220 )
$OBJ14TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ14TXT.Text          = "Modified Date:"
$ADREPORT.Controls.Add( $OBJ14TXT )

# Modified checkbox
$OBJ14BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ14BOX.Location      = New-Object System.Drawing.Size( 230,218 )
$OBJ14BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ14BOX.TabIndex      = 15
$OBJ14BOX.Name          = "Modified"
$ADREPORT.Controls.Add( $OBJ14BOX )



# Password label
$OBJ15TXT               = New-Object System.Windows.Forms.Label
$OBJ15TXT.Location      = New-Object System.Drawing.Size( 150,240 )
$OBJ15TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ15TXT.Text          = "Password Set:"
$ADREPORT.Controls.Add( $OBJ15TXT )

# Password checkbox
$OBJ15BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ15BOX.Location      = New-Object System.Drawing.Size( 230,238 )
$OBJ15BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ15BOX.TabIndex      = 16
$OBJ15BOX.Name          = "PasswordLastSet"
$ADREPORT.Controls.Add( $OBJ15BOX )



# Enabled label
$OBJ08TXT               = New-Object System.Windows.Forms.Label
$OBJ08TXT.Location      = New-Object System.Drawing.Size( 150,260 )
$OBJ08TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ08TXT.Text          = "Enabled Status:"
$ADREPORT.Controls.Add( $OBJ08TXT )

# Enable checkbox
$OBJ08BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ08BOX.Location      = New-Object System.Drawing.Size( 230,258 )
$OBJ08BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ08BOX.TabIndex      = 17
$OBJ08BOX.Name          = "Enabled"
$ADREPORT.Controls.Add( $OBJ08BOX )



#This creates a label for the 18th attribute - Operating System
$OBJ17TXT               = New-Object System.Windows.Forms.Label
$OBJ17TXT.Location      = New-Object System.Drawing.Size( 150,280 )
$OBJ17TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ17TXT.Text          = "OS:"
$ADREPORT.Controls.Add( $OBJ17TXT )

#This creates the checkbox for the 18th attribute - Operating System
$OBJ17BOX               = New-Object System.Windows.Forms.Checkbox
 $OBJ17BOX.Location     = New-Object System.Drawing.Size( 230,278 )
$OBJ17BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ17BOX.TabIndex      = 18
 $OBJ17BOX.Name         = "OperatingSystem"
 $OBJ17BOX.Enabled      = $FALSE
$ADREPORT.Controls.Add( $OBJ17BOX )



# OU Label
$OBJ16TXT               = New-Object System.Windows.Forms.Label
$OBJ16TXT.Location      = New-Object System.Drawing.Size( 20,260 )
$OBJ16TXT.Size          = New-Object System.Drawing.Size( 80,20 )
$OBJ16TXT.Text          = "OU:"
$ADREPORT.Controls.Add( $OBJ16TXT )

# OU Checkbox
$OBJ16BOX               = New-Object System.Windows.Forms.Checkbox
$OBJ16BOX.Location      = New-Object System.Drawing.Size( 100,258 )
$OBJ16BOX.Size          = New-Object System.Drawing.Size( 20,20 )
$OBJ16BOX.TabIndex      = 8
$OBJ16BOX.Name          = "CanonicalName"
$ADREPORT.Controls.Add( $OBJ16BOX )




# OK Button
$OK                     = New-Object System.Windows.Forms.Button
$OK.Location            = New-Object System.Drawing.Size( 30,315 )
$OK.Size                = New-Object System.Drawing.Size( 75,23 )
$OK.ForeColor           = "Green"
$OK.Text                = "OK"
$OK.DialogResult        = "OK" 
$OK.add_Click( { $ADREPORT.Close() } ) 
$ADREPORT.Controls.Add( $OK )

# Cancel button
$CANCEL                 = New-Object System.Windows.Forms.Button
$CANCEL.Location        = New-Object System.Drawing.Point( 160,315 )
$CANCEL.Size            = New-Object System.Drawing.Size( 75,23 )
$CANCEL.ForeColor       = "Red"
$CANCEL.Text            = "Cancel"
$CANCEL.DialogResult    = "Cancel"
$ADREPORT.Controls.Add( $CANCEL )



# If PC checkbox ticked, grey out user specific attributes
$OBJPCBOX.Add_CheckStateChanged( {
IF ( $OBJPCBOX.Checked )
     { $USERBOX.Checked    = $FALSE                                  # AD User box
         $OBJ02BOX.Enabled = $FALSE ; $OBJ02BOX.Checked = $FALSE     # Full name
         $OBJ03BOX.Enabled = $FALSE ; $OBJ03BOX.Checked = $FALSE     # First name
         $OBJ04BOX.Enabled = $FALSE ; $OBJ04BOX.Checked = $FALSE     # Surname
         $OBJ05BOX.Enabled = $FALSE ; $OBJ05BOX.Checked = $FALSE     # Department
         $OBJ06BOX.Enabled = $FALSE ; $OBJ06BOX.Checked = $FALSE     # Job Title
         $OBJ07BOX.Enabled = $FALSE ; $OBJ07BOX.Checked = $FALSE     # Manager
         $OBJ09BOX.Enabled = $FALSE ; $OBJ09BOX.Checked = $FALSE     # HomeDirectory
         $OBJ10BOX.Enabled = $FALSE ; $OBJ10BOX.Checked = $FALSE     # Mail
         $OBJ11BOX.Enabled = $FALSE ; $OBJ11BOX.Checked = $FALSE     # Phone
         $OBJ12BOX.Enabled = $FALSE ; $OBJ12BOX.Checked = $FALSE     # Mobile
         $OBJ17BOX.Enabled = $TRUE  ; $OBJ18BOX.Enabled = $TRUE }    # OS & IP Address

# IF User box ticked, untick PC box and un-grey user specific attributes
IF ( $OBJPCBOX.Checked -eq $FALSE ) 
     { $USERBOX.Checked    = $TRUE  ; $OBJ02BOX.Enabled = $TRUE
         $OBJ03BOX.Enabled = $TRUE  ; $OBJ04BOX.Enabled = $TRUE
         $OBJ05BOX.Enabled = $TRUE  ; $OBJ06BOX.Enabled = $TRUE
         $OBJ07BOX.Enabled = $TRUE  ; $OBJ09BOX.Enabled = $TRUE
         $OBJ10BOX.Enabled = $TRUE  ; $OBJ11BOX.Enabled = $TRUE
         $OBJ12BOX.Enabled = $TRUE
         $OBJ17BOX.Enabled = $FALSE ; $OBJ18BOX.Enabled = $FALSE }

} ) # CLOSES $OBJPCBOX.Add_CheckStateChanged( {


# IF user box ticked, untick PC box
$USERBOX.Add_CheckStateChanged( {
IF ( $USERBOX.Checked -eq $TRUE )
     { $OBJPCBOX.Checked = $FALSE } } )




# Launch the checkbox form.
$ADREPORT.Add_Shown( { $ADREPORT.Activate() } )



# When you've pressed ok...

# /// If you choose ADUsers then first create two empty variables; populate $ADVAIRABLES with the name of each checkbox 
#         ticked.
#         Perform an unfiltered search of Active Directory users and select just the attributes from the checkboxes. This 
#         is measured for visual feedback.
#         If the Manager attribute is checked, this comes back in DN form ( CN=Graham Jordan,OU=Users,DC=DOMAIN,DC=LOCAL ), 
#         so run a Get-Aduser against each DN to bring back the full name.
#         The manager conversion is wrapped up in Write-Progress to give you a visual impression of how far into the change,
#         since this takes a while.
#         If the OU attribute is ticked ( CanonicalName ), then run a replace on each line to remove the domain name, and 
#         the users full name leaving just Users/...
#         Finally exports a CSV to your user profile in "AD Report for Users - 28 02 2018 - 12.45.csv" format.


IF ( ( $ADREPORT.ShowDialog() ) -eq "OK" )
   { IF ( $USERBOX.Checked )
        { $ADVARIABLES = @()
          $USERS             = @()

          Write-Host "Performing a search of all users on Active Directory. This part can take a minute or so" -f Green ; "" 
          Get-Variable -Name OBJ* | ?{ $_.Value.Checkstate -eq 1    } | %{ $ADVARIABLES += $_.Value.Name }

          $SECONDS = ( Measure-Command {
          $USERS    += Get-ADUser -Filter * -Property $ADVARIABLES | select $ADVARIABLES | sort Name } ).seconds

          Write-Host $USERS.Count user accounts identified in $SECONDS seconds. -f Green ; ""


          IF ( $USERS.Manager -notlike $NULL ) 
             { $USERS | foreach-object -begin { $COUNT = 0 } -process { 
               $USER = $_

               IF ( $USER.Manager )
                  { $USER.Manager = ( ( Get-ADUser $USER.Manager ).Name ).ToString() }
                    $COUNT = $COUNT + 1
                    Write-Progress -Activity "Altering Manager DN's to Manager Name" -Status "Progress: $(
                    $USER.Name )" -PercentComplete ( $COUNT / $USERS.count * 100 ) } }


          IF ( $USER.CanonicalName )
             { Foreach ( $USER in $USERS )
                       { $USER.CanonicalName = $USER.CanonicalName.Substring( $USER.CanonicalName.IndexOf( "/" ) +1 )
                         $USER.CanonicalName = $USER.CanonicalName -replace "/$( $USER.Name )" } }


          $USERS | Export-Csv "$env:USERPROFILE\AD Report for Users - $( Get-Date -Format "dd MMM yyyy - hh.mm" ).csv" -NoClobber -NoTypeInformation -Force }


IF ( $OBJPCBOX.Checked )
   { $ADVARIABLES = @()
     Get-Variable -Name OBJ* | ?{ $_.Value.Checkstate -eq 1    } | %{ $ADVARIABLES += $_.Value.Name }

     Write-Host "Performing a search of all computers on Active Directory. This part can take a minute or so" -f Green ; ""

     $PCS  = @()
     $PCS += Get-ADComputer -Filter * -Properties $ADVARIABLES | select $ADVARIABLES | sort SamAccountName


     IF ( $PCS.CanonicalName )
        { Foreach ( $PC in $PCS )
                  { $PC.CanonicalName = $PC.CanonicalName.Substring( $PC.CanonicalName.IndexOf( "/" ) +1 ) 
                    $PC.CanonicalName = $PC.CanonicalName -replace "/$( $PC.SamAccountName )$" } }

     $PCS | Export-Csv "$env:USERPROFILE\AD Report for Computers - $( Get-Date -Format "dd MMM yyyy - hh.mm" ).csv" -NoClobber -NoTypeInformation -Force }


}

1 个答案:

答案 0 :(得分:1)

这很烦人。图形用户界面很恶心。所以你的问题是,如果你运行以下命令:

Get-ADComputer -Identity PC01 -Properties 'Created','PasswordLastSet',''

它会抛出一个错误,因为其中一个属性是null /空字符串。 $ADVARIABLES中有一个空/空字符串,导致您的错误。

根据你的脚本我做了一个修改:

Get-Variable -Name OBJ* | ?{ $_.Value.Checkstate -eq 1    } | %{
    Write-Host -Object "Adding property '$($_.Value.Name)' from $($_.Name)"
    $ADVARIABLES += $_.Value.Name
}

这为我提供了以下输出:

Adding property 'SamAccountName' from OBJ01BOX
Adding property 'Created' from OBJ13BOX
Adding property 'PasswordLastSet' from OBJ15BOX
Adding property '' from OBJPCBOX

您需要重新命名$OBJPCBOX或在填充$ADVARIABLES时创建更好的过滤器。