使用Powershell模拟右键单击Active Directory组的属性

时间:2014-07-31 15:25:28

标签: powershell active-directory command-prompt

为了澄清,我希望GUI出现,就像实际的窗口一样。这是一个侧面项目,我已经尝试了一段时间,现在的目标是更快地连接一组用户和文件夹。由于这是一个专注于节省时间的过程,我想让这种方法直接进入属性而无需主动搜索文件夹。

  • 询问是否需要更多信息
  • 可以提供代码(不确定为什么你需要它我不喜欢,但我可以安排)
  • 我真的不想使用任何外部来源或安装。主要目标是让这个powershell脚本能够被放在任何一个ol'电脑,让它工作。 (不是真的有任何旧电脑,但你明白我的观点)

我选择RUNDLL32 dsquery,OpenQueryWindow作为选项,但它没有给我所需要的东西。

TL:DR - 几乎就是标题所说的。我希望能够运行此命令并显示特定安全组的属性窗口,就好像我在Active Directory中右键单击属性一样。

    #######Paths#########
param (
$MYDRIVE_PATH = "\\------\KSD\",
$LOG_FILE_PATH = "C:\Users\Documents\Test_Docs\",
$UserListFile = "C:\Users\Documents\Test_Docs\brenda.txt"
)
##########################

Add-PSSnapin Quest.ActiveRoles.ADManagement
#import-module activedirectory

$LogFile = $LOG_FILE_PATH + "\CreateMyDriveFolderAndPermissions_" + (Get-Date -Format "MM-d-y.h.m.s.ms") + ".log"
$groupRights = [System.Security.AccessControl.FileSystemRights]"ListDirectory, ReadData, WriteData, CreateFiles, CreateDirectories, AppendData, ReadExtendedAttributes, WriteExtendedAttributes, Traverse, ExecuteFile, DeleteSubdirectoriesAndFiles, ReadAttributes, WriteAttributes, Write, ReadPermissions, Read, ReadAndExecute, Synchronize" 
$InheritanceFlag = ([System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None 

$ErrorCount = 0
$DestinationCheck = 1
$FolderNameCheck = 1

 ####################################### Option Box #######################################
          [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
          [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

                $objForm = New-Object System.Windows.Forms.Form 
                $objForm.Text = "Options"
                $objForm.Size = New-Object System.Drawing.Size(300,200) 
                $objForm.StartPosition = "CenterScreen"

                $objForm.KeyPreview = $True
                $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
                    {$x=$objTextBox.Text;$objForm.Close()}})
                $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
                    {$objForm.Close()}})

                $OKButton = New-Object System.Windows.Forms.Button
                $OKButton.Location = New-Object System.Drawing.Size(75,120)
                $OKButton.Size = New-Object System.Drawing.Size(75,23)
                $OKButton.Text = "OK"
                $OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
                $objForm.Controls.Add($OKButton)

                $CancelButton = New-Object System.Windows.Forms.Button
                $CancelButton.Location = New-Object System.Drawing.Size(150,120)
                $CancelButton.Size = New-Object System.Drawing.Size(75,23)
                $CancelButton.Text = "Cancel"
                $CancelButton.Add_Click({$objForm.Close();})
                $objForm.Controls.Add($CancelButton)

                $objLabel = New-Object System.Windows.Forms.Label
                $objLabel.Location = New-Object System.Drawing.Size(10,20) 
                $objLabel.Size = New-Object System.Drawing.Size(280,20) 
                $objLabel.Text = "Options:"
                $objForm.Controls.Add($objLabel) 

                $objBrenda = New-Object System.Windows.Forms.checkbox
                $objBrenda.Location = New-Object System.Drawing.Size(10,50)
                $objBrenda.Size = New-Object System.Drawing.Size(200,20)
                $objBrenda.Checked = $false
                $objBrenda.Text = "Brenda Fernandez"
                $objForm.Controls.Add($objBrenda)

                $objForm.Topmost = $True

                $objForm.Add_Shown({$objForm.Activate()})
                [void] $objForm.ShowDialog()

##############################################################################################

####################################### Destination Box #######################################


            [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
            [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

##### Main Box Creation #####
            $objForm = New-Object System.Windows.Forms.Form 
            $objForm.Text = "New Folder Destination"
            $objForm.Size = New-Object System.Drawing.Size(300,200) 
            $objForm.StartPosition = "CenterScreen"

            $objForm.KeyPreview = $True
            $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
                {$x=$objListBox.SelectedItem;$objForm.Close()}})
            $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
                {$objForm.Close()}})       
    ###################

##### "Ok" Button Creation #####
            $OKButton = New-Object System.Windows.Forms.Button
            $OKButton.Location = New-Object System.Drawing.Size(75,120)
            $OKButton.Size = New-Object System.Drawing.Size(75,23)
            $OKButton.Text = "OK"
            $OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close();$DestinationCheck = 0})
            $objForm.Controls.Add($OKButton)
    ###################

##### "Cancel" Button Creation #####
            $CancelButton = New-Object System.Windows.Forms.Button
            $CancelButton.Location = New-Object System.Drawing.Size(150,120)
            $CancelButton.Size = New-Object System.Drawing.Size(75,23)
            $CancelButton.Text = "Cancel"
            $CancelButton.Add_Click({$objForm.Close();[System.Environment]::Exit(0)})
            $objForm.Controls.Add($CancelButton)
    ###################

##### Message Right Above Selection Box #####
            $objLabel = New-Object System.Windows.Forms.Label
            $objLabel.Location = New-Object System.Drawing.Size(10,20) 
            $objLabel.Size = New-Object System.Drawing.Size(280,20) 
            $objLabel.Text = "Select a Folder:"
            $objForm.Controls.Add($objLabel) 
    ###################

##### List Selection #####
            $objListBox = New-Object System.Windows.Forms.ListBox 
            $objListBox.Location = New-Object System.Drawing.Size(10,40) 
            $objListBox.Size = New-Object System.Drawing.Size(260,20) 
            $objListBox.Height = 80


    ##### Options for Path #####
            [void] $objListBox.Items.Add("Department")
            [void] $objListBox.Items.Add("Engagement")
            [void] $objListBox.Items.Add("Project")
            [void] $objListBox.Items.Add("Opportunity")
    ###################

            $objForm.Controls.Add($objListBox) 

            $objForm.Topmost = $True

            $objForm.Add_Shown({$objForm.Activate()})
            [void] $objForm.ShowDialog()

          ###Path Creation###
           $MYDRIVE_PATH = $MYDRIVE_PATH + $x
          ###################

          $ParentFolder = $x


################################################################################################



####################################### Name Box #######################################
          [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
          [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

                $objForm = New-Object System.Windows.Forms.Form 
                $objForm.Text = "Data Entry Form"
                $objForm.Size = New-Object System.Drawing.Size(300,200) 
                $objForm.StartPosition = "CenterScreen"

                $objForm.KeyPreview = $True
                $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
                    {$x=$objTextBox.Text;$objForm.Close()}})
                $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
                    {$objForm.Close()}})

                $OKButton = New-Object System.Windows.Forms.Button
                $OKButton.Location = New-Object System.Drawing.Size(75,120)
                $OKButton.Size = New-Object System.Drawing.Size(75,23)
                $OKButton.Text = "OK"
                $OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close();$FolderNameCheck = 0})
                $objForm.Controls.Add($OKButton)

                $CancelButton = New-Object System.Windows.Forms.Button
                $CancelButton.Location = New-Object System.Drawing.Size(150,120)
                $CancelButton.Size = New-Object System.Drawing.Size(75,23)
                $CancelButton.Text = "Cancel"
                $CancelButton.Add_Click({$objForm.Close();[System.Environment]::Exit(0)})
                $objForm.Controls.Add($CancelButton)

                $objLabel = New-Object System.Windows.Forms.Label
                $objLabel.Location = New-Object System.Drawing.Size(10,20) 
                $objLabel.Size = New-Object System.Drawing.Size(280,20) 
                $objLabel.Text = "Enter File Name:"
                $objForm.Controls.Add($objLabel) 

                $objTextBox = New-Object System.Windows.Forms.TextBox 
                $objTextBox.Location = New-Object System.Drawing.Size(10,40) 
                $objTextBox.Size = New-Object System.Drawing.Size(260,20) 
                $objForm.Controls.Add($objTextBox) 

                $objForm.Topmost = $True

                $objForm.Add_Shown({$objForm.Activate()})
                [void] $objForm.ShowDialog()

                $FolderName = $x
                #if([string]::IsNullOrEmpty($FolderName)){$FolderNameCheck = 1}

                $SecurityName = "US-SG " + $FolderName

################################################################################################



if( $objBrenda.Checked -eq $true )  # Checks if it is a Brenda Fernandez folder and if so adds _QPC to the security group name
{
    $SecurityName = $SecurityName + "_QPC"
}


if($FolderNameCheck -eq 0 -and $DestinationCheck -eq 0 ) #Failsafe to make sure all information has been filled out
{

#####Create Security Group#####
              $Description = $ParentFolder + "\" + $FolderName

              New-QADGroup -Name $SecurityName -samAccountName $SecurityName -Description $Description -GroupScope DomainLocal -ParentContainer "------------"



    ##################################          

      $Ar = New-Object system.security.accesscontrol.filesystemaccessrule($SecurityName,$groupRights,$InheritanceFlag, $PropagationFlag,"Allow") #Adds all the security options to the variable $Ar
      $UserFolder = New-Item -Path ('{0}\{1}' -f $MYDRIVE_PATH,$FolderName) -ItemType Directory # Creates New Folder
      $Acl = get-acl -Path $UserFolder.FullName #Finds location of Folder's Security group


      while(1) { #Waits for Security Group to be created and appear in Active Directory

        if(  $Acl.SetAccessRule($Ar) -eq $NULL ){ break; }   ########### Once Completed it will set the $Ar to $Acl and break out of the loop
        Start-Sleep -s 1    
      }

      $Acl | Set-Acl $UserFolder.FullName  #####Set security group to user folder

      ##################### Notification Popup ##########################
        [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

        $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 

        $objNotifyIcon.Icon = "C:\Users\Downloads\success.ico"
        $objNotifyIcon.BalloonTipIcon = "Info" 
        $objNotifyIcon.BalloonTipText =  "The folder " + $FolderName + " has been successfully created."
        $objNotifyIcon.BalloonTipTitle = "Proccess Complete"

        $objNotifyIcon.Visible = $True 
        $objNotifyIcon.ShowBalloonTip(10000)
        Start-Sleep -s 1  
        $objNotifyIcon.Dispose()

     ##############################################################################################

    if( $objBrenda.Checked -eq $true ) 
    {

    $DPText = "US\" + $SecurityName + "_QPC;"
    Add-Content $UserListFile $DPText

    }
} 

if ($ErrorCount -eq 0)
{
    Write-Host "Done." -ForegroundColor Green
}
else{
    Write-Host "Done. ($ErrorCount)" -ForegroundColor Red
}

<#
              if(() -ne $Null)
              {
                    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

                    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 

                    $objNotifyIcon.Icon = "C:\Users\Downloads\success.ico"
                    $objNotifyIcon.BalloonTipIcon = "Error" 
                    $objNotifyIcon.BalloonTipText =  "The security group " + $FolderName + " already exists."
                    $objNotifyIcon.BalloonTipTitle = "Something went horribly wrong!"

                    $objNotifyIcon.Visible = $True 
                    $objNotifyIcon.ShowBalloonTip(10000)
                    Start-Sleep -s 1  
                    $objNotifyIcon.Dispose()

                    exit
              }

              #>

#[Windows.Clipboard]::SetText("Meow")

1 个答案:

答案 0 :(得分:0)

我觉得你太复杂了。我会尝试使用与RSAT(远程服务器管理工​​具)一起安装的ActiveDirectory模块。不要害怕离开GUI,一旦你习惯它就不那么可怕了。 :)

真的,我的意思是你可以用一个班轮来做这件事。