如何在ps1中结束功能

时间:2014-10-09 19:27:37

标签: powershell

这可能是一个非常愚蠢的问题,但我还在学习Powershell ...... 是否可以从另一个PowerShell脚本(.ps1)关闭或停止powershell函数 我创建了一个函数,它只是一个文本覆盖字幕样式进度条 - 不是基于任何百分比或任何东西 - 它只是作为事件正在发生的消息。 功能正常。 然后我有另一个ps1文件,它删除并将文件复制到服务器列表。这个脚本工作正常。 我想做的就是调用ps1中的函数(我已成功完成),然后调用结束ps1中的函数,但是我似乎找不到任何关于如何停止的内容功能,除非它基于百分比。

Function ProgressPopup {
param(
[string]$StatusText = 'HH CMDs Update Progress', 
[string]$LabelMessage = 'Please Wait',
[string]$TextOverlay = 'Synchronizing Files...',
[String]$ICOpath = 'C:\Scripts\ICONS\Synch.ico'
)

If ( Get-Variable -Name Form1 -Scope Global -ErrorAction SilentlyContinue ) {
    $Form1.close()  #Kill previous Progress Message to prevent lost handles
}
If ($LabelMessage -eq ""){
    Return
}   

$LabelMessage="`r`n   "+$LabelMessage+"   `r`n " #add space around the message

#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
Add-Type -AssemblyName "mscorlib"
Add-Type -AssemblyName "System"
Add-Type -AssemblyName "System.Windows.Forms"
Add-Type -AssemblyName "System.Data"
Add-Type -AssemblyName "System.Drawing"
Add-Type -AssemblyName "System.Xml"
Add-Type -AssemblyName "System.DirectoryServices"
Add-Type -AssemblyName "System.Core"
Add-Type -AssemblyName "System.ServiceProcess"
#endregion Import Assemblies

#----------------------------------------------
#region Define SAPIEN Types
#----------------------------------------------
try{
    $local:type = [ProgressBarOverlay]
}
catch
{
    Add-Type -ReferencedAssemblies ('System.Windows.Forms', 'System.Drawing') -TypeDefinition  @" 
    using System;
    using System.Windows.Forms;
    using System.Drawing;
    namespace SAPIENTypes
    {
        public class ProgressBarOverlay : System.Windows.Forms.ProgressBar
        {
            protected override void WndProc(ref Message m)
            { 
                base.WndProc(ref m);
                if (m.Msg == 0x000F)// WM_PAINT
                {
                    if (Style != System.Windows.Forms.ProgressBarStyle.Marquee || !string.IsNullOrEmpty(this.Text))
                    {
                        using (Graphics g = this.CreateGraphics())
                        {
                            using (StringFormat stringFormat = new StringFormat(StringFormatFlags.NoWrap))
                            {
                                stringFormat.Alignment = StringAlignment.Center;
                                stringFormat.LineAlignment = StringAlignment.Center;
                                if (!string.IsNullOrEmpty(this.Text))
                                    g.DrawString(this.Text, this.Font, Brushes.Black, this.ClientRectangle, stringFormat);
                                else
                                {
                                    int percent = (int)(((double)Value / (double)Maximum) * 100);
                                    g.DrawString(percent.ToString() + "%", this.Font, Brushes.Black, this.ClientRectangle, stringFormat);
                                }
                            }
                        }
                    }
                }
            }

            public string TextOverlay
            {
                get
                {
                    return base.Text;
                }
                set
                {
                    base.Text = value;
                    Invalidate();
                }
            }
        }
    }

“@ | Out-Null     }     #endregion定义SAPIEN类型

#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$Label1 = New-Object 'System.Windows.Forms.Label'
$progressbaroverlay1 = New-Object 'SAPIENTypes.ProgressBarOverlay'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects

#----------------------------------------------
# User Generated Script
#----------------------------------------------







$formEnterStatusText_LoadText_Load={
    #TODO: Initialize Form Controls here

}

$Label1_Click={
    #TODO: Place custom script here

}

$progressbaroverlay1_Click={
    #TODO: Place custom script here

}

# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------

$Form_StateCorrection_Load=
{
    #Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}

$Form_Cleanup_FormClosed=
{
    #Remove all event handlers from the controls
    try
    {
        $Label1.remove_Click($Label1_Click)
        $progressbaroverlay1.remove_Click($progressbaroverlay1_Click)
        $form1.remove_Load($Form_StateCorrection_Load)
        $form1.remove_FormClosed($Form_Cleanup_FormClosed)
    }
    catch [Exception]
    { }
}
#endregion Generated Events

#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($Label1)
$form1.Controls.Add($progressbaroverlay1)
$form1.ClientSize = '287, 99'

#add Icon to dialog
$Form1.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('C:\Scripts\ICONS\Synch.ico')


$form1.Name = "form1"
$form1.Text = "HH CMDs Update Progress"
#
# Label1
#
$Label1.Location = '29, 9'
$Label1.Name = "Label1"
$Label1.Size = '227, 33'
$Label1.TabIndex = 1
$Label1.Text = "Please Wait"
$Label1.TextAlign = 'TopCenter'
$Label1.add_Click($Label1_Click)
#
# progressbaroverlay1
#
$progressbaroverlay1.BackColor = 'ButtonHighlight'
$progressbaroverlay1.Location = '29, 54'
$progressbaroverlay1.MarqueeAnimationSpeed = 25
$progressbaroverlay1.Name = "progressbaroverlay1"
$progressbaroverlay1.Size = '227, 23'
$progressbaroverlay1.Style = 'Marquee'
$progressbaroverlay1.TabIndex = 0
$progressbaroverlay1.TextOverlay = "Synchronizing Files....."
$progressbaroverlay1.add_Click($progressbaroverlay1_Click)
$form1.ResumeLayout()
#endregion Generated Form Code

#----------------------------------------------

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()
} #End Function

1 个答案:

答案 0 :(得分:0)

如果我没有误解某些东西,这就是我得到的:

  1. 您在一个单独的文件中有一个功能
  2. 您将脚本放在另一个单独的文件中
  3. 您希望在(2)中的脚本结束时停止(1)中的功能
  4. ---就是这样......

    继续并在主脚本(2)中包含该函数,当脚本(2)存在时,该函数也将如此。

    要从主脚本加载函数使用点源,请在此处阅读:http://mctexpert.blogspot.dk/2011/04/dot-sourcing-powershell-script.html - 而不是在shell中执行此操作,您只需在主脚本(2)中执行相同的操作。 / p>

    快乐编码: - )