2017-10-16 38 views
1

我有以下脚本,并且它完成了所有命令,除了运行cmd.exe以及给定的文件/参数...它没有错误,它之后完成从临时文件夹中删除文件。任何想法如何通过使用参数“位置=(用户选择的位置代码)/ s”通过powershell脚本从命令行运行“file.exe”?Powershell脚本不会从cmd.exe运行具有给定参数的文件

# Get the Computer ID. 
Add-Type -AssemblyName System.Windows.Forms 
Add-Type -AssemblyName System.Drawing 

$form = New-Object System.Windows.Forms.Form 
$form.Text = "'Enter Computer ID" 
$form.Size = New-Object System.Drawing.Size(300,200) 
$form.StartPosition = "CenterScreen" 

$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Point(75,120) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK" 
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK 
$form.AcceptButton = $OKButton 
$form.Controls.Add($OKButton) 

$CancelButton = New-Object System.Windows.Forms.Button 
$CancelButton.Location = New-Object System.Drawing.Point(150,120) 
$CancelButton.Size = New-Object System.Drawing.Size(75,23) 
$CancelButton.Text = "Cancel" 
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel 
$form.CancelButton = $CancelButton 
$form.Controls.Add($CancelButton) 

$label = New-Object System.Windows.Forms.Label 
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Please enter the Computer ID." 
$form.Controls.Add($label) 

$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$textBox.MaxLength = 8 
$form.Controls.Add($textBox) 

$form.Topmost = $True 

$form.Add_Shown({$textBox.Select()}) 
$result = $form.ShowDialog() 

if ($result -eq [System.Windows.Forms.DialogResult]::OK) 
{ 
    $Machine = $textBox.Text 
    $Machine 
} 

# Get the Location Code. 
Add-Type -AssemblyName System.Windows.Forms 
Add-Type -AssemblyName System.Drawing 

$form1 = New-Object System.Windows.Forms.Form 
$form1.Text = "Select a Location" 
$form1.Size = New-Object System.Drawing.Size(190,250) 
$form1.StartPosition = "CenterScreen" 

$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Point(10,180) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK" 
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK 
$form1.AcceptButton = $OKButton 
$form1.Controls.Add($OKButton) 

$CancelButton = New-Object System.Windows.Forms.Button 
$CancelButton.Location = New-Object System.Drawing.Point(85,180) 
$CancelButton.Size = New-Object System.Drawing.Size(75,23) 
$CancelButton.Text = "Cancel" 
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel 
$form.CancelButton = $CancelButton 
$form.Controls.Add($CancelButton) 

$label = New-Object System.Windows.Forms.Label 
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Please select a Location Code:" 
$form.Controls.Add($label) 

$listBox = New-Object System.Windows.Forms.ListBox 
$listBox.Location = New-Object System.Drawing.Point(10,40) 
$listBox.Size = New-Object System.Drawing.Size(150,20) 
$listBox.Height = 140 

[void] $listBox.Items.Add("01") 
[void] $listBox.Items.Add("02") 
[void] $listBox.Items.Add("03") 
[void] $listBox.Items.Add("04") 
[void] $listBox.Items.Add("06") 
[void] $listBox.Items.Add("07") 

$form1.Controls.Add($listBox) 

$form1.Topmost = $True 

$result1 = $form1.ShowDialog() 

if ($result1 -eq [System.Windows.Forms.DialogResult]::OK) 
{ 
    $Server = $listBox.SelectedItem 
    $Server 
} 

# Ensure the Computer ID is available on the Network before continuing. 
IF (Test-Connection -ComputerName $Machine -Count 1){ 
    Try { 
     $destination = "\\" + $Machine + "\C$\temp\" 
     $source = "\\server\location\folder\file name 1.1.EXE" 
     New-Item -Path $destination -ItemType Directory -Force 
     Copy-Item -Path $source -Destination $destination -Force | Out-Null 
     Invoke-Command -ComputerName $Machine -ArgumentList $Server -ScriptBlock { 
      $file = "C:\temp\file name 1.1.EXE" 
      $renfile = "C:\temp\file.exe" 
      $execfile = "C:\temp\file.exe Location="+($args[0]).ToString()+" /s" 
      Rename-Item $file $renfile 
      & cmd.exe /c $execfile | Out-Null 
      Remove-Item $renfile -Force 
      } 
     $status = "Success" 
     } 
    Catch { 
     $status = "Failed" 
     } 
    "$Machine, $status" | out-file -filepath c:\temp\result2.csv -Append -Encoding ascii 

    } 
ELSE{ 
    #Sets status to "Not on Network" if the Test-Connection failed. Indicates the PC is not on the Network currently" 
    $status = "Not on Network" 
} 

注:下面的代码块工作正常,但专为计算机名称的.txt文件,而不是一个GUI用户可以在其中输入单独一个位置,计算机ID:

Get-Content C:/temp/faillist.txt | 
ForEach-Object { 
    Try { 
     $source = "\\server\location\folder\file name 1.1.EXE" 
     $destination = "\\" + $_ + "\C$\temp\" 
     New-Item -Path $destination -ItemType Directory -Force 
     Copy-Item -path $source -Destination $destination -force | Out-Null 
     Invoke-Command -ComputerName $_ -ScriptBlock { 
      & cmd.exe /c "C:\temp\file name 1.1.EXE" Location=02 /s | Out-Null 
      } 
     $file = $destination + "file name 1.1.EXE" 
     Remove-Item $file -Force 
     $status = "Success" 
     } 
    Catch { 
     $status = "Failed" 
     } 
    "$_, $status" | out-file -filepath c:\temp\result2.csv -Append -Encoding ascii 
} 
+0

你为什么在PS脚本中使用'cmd/c'? – TheIncorrigible1

+0

/c没有必要......在远程计算机上进行无提示安装......这是我们由软件供应商提供的方式(必须使用给定开关从CLI进行安装......否则将不会安装) ...我很高兴,我知道。 –

+0

默认情况下,脚本的所有参数都是字符串。我建议有一个'Param()'块来捕获位置而不是'$ args [0]'。我会发布一个建议的替代方案 – TheIncorrigible1

回答

1
Param(
    [Parameter(Position=0,Mandatory)] 
    [String]$Location 
) 

<# ... #> 

& 'C:\Temp\File.exe' Location="$Location" /s >$Null 

或者:

$Proc = Start-Process -FilePath 'C:\Temp\File.exe' -ArgumentList @("Location=$Location",'/s') -NoNewWindow -Wait -WindowStyle 'Hidden' -PassThru 

在这种方法中,你可以捕捉的过程细节,如退出码,和有进一步的行动$Proc.ExitCode

+0

明白了,谢谢! –

相关问题