1

I have the following script, and it completes all the commands EXCEPT running cmd.exe with the file/parameters given...it does not error, and it completes removing the file from the temp folder afterwards. Any idea how to get "file.exe" to run with the parameters "Location=(user-selected location code) /s" from a command line via powershell script?

# 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"
}

Note: The following code block works fine, but is designed for a .txt file of computer names, as opposed to a GUI where the user can enter a location and computer ID individually:

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
}
6
  • Why are you using cmd /c in a PS script? Commented Oct 16, 2017 at 14:17
  • /c isn't really necessary...doing silent installs on remote machines...and this is the way we were given by the software vendor (has to be installed from CLI with given switches...otherwise will not install)...janky, I know. Commented Oct 16, 2017 at 14:18
  • All arguments to the script will be strings by default. I'd suggest having a Param() block to capture the location rather than $args[0]. I'll post a suggested alternative Commented Oct 16, 2017 at 14:19
  • As I'm still quite ignorant to powershell, and used a template to make the "GUI" portion of this...is it possible to use the param block in a similar manner (goal is to use ps2exe to make this an executable GUI for users)? Also, thank you very much for the answer! Commented Oct 16, 2017 at 14:26
  • Do users execute this from the command line? You can get input from a Windows Form and store it into a variable for use if that's your future plan is to prompt for the location instead. Commented Oct 16, 2017 at 14:29

1 Answer 1

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

<# ... #>

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

Alternatively:

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

In this method, you can capture the process details, like exitcode, and have further action e.g. $Proc.ExitCode

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.