簡體   English   中英

如何在PS1中結束功能

[英]How to end function in ps1

這可能是一個非常愚蠢的問題,但是我仍在學習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();
                }
            }
        }
    }

“ @ |無空} #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. 您在一個單獨的文件中有一個功能
  2. 您將腳本放在另一個單獨的文件中
  3. 您想要在(2)中的腳本結束時停止(1)中的功能

---是的.....

繼續並將該函數包含在主腳本(2)中,並且當腳本(2)存在時,該函數也將包含在內。

要從主腳本中加載功能,請使用點源,請在此處閱讀有關內容: http : //mctexpert.blogspot.dk/2011/04/dot-sourcing-powershell-script.html-而不是在shell中進行操作,您只需在主腳本(2)中執行相同的操作即可。

快樂的編碼:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM