脚本使用 ISE 而不是 powershell.exe
Script runs with ISE but not powershell.exe
经过数月的“编写脚本”后,我终于让我的脚本按我需要的方式工作,只是当 运行 它来自 ISE 时,它只做我想做的事。当我使用 powershell.exe 启动它时,它会抛出一些关于无法找到 [system.windows.forms 的问题。 “对话结果]”。
我附上了脚本的相关部分,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
非常感谢您提供的任何帮助。
当您在 ISE 中时,它会自动加载执行许多操作所需的模块,consoelhost 不会。
如果您的脚本中有表单代码,您必须获取所需的资源,以使其在控制台主机中正常 运行。
将它放在脚本的顶部。这是我为功能保留的片段
# 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
你不需要全部,这取决于你正在做什么或计划做什么。至少,你需要这个...
Add-Type -AssemblyName System.Windows.Forms
顺便说一句,这可能是一个发布错误,但这行在语法上不正确。
if ($result -eq [System.Windows.Forms.DialogResult}::OK)
应该是这个...
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
Open/Close 括号类型必须匹配
经过数月的“编写脚本”后,我终于让我的脚本按我需要的方式工作,只是当 运行 它来自 ISE 时,它只做我想做的事。当我使用 powershell.exe 启动它时,它会抛出一些关于无法找到 [system.windows.forms 的问题。 “对话结果]”。
我附上了脚本的相关部分,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
非常感谢您提供的任何帮助。
当您在 ISE 中时,它会自动加载执行许多操作所需的模块,consoelhost 不会。
如果您的脚本中有表单代码,您必须获取所需的资源,以使其在控制台主机中正常 运行。
将它放在脚本的顶部。这是我为功能保留的片段
# 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
你不需要全部,这取决于你正在做什么或计划做什么。至少,你需要这个...
Add-Type -AssemblyName System.Windows.Forms
顺便说一句,这可能是一个发布错误,但这行在语法上不正确。
if ($result -eq [System.Windows.Forms.DialogResult}::OK)
应该是这个...
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
Open/Close 括号类型必须匹配