2016-08-11 99 views
0

我對Powershell相當陌生,並且爲一些同事編寫了這個表單。我只想得到一些建議和指導,說明它的寫法,還有什麼更好,或者它是否正確。謝謝。Powershell查找遠程登錄用戶

enter image description here

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

      function _Search { 
       if ($UernameSearch) {Clear-Variable -Name UernameSearch} 
       $OutputBox1.Clear() 
       $TextSearch = $SearchBox1.Text 
       $UernameSearch = gwmi win32_computersystem -comp $TextSearch | select USername 
        if ($UernameSearch) { 
         if ($UernameSearch.USername) {$OutputBox1.Text = $UernameSearch.USername} 
         else {$OutputBox1.Text = "No user currently logged on."}} 
        else {$OutputBox1.Text = "Is $TextSearch offline?"} 
       $OutputBox1.Enabled = $true 
      } 

      $objForm = New-Object System.Windows.Forms.Form 
      $objForm.Text = "Who's Logged In" 
      $Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe") 
      $objForm.Icon = $Icon 
      $objForm.Size = New-Object System.Drawing.Size(250,200) 
      $objForm.StartPosition = "CenterScreen" 
      $objForm.MaximizeBox = $false 
      $objForm.FormBorderStyle = 'Fixed3D' 
      $objForm.KeyPreview = $True 
      $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}}) 

      $objLabel = New-Object System.Windows.Forms.Label 
      $objLabel.Location = New-Object System.Drawing.Size(30,5) 
      $objLabel.AutoSize = $true 
      $objLabel.TextAlign = "TopCenter" 
      $objLabel.Text = "Use this tool to find who is currently 
      logged onto a remote machine." 
      $objForm.Controls.Add($objLabel) 

      $objLabe2 = New-Object System.Windows.Forms.Label 
      $objLabe2.Location = New-Object System.Drawing.Size(60,50) 
      $objLabe2.Size = New-Object System.Drawing.Size(150,15) 
      $objLabe2.Text = "Enter Computer Name" 
      $objForm.Controls.Add($objLabe2) 

      $SearchBox1 = New-Object System.Windows.Forms.TextBox 
      $SearchBox1.Location = New-Object System.Drawing.Size(15,70) 
      $SearchBox1.Height = 25 
      $SearchBox1.Width = 210 
      $SearchBox1.Multiline = $false 
      $SearchBox1.Add_KeyDown({if ($_.KeyCode -eq "Enter") {_Search}}) 
      $objForm.Controls.Add($SearchBox1) 

      $SearchButton = New-Object System.Windows.Forms.Button 
      $SearchButton.Location = New-Object System.Drawing.Size(15,95) 
      $SearchButton.Height = 25 
      $SearchButton.Width = 210 
      $SearchButton.Text = "GO!" 
      $SearchButton.Add_Click({_Search}) 
      $objForm.Controls.Add($SearchButton) 

      $OutputBox1 = New-Object System.Windows.Forms.TextBox 
      $OutputBox1.Location = New-Object System.Drawing.Size(15,125) 
      $OutputBox1.Multiline = $false 
      $OutputBox1.Height = 25 
      $OutputBox1.Width = 210 
      $OutputBox1.Multiline = $false 
      $OutputBox1.Enabled = $false 
      $objForm.Controls.Add($OutputBox1) 

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

回答

0

Windows.Forms的VS WPF

的Windows Presentation Foundation是更先進的替代。這很好,但有一個學習曲線。

很多顯示代碼都會消失在一個XAML文件中(一團XML),這意味着您可以使用後面的佈線。

變量命名

匈牙利表示法(以下簡稱 「OBJ」 前綴)是位沒有意義的。特別是考慮到你不一致地應用它。

帕斯卡案件或駱駝案件是最近流行的慣例。例如

  • 帕斯卡爾:MyVariable的
  • 駱駝:MYVARIABLE

我有我自己的,大多數人做的。選擇一些您感覺舒適的產品並持續使用。

功能_search

這並不容易發現,也不是可重複使用的。爲什麼是下劃線?

考慮製作一個通用函數,該函數將這些信息作爲對象的集合返回。您的GUI可以使用該功能的輸出並顯示它。

Assembly.LoadWithPartialName

Obsolete。充其量是不明確的。此外,還有整潔的PowerShell的替代品:

Add-Type -Assembly System.Windows.Forms 

別名

gwmi,選擇,截斷參數名:這些都沒有在生產代碼的地方。

Aliaes爲您節省了幾個按鍵,當您在控制檯上輕敲時,這很好。當你開發一些東西分享時,他們會增加默默無聞。

任何在將來查看或更新代碼的人都必須熟悉至少是描述性命令和別名。

一致的格式

風格永遠是個人的,但也有這樣的片段寫作分支語句的非常流行的方式一小:

if ($UernameSearch) { 
    if ($UernameSearch.USername) {$OutputBox1.Text = $UernameSearch.USername} 
    else {$OutputBox1.Text = "No user currently logged on."}} 
else {$OutputBox1.Text = "Is $TextSearch offline?"} 

考慮我喜歡的風格:

if ($UsernameSearch) { 
    if ($UsernameSearch.Username) { 
     $OutputBox1.Text = $UsernameSearch.Username 
    } else { 
     $OutputBox1.Text = "No user currently logged on." 
    } 
} else { 
    $OutputBox1.Text = "Is $TextSearch offline?" 
} 
+0

我認爲你的意思是XAML,而不是YAML在你對WPF和Windows Forms的解釋中。 – boeprox

+0

我做了,謝謝。 –

+0

非常有用,謝謝! – PSnewbie