Я использую PowerShell Version 3. У меня есть странная проблема, которую я хотел бы исправить. Я написал сценарий PowerShell, который читает конфигурационный CSV-файл (с путями приложений и именами) и создает форму с кнопками приложения. Когда я пытаюсь представить аргумент Start-Process FilePath
с переменной, он просто не работает. Когда я повторяю переменную, все правильно. Может быть, кто-то здесь знает, что не так, и может мне помочь. я получаю сообщение об ошибке, что
Argument is NULL or empty in:
+ $btn.add_click({Start-Process -FilePath "$ButtonCommand"})
Я попытался решить эту проблему с помощью решения другой StackOverflow нити:
$ButtonCommand = $ButtonCommand.replace("\","\\").replace('"',"")
и пытался представить $ButtonCommand
с и без «"»
#Search Script Path, Locate Config File in same Path
$fullPathIncFileName = $MyInvocation.MyCommand.Definition
$currentScriptName = $MyInvocation.MyCommand.Name
$currentExecutingPath = $fullPathIncFileName.Replace($currentScriptName, "")
$ConfigFile = $currentExecutingPath + "admintools.conf"
#Read Config File
$StringArray = Get-Content $ConfigFile
#Count Lines in Config File
$ConfigLineCount = Get-Content $ConfigFile | Measure-Object –Line
# Create Button Function
function CreateButton ($ButtonCommand, $ButtonName, $ButtonLocX, $ButtonLocY, $ButtonSizeX, $ButtonSizeY)
{
$btn = New-Object System.Windows.Forms.Button
$btn.add_click({Start-Process -FilePath "$ButtonCommand"})
$btn.Text = $ButtonName
$btn.Location = New-Object System.Drawing.Size($ButtonLocX,$ButtonLocY)
$btn.Size = New-Object System.Drawing.Size($ButtonSizeX,$ButtonSizeY)
$btn.Cursor = [System.Windows.Forms.Cursors]::Hand
$btn.BackColor = [System.Drawing.Color]::LightGreen
$form.Controls.Add($btn)
}
# Define Form
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object Windows.Forms.Form
$form.Size = New-Object Drawing.Size @(260,250)
$Form.Text = "Tools"
$form.StartPosition = "CenterScreen"
#For each line in the configfile a button gets created.
foreach ($ArrayLine in $StringArray)
{
$Ar = [string]$ArrayLine
$Ar = $Ar.Split(";")
$UserArray += @($Ar[1])
CreateButton $Ar[0] $Ar[1] $Ar[2] $Ar[3] $Ar[4] $Ar[5]
}
#Show Form
$drc = $form.ShowDialog()
Мое хорошее предположение заключается в том, что '' $ ButtonCommand "' будет пустым внутри, даже если он выходит из области видимости внутри этого srcriptblock. – Matt