简体   繁体   中英

Script runs with ISE but not powershell.exe

After months of "scripting" I finally got my script working as I need it to, except it only does what I want when running it from ISE. When I start it using powershell.exe it throws a fit something about unable to find [system.windows.forms. "dialogresult]".

I have attached the relevant portion of the script, TYIA

    $cred = Get-Credential
   $Job = Start-Job -ScriptBlock {
   Add-Type -AssemblyName System.Windows.Forms
   Add-Type -AssemblyName System.Drawing
   $form = New-Object System.Windows.Forms.Form
   $form.Text = 'Admin Tools’
   $form.Size = New-Object System.Drawing.Size(900,600)
   $form.StartPosition = 'CenterScreen'
   $form.AutoSize = $true
   $form.MaximizeBox = $false
   $form.FormBorderStyle = 'FixedSingle'


   $img = [System.Drawing.Image]::Fromfile("c:\users\$env:username\Pictures\logo.png")
   $pictureBox = New-Object Windows.Forms.PictureBox
   $pictureBox.Width = $img.Size.Width
   $pictureBox.Height = $img.Size.Height
   $pictureBox.Location = New-Object System.Drawing.Size(600,465)
   $pictureBox.Image = $img
   $form.controls.add($pictureBox) 

   $ADUCButton = New-Object System.Windows.Forms.Button
   $ADUCButton.Location = New-Object System.Drawing.Point(10,25)
   $ADUCButton.Size = New-Object System.Drawing.Size(300,100)
   $ADUCButton.Font = New-Object System.Drawing.Font(“Times New Roman”,14, [System.Drawing.Fontstyle]::Bold) 
   $ADUCButton.Text = ' Active Directory Users and Computers '
   $ADUCButton.Add_Click({Start-Process -filepath 'c:\windows\system32\cmd.exe' -WindowStyle maximized})
   $ADUCButton.FlatAppearance.BorderColor = [System.Drawing.Color]::DarkBlue
   $ADUCButton.BackColor = [System.Drawing.Color]::CornflowerBlue
   $form.Controls.Add($ADUCButton)

   $label = New-Object System.Windows.Forms.Label
   $label.location = New-Object System.Drawing.Point(100,500)
   $label.Size = New-Object System.Drawing.Size(280,70)
   $label.Font = New-Object System.Drawing.Font("Lucida Console",8, 
 [System.Drawing.FontStyle]::Italic)
   $label.Text = 'Created by a PowerShell Novice'
   $form.Controls.Add($label)

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



   $result = $form.ShowDialog()

   if ($result -eq [System.Windows.Forms.DialogResult]::OK)
   {
   $x = $listBox.SelectedItems
   $x
   }

   } -Credential $cred
   Recieve-Job $job

Any assistance you can provide is greatly appreciated.

When you are in the ISE it will autoload modules needed to do many things, the consoelhost does not.

If you have form code in your scripts, you must at needed resources, for it to properly run in the consolehost.

Put this at the top of your script. Here is a snippet I keep in for functions

# Initialize GUI resources
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName presentationframework
Add-type -AssemblyName microsoft.VisualBasic
[System.Windows.Forms.Application]::EnableVisualStyles()
Add-Type -AssemblyName System.Drawing

# Required for use with web SSL sites
[Net.ServicePointManager]::
SecurityProtocol = [Net.ServicePointManager]::
                   SecurityProtocol -bor 
                   [Net.SecurityProtocolType]::
                   Tls12

You don't need them all, depending on what you are doing or plan to. At the minimum, you need this...

Add-Type -AssemblyName System.Windows.Forms

BTW, this is probably a posting error, but this line is not syntactically correct.

if ($result -eq [System.Windows.Forms.DialogResult}::OK)

It should be this...

if ($result -eq [System.Windows.Forms.DialogResult]::OK)

Open/Close bracket type must match

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM