使用 $Variable 为 Start-Process 提交 -Filepath 参数
Using a $Variable to Submit -Filepath Argument for Start-Process
我正在使用 PowerShell 版本 3。
我有一个奇怪的问题想解决。我编写了一个 PowerShell 脚本,它读取一个配置 CSV 文件(带有应用程序路径和名称)并创建一个带有应用程序按钮的表单。当我尝试提交带有变量的 Start-Process FilePath
参数时,它就不起作用了。当我回显变量时,一切都是正确的。也许这里有人知道出了什么问题,可以帮助我。
我得到一个错误
Argument is NULL or empty in:
+ $btn.add_click({Start-Process -FilePath "$ButtonCommand"})
我尝试使用另一个 Whosebug 线程的解决方案来解决问题:
$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
没有被初始化。一种解决方案是在其自己的变量中声明脚本块。
$clickEvent = {Start-Process -FilePath "$ButtonCommand"}
$btn.add_click($clickEvent)
关于这个有几个线程,但有两个值得注意 here at SO and on TechNet
同样,您可以在全局范围内使用变量来解决此问题。
$btn.add_click({Start-Process -FilePath "$global:ButtonCommand"})
我正在使用 PowerShell 版本 3。
我有一个奇怪的问题想解决。我编写了一个 PowerShell 脚本,它读取一个配置 CSV 文件(带有应用程序路径和名称)并创建一个带有应用程序按钮的表单。当我尝试提交带有变量的 Start-Process FilePath
参数时,它就不起作用了。当我回显变量时,一切都是正确的。也许这里有人知道出了什么问题,可以帮助我。
我得到一个错误
Argument is NULL or empty in:
+ $btn.add_click({Start-Process -FilePath "$ButtonCommand"})
我尝试使用另一个 Whosebug 线程的解决方案来解决问题:
$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
没有被初始化。一种解决方案是在其自己的变量中声明脚本块。
$clickEvent = {Start-Process -FilePath "$ButtonCommand"}
$btn.add_click($clickEvent)
关于这个有几个线程,但有两个值得注意 here at SO and on TechNet
同样,您可以在全局范围内使用变量来解决此问题。
$btn.add_click({Start-Process -FilePath "$global:ButtonCommand"})