使用Powershell从输入框存储多个输入项

时间:2015-01-29 16:51:21

标签: powershell

我编写了一个powershell脚本,它接受.csv文件并在网络驱动器上创建数百个子目录。在尝试将GUI面板放在上面时,我遇到了问题。该对话框要求.csv文件以及创建子目录的文件夹的路径。我假设这一行:

 {$Senior_Options_CSV = $objTextBox.Text;$Senior_Directory = $objTextBox1.Text;$objForm.Close()}})

正在为对话框中的项创建一个数组,但我不确定如何在代码中引用它们。现在脚本运行但显然没有结果。脚本是:

<#Script to Create Directories from a .csv file. The script asks for where
the .csv file is located and then proceeds to check if it is empty,giving a warning if it is not, 
then creates a folder from user input with Student subdirectories.
#>

#From https://technet.microsoft.com/en-us/library/ff730941.aspx

#mainSection

  #Declare variables
[string]$Senior_Options_CSV 

   # Load needed libraries(Assemblies)
[void]  [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

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

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

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$Senior_Options_CSV = $objTextBox.Text;$Senior_Directory = $objTextBox1.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(70,130)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$Senior_Options_CSV = $objTextBox.Text;$Senior_Directory = $objTextBox1.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,130)
$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.Font = "Microsoft Sans Serif, 9pt, style=Bold"
$objLabel.Text = "Please enter the Location of the .csv file:"
$objForm.Controls.Add($objLabel) 

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


$objLabel_1 = New-Object System.Windows.Forms.Label
$objLabel_1.Location = New-Object System.Drawing.Size(10,65) 
$objLabel_1.Size = New-Object System.Drawing.Size(280,30) 
$objLabel_1.Font = "Microsoft Sans Serif, 9pt, style=Bold"
$objLabel_1.Text = "Please enter the Directory to create the Student Subdirectories(eg. S:\Students):"
$objForm.Controls.Add($objLabel_1) 


$objTextBox1 = New-Object System.Windows.Forms.TextBox 
$objTextBox1.Name = 'Text'
$objTextBox1.Location = New-Object System.Drawing.Size(10,100) 
$objTextBox1.Size = New-Object System.Drawing.Size(260,20) 
$objForm.Controls.Add($objTextBox1) 

$objForm.Topmost = $True

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




#$Senior_Options_CSV = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the .csv file that contains `nSenior Options Students. The .csv file should include a 'First' and 'Last' column", "Senior", "C:\")




If ($Senior_Options_CSV -eq "")  {
       Break
     }

If (!(Test-Path -Path "$Senior_Options_CSV" -PathType Leaf))  {

      [Windows.Forms.MessageBox]::Show("Attention. File Does Not Exist.`nCheck Your File Path and Name.","Warning", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Error)
      Break

 }

#Create the array from the csv file selecting the columns you want in your array.
$Sen_Opt_Array = Import-Csv $Senior_Options_CSV | Select First, Last



#Check If Directory Empty.
  If (Test-Path -Path $Senior_Directory -PathType Container)  {
  $directoryInfo = Get-ChildItem $Senior_Directory | Measure-Object
  #$directoryInfo.count returns the count of all of the files in the directory
  #Note.The 1 & 48 replace having to write out the [System.Windows.Forms.MessageBoxButtons]::OKCancel,[System.Windows.Forms.MessageBoxIcon]::Warning)
  #Portions of the [Windows.Forms.MessageBox] command
  If (!($directoryInfo.count -eq 0)) {
          $Dir_Check = [Windows.Forms.MessageBox]::Show("Warning.The Directory is Not Empty! Do You Want To Continue?","Attention",1,48)
          $Dir_Check

        If (!($Dir_Check -eq "OK")) {
           Break
           }
}
}

for($i = 0; $i -le $Sen_Opt_Array.count -1; $i++) {
  $S_Name = $Sen_Opt_Array[$i]

  <#I need to force the evaluation of each value so I can display the data. To do this, I use a subexpression. 
  A subexpression consists of a dollar sign and a pair of parentheses.#>

  New-Item -Path $Senior_Directory\$($S_Name.Last)$($S_Name.First.Substring(0,1)) -ItemType Directory -Force

  }

[Windows.Forms.MessageBox]::Show("$($Sen_Opt_Array.count) Files Created in $Senior_Directory Directory.","Attention", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
      Break

奇怪的是我有这个脚本可以正常使用对话框和四个输入:

<#© 2014 
This program builds a form for users to input four parameters (screensavertimeout,monitor timeout,
hibernate off,sleep timeout) and changes these values system wide on the computer#>

#Load appropriate libraries
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 


<#Start by creating Form with properties you desire. See here for 
more information url:https://blogs.technet.com/b/stephap/archive/2012/04/23/building-forms-with-powershell-part-1-the-form.aspx?Redirected=true#>

$Form = New-Object System.Windows.Forms.Form 
$Form.Text = "© 2014"
$Form.Size = New-Object System.Drawing.Size(650,375) 
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
$Form.Add_Shown({$Form.Activate()})
$Form.Controls.Add($Label_Header)

#Create the OK and Cancel Buttons w/appropriate properties
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,280)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text='OK'
$OKButton.DialogResult='Ok'
$Form.Controls.Add($OKButton)
$Form.AcceptButton=$OKButton

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(155,280)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text ='Cancel'
$Form.Controls.Add($CancelButton)
$Form.CancelButton=$CancelButton

#Create the label for the Form
$Label_Header = New-Object System.Windows.Forms.Label
$Label_Header.Font = "Microsoft Sans Serif, 13pt, style=Bold"
$Label_Header.Location = New-Object System.Drawing.Size(5,12)
$Label_Header.Size = New-Object System.Drawing.Size(650,22)
$Label_Header.Name = "Power_Header"
$Label_Header.TabIndex = 4
$Label_Header.Text = "Change Your ScreenSaver and Power Saver Settings"
$Form.Controls.Add($Label_Header)

<# Now we create the four boxes ( three text, one checkbox) for the Form w/labels for each
   Font styles are: Regular, Bold, Italic, Underline, Strikeout 
   ex. $Label1.Font = New-Object System.Drawing.Font("Times New Roman",12.5,[System.Drawing.FontStyle]::Italic)#>

$Label1 = New-Object System.Windows.Forms.Label
$Label1.Font = New-Object System.Drawing.Font("Times New Roman",12.5)
$Form.Font = $Font
$Label1.Location = New-Object System.Drawing.Size(20,55) 
$Label1.Size = New-Object System.Drawing.Size(380,20) 
$Label1.Text = "Change Power Off Display Value (In Minutes) :"
$Form.Controls.Add($Label1) 

$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Name='Text1'
$TextBox1.Location = New-Object System.Drawing.Size(20,80) 
$TextBox1.Size = New-Object System.Drawing.Size(260,50) 
$Form.Controls.Add($TextBox1) 

$Label2 = New-Object System.Windows.Forms.Label
$Label2.Font = New-Object System.Drawing.Font("Times New Roman",12.5)
$Label2.Location = New-Object System.Drawing.Size(20,110) 
$Label2.Size = New-Object System.Drawing.Size(450,20) 
$Label2.Text = "Please enter Screensaver Timeout (Logout/In To Take Effect):"
$Form.Controls.Add($Label2) 

$TextBox2 = New-Object System.Windows.Forms.TextBox 
$TextBox2.Name='Text2'
$TextBox2.Location = New-Object System.Drawing.Size(20,130) 
$TextBox2.Size = New-Object System.Drawing.Size(260,50) 
$Form.Controls.Add($TextBox2) 

$Label3 = New-Object System.Windows.Forms.Label
$Label3.Font = New-Object System.Drawing.Font("Times New Roman",12.5)
$Label3.Location = New-Object System.Drawing.Size(20,160) 
$Label3.Size = New-Object System.Drawing.Size(680,20) 
$Label3.Text = "Change sleep time in minutes (Should be longer then Screen Saver timeout):"
$Form.Controls.Add($Label3) 

$TextBox3 = New-Object System.Windows.Forms.TextBox 
$TextBox3.Name='Text3'
$TextBox3.Location = New-Object System.Drawing.Size(20,180) 
$TextBox3.Size = New-Object System.Drawing.Size(260,50) 
$Form.Controls.Add($TextBox3) 

$Label4 = New-Object System.Windows.Forms.Label
$Label4.Font = New-Object System.Drawing.Font("Times New Roman",12.5)
$Label4.Location = New-Object System.Drawing.Size(16,215) 
$Label4.Size = New-Object System.Drawing.Size(360,20) 
$Label4.Text = "Turnoff Hibernate:"
$Form.Controls.Add($Label4) 

$CheckBox4 = New-Object System.Windows.Forms.CheckBox
$CheckBox4.Name='$CheckBox4'
$CheckBox4.Location = New-Object System.Drawing.Size(20,220) 
$CheckBox4.Size = New-Object System.Drawing.Size(260,50)
$Form.Controls.Add($CheckBox4) 

#End of Form Textbox creation

<#  Check to see if OK button was clicked. If it was then execute the commands to change power settings 
by first checking for blanks or whitespaces#>

if($Form.ShowDialog() -eq 'Ok' ){

    If ([string]::IsNullOrWhiteSpace($TextBox1.Text)) {}
    Else {powercfg -Change -monitor-timeout-ac $TextBox1.Text}

    If ([string]::IsNullOrWhiteSpace($TextBox2.Text)) {}
    Else {$TextBox2.Text = [int]$TextBox2.Text * 60
          reg add "HKLM\Software\Policies\Microsoft\Windows\Control Panel\Desktop" /v ScreenSaveTimeOut /t REG_SZ /d $TextBox2.Text /f | Out-Null
          }

    If ([string]::IsNullOrWhiteSpace($TextBox3.Text)) {}
    Else {powercfg -Change -standby-timeout-ac $TextBox3.Text}

    If ($CheckBox4.Checked -eq $true) {powercfg -Hibernate Off}


}else{
       Exit
     }

我尝试在我的新脚本中替换但是OK按钮没有渲染??

1 个答案:

答案 0 :(得分:0)

转发:我对表单没有太多经验,这更像是一种解决方法,而不是答案。

无论出于何种原因,变量状态都不在此代码块之外。此代码来自的Windows PowerShell Tip of the Week具有相同的问题,其中示例中的$x为空/空。

{$Senior_Options_CSV = $objTextBox.Text;$Senior_Directory = $objTextBox1.Text;$objForm.Close()}})

如果您使用Write-Host检查同一块内的返回,则会得到预期结果。但是在该代码块之外,该变量为null。如果您更新支票,可以看到这个

If (!$Senior_Options_CSV)  {
       Write-Warning "Senior_Options_CSV is null or empty"
       Break     
}

我尝试解决此问题的方法只是调用文本框的值。由于对象仍然存在,因此仍可访问值。

[void] $objForm.ShowDialog()

$Senior_Options_CSV = $objTextBox.Text
$Senior_Directory = $objTextBox1.Text

代码测试并在此之后正常工作。

在此行[void]前面添加[void](New-Item -Path $Senior_Directory...)会抑制该输出,因为您在末尾有一条消息告诉您该信息