变量未被传递

时间:2014-03-22 02:13:42

标签: powershell

我有一个正在运行的脚本。它要求http链接,然后将其传递给Vlc视频播放器。现在,我试图让它看起来更漂亮,并添加一个表格框,再次输入http链接,然后我现在调用$ X的输入变量将传递给Vlc视频播放器。新脚本上的代码出现问题,Vlc播放​​器无法打开。谁能帮助我?感谢

工作脚本

$link = read-host "Enter your Http Link" 
$ip = read-host "Enter your Local Ip Address:Port "
$processArgs = "$link",":sout=#transcode{vcodec=VP80,vb=2000,acodec=vorb,ab=128,
channels=2,samplerate=44100}:http{mux=webm,dst=:8080/stream} :sout-all :sout-keep"
start-process "C:\Program Files\VideoLAN\VLC\vlc.exe" $processArgs    
& "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"    
"https://dabble.me/cast/?video_link=http://$ip/stream"

非工作脚本

[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()})
$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 = "Enter the Http Link:"
$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()

**$x** 

$ip = read-host "Enter your Local Ip Address:Port "
$processArgs = "$x",":sout=#transcode{vcodec=VP80,vb=2000,acodec=vorb,
ab=128,channels=2, samplerate=44100}:http{mux=webm,dst=:8080/stream} 
:sout-all :sout-keep"
start-process "C:\Program Files\VideoLAN\VLC\vlc.exe" $processArgs  
& "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"     
"https://dabble.me/cast/?video_link=http://$ip/stream"

1 个答案:

答案 0 :(得分:0)

<强>修正:

Try replacing $x with $Script:x everywhere in your script

<强>解释

您假设脚本块中的$ x可以从外部访问。不是。您可以克服这个问题的一种方法是使用$ script:VariableName方法。您以这种方式定义的变量将对该脚本中的所有代码块可见