通过右键单击文件来运行GUI Powershell脚本

时间:2018-09-24 21:23:42

标签: powershell alternate-data-stream

我已经使用GUI .net框架构建了一个powershell脚本,该框架为用户提供了图形界面以向NTFS文件系统上的文件添加备用数据流(ADS)。

以下是我为powershell脚本编写的代码:

<# 
This script is a GUI featured way to add extended attributes to files 
#>

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#region begin GUI{ 

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '600,600'
$Form.text                       = "Add Extended Attributes"
$Form.TopMost                    = $false

# Add Extended Attributes Label
$mainLabel                       = New-Object system.Windows.Forms.Label
$mainLabel.text                  = "Add Extended Attributes"
$mainLabel.AutoSize              = $true
$mainLabel.width                 = 25
$mainLabel.height                = 10
$mainLabel.location              = New-Object System.Drawing.Point(180,10)
$mainLabel.Font                  = 'Microsoft Sans Serif,18'

# text box for entering file path 
$filePath                        = New-Object system.Windows.Forms.TextBox
$filePath.multiline              = $false
$filePath.width                  = 300
$filePath.height                 = 20
$filePath.location               = New-Object System.Drawing.Point(200,80)
$filePath.Font                   = 'Microsoft Sans Serif,10'

# label for the file path text box "File Path: "
$FilePathLabel                   = New-Object system.Windows.Forms.Label
$FilePathLabel.text              = "File Path: "
$FilePathLabel.AutoSize          = $true
$FilePathLabel.width             = 25
$FilePathLabel.height            = 10
$FilePathLabel.location          = New-Object System.Drawing.Point(80,85)
$FilePathLabel.Font              = 'Microsoft Sans Serif,10'

# Attributes Label
$idLabel                         = New-Object system.Windows.Forms.Label
$idLabel.text                    = "Attributes"
$idLabel.AutoSize                = $true
$idLabel.width                   = 25
$idLabel.height                  = 10
$idLabel.location                = New-Object System.Drawing.Point(80,150)
$idLabel.Font                    = 'Microsoft Sans Serif,12'

# Values Label
$valueLabel                      = New-Object system.Windows.Forms.Label
$valueLabel.text                 = "Value"
$valueLabel.AutoSize             = $true
$valueLabel.width                = 25
$valueLabel.height               = 10
$valueLabel.location             = New-Object System.Drawing.Point(300,150)
$valueLabel.Font                 = 'Microsoft Sans Serif,12'

# Checkbox for ID attribute
$fileId                          = New-Object System.Windows.Forms.CheckBox
$fileId.text                     = "Id"
$fileId.AutoSize                 = $true
$fileId.width                    = 104
$fileId.height                   = 20
$fileId.location                 = New-Object System.Drawing.Point(100,200)
$fileId.Font                     = 'Microsoft Sans Serif,10'

# Label for the ID checkbox
$idValue                         = New-Object system.Windows.Forms.TextBox
$idValue.multiline               = $false
$idValue.width                   = 300
$idValue.height                  = 20
$idValue.location                = New-Object System.Drawing.Point(202,200)
$idValue.Font                    = 'Microsoft Sans Serif,10'

# Checkbox for Description attribute
$description                      = New-Object System.Windows.Forms.CheckBox
$description.text                 = "Description"
$description.AutoSize             = $true
$description.width                = 104
$description.height               = 20
$description.location             = New-Object System.Drawing.Point(100,250)
$description.Font                 = 'Microsoft Sans Serif,10'

# Label for the Description checkbox
$descriptionValue                 = New-Object system.Windows.Forms.TextBox
$descriptionValue.multiline       = $false
$descriptionValue.width           = 300
$descriptionValue.height          = 20
$descriptionValue.location        = New-Object System.Drawing.Point(202,250)
$descriptionValue.Font            = 'Microsoft Sans Serif,10'

# Checkbox for Type attribute
$type                             = New-Object System.Windows.Forms.CheckBox
$type.text                        = "Type"
$type.AutoSize                    = $true
$type.width                       = 104
$type.height                      = 20
$type.location                    = New-Object System.Drawing.Point(100,300)
$type.Font                        = 'Microsoft Sans Serif,10'

# Label for the type checkbox
$typeValue                        = New-Object system.Windows.Forms.TextBox
$typeValue.multiline              = $false
$typeValue.width                  = 300
$typeValue.height                 = 20
$typeValue.location               = New-Object System.Drawing.Point(202,300)
$typeValue.Font                   = 'Microsoft Sans Serif,10'

# Checkbox for silo attribute
$silo                             = New-Object System.Windows.Forms.CheckBox
$silo.text                        = "Silo"
$silo.AutoSize                    = $true
$silo.width                       = 104
$silo.height                      = 20
$silo.location                    = New-Object System.Drawing.Point(100,350)
$silo.Font                        = 'Microsoft Sans Serif,10'

# Label for the silo checkbox
$siloValue                        = New-Object system.Windows.Forms.TextBox
$siloValue.multiline              = $false
$siloValue.width                  = 300
$siloValue.height                 = 20
$siloValue.location               = New-Object System.Drawing.Point(202,350)
$siloValue.Font                   = 'Microsoft Sans Serif,10'

# submitt button 
$button                           = New-Object System.Windows.Forms.Button
$button.text                      = "Submit"
$button.AutoSize                  = $true
$button.location                  = New-Object System.Drawing.Point(250,500)
$button.Font                      = 'Microsoft Sans Serif,10'



$Form.controls.AddRange(@($mainLabel, $fileId,$filePath,$idLabel,$valueLabel,$FilePathLabel,$idValue,$descriptionValue,$description, $type, $typeValue, $silo, $siloValue, $button))

#region gui events {

function SubmitForm(){


    if($fileId.checked -eq $true){
        sc -path $filePath.Text -stream $fileId.text -value $idValue.Text
    }

    if($description.checked -eq $true){
        sc -path $filePath.Text -stream $description.text -value $descriptionValue.text
    }

    if($type.checked -eq $true){
        sc -path $filePath.Text -stream $type.text -value $typeValue.text
    }

    if($silo.checked -eq $true){
        sc -path $filePath.Text -stream $silo.text -value $siloValue.text
    }


    [System.Windows.Forms.MessageBox]::Show("Successfully Added Attributes")
}
#Add Button event 
    $Button.Add_Click({SubmitForm})

#endregion events }

#endregion GUI }



# logic here


[void]$Form.ShowDialog()

当前,用户将必须从根文件夹实际运行powershell脚本,然后将文件路径以及其余扩展属性添加到GUI输入的文本中。下面是我目前拥有的一个示例:

enter image description here

我希望用户能够右键单击任何文件,并让表单提供在Windows资源管理器中右键单击的文件的路径,而不是由进行更新的人员手动输入的路径到备用数据流。类似于使用zip7提取文件的方式(下面的示例)。

enter image description here

有人可以告诉我这是否可能吗?除了使用powershell之外,我是否应该尝试使用其他语言解决这个问题?

1 个答案:

答案 0 :(得分:4)

您可以使用Powershell来完成所有这一切。

首先,您想从代码中创建脚本并为所选文件夹输入参数。像这样:

import java.awt.*;
import java.awt.print.*;
import javax.swing.*;

public class TestGradient {

    public static class TwoRectangles extends JComponent {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.RED);
        g2d.fillRect(0, 75, 500, 150);  //paint a red opaque rectangle

        LinearGradientPaint p = new LinearGradientPaint(50, 100, 500, 100, new float[]{0.0f, 1.0f}, new Color[]{Color.GREEN, new Color(128, 128, 128, 128)}, MultipleGradientPaint.CycleMethod.NO_CYCLE);
        g2d.setPaint(p);
        g2d.fillRect(50, 50, 500, 200);  //paint a green gradient with transparency
    }
}

public static final TwoRectangles rect = new TwoRectangles();

public static void main(String[] args) throws PrinterException {

    JFrame f  = new JFrame("Test Gradient");
    f.setLayout(new BorderLayout());
    f.add(rect);
    f.setSize(600, 300);
    f.setVisible(true);


    PrinterJob printJob = PrinterJob.getPrinterJob();


    Printable prt = new Printable() {

        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            if (pageIndex > 0) 
                return(NO_SUCH_PAGE);

            rect.print(graphics);      
            return PAGE_EXISTS;
        }

    };

    printJob.setPrintable(prt);
    if (printJob.printDialog()) 
          printJob.print();

  } 
 }

接下来,您需要根据上下文菜单项和powershell脚本创建注册表参考。像这样:

param($FileName)
<# 
This script is a GUI featured way to add extended attributes to files 
#>

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#region begin GUI{ 

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '600,600'
$Form.text                       = "Add Extended Attributes"
$Form.TopMost                    = $false

# Add Extended Attributes Label
$mainLabel                       = New-Object system.Windows.Forms.Label
$mainLabel.text                  = "Add Extended Attributes"
$mainLabel.AutoSize              = $true
$mainLabel.width                 = 25
$mainLabel.height                = 10
$mainLabel.location              = New-Object System.Drawing.Point(180,10)
$mainLabel.Font                  = 'Microsoft Sans Serif,18'

# text box for entering file path 
$filePath                        = New-Object system.Windows.Forms.TextBox
$filePath.multiline              = $false
$filePath.width                  = 300
$filePath.height                 = 20
$filePath.location               = New-Object System.Drawing.Point(200,80)
$filePath.Font                   = 'Microsoft Sans Serif,10'
$filePath.Text                   = $FileName

# label for the file path text box "File Path: "
$FilePathLabel                   = New-Object system.Windows.Forms.Label
$FilePathLabel.text              = "File Path: "
$FilePathLabel.AutoSize          = $true
$FilePathLabel.width             = 25
$FilePathLabel.height            = 10
$FilePathLabel.location          = New-Object System.Drawing.Point(80,85)
$FilePathLabel.Font              = 'Microsoft Sans Serif,10'

# Attributes Label
$idLabel                         = New-Object system.Windows.Forms.Label
$idLabel.text                    = "Attributes"
$idLabel.AutoSize                = $true
$idLabel.width                   = 25
$idLabel.height                  = 10
$idLabel.location                = New-Object System.Drawing.Point(80,150)
$idLabel.Font                    = 'Microsoft Sans Serif,12'

# Values Label
$valueLabel                      = New-Object system.Windows.Forms.Label
$valueLabel.text                 = "Value"
$valueLabel.AutoSize             = $true
$valueLabel.width                = 25
$valueLabel.height               = 10
$valueLabel.location             = New-Object System.Drawing.Point(300,150)
$valueLabel.Font                 = 'Microsoft Sans Serif,12'

# Checkbox for ID attribute
$fileId                          = New-Object System.Windows.Forms.CheckBox
$fileId.text                     = "Id"
$fileId.AutoSize                 = $true
$fileId.width                    = 104
$fileId.height                   = 20
$fileId.location                 = New-Object System.Drawing.Point(100,200)
$fileId.Font                     = 'Microsoft Sans Serif,10'

# Label for the ID checkbox
$idValue                         = New-Object system.Windows.Forms.TextBox
$idValue.multiline               = $false
$idValue.width                   = 300
$idValue.height                  = 20
$idValue.location                = New-Object System.Drawing.Point(202,200)
$idValue.Font                    = 'Microsoft Sans Serif,10'

# Checkbox for Description attribute
$description                      = New-Object System.Windows.Forms.CheckBox
$description.text                 = "Description"
$description.AutoSize             = $true
$description.width                = 104
$description.height               = 20
$description.location             = New-Object System.Drawing.Point(100,250)
$description.Font                 = 'Microsoft Sans Serif,10'

# Label for the Description checkbox
$descriptionValue                 = New-Object system.Windows.Forms.TextBox
$descriptionValue.multiline       = $false
$descriptionValue.width           = 300
$descriptionValue.height          = 20
$descriptionValue.location        = New-Object System.Drawing.Point(202,250)
$descriptionValue.Font            = 'Microsoft Sans Serif,10'

# Checkbox for Type attribute
$type                             = New-Object System.Windows.Forms.CheckBox
$type.text                        = "Type"
$type.AutoSize                    = $true
$type.width                       = 104
$type.height                      = 20
$type.location                    = New-Object System.Drawing.Point(100,300)
$type.Font                        = 'Microsoft Sans Serif,10'

# Label for the type checkbox
$typeValue                        = New-Object system.Windows.Forms.TextBox
$typeValue.multiline              = $false
$typeValue.width                  = 300
$typeValue.height                 = 20
$typeValue.location               = New-Object System.Drawing.Point(202,300)
$typeValue.Font                   = 'Microsoft Sans Serif,10'

# Checkbox for silo attribute
$silo                             = New-Object System.Windows.Forms.CheckBox
$silo.text                        = "Silo"
$silo.AutoSize                    = $true
$silo.width                       = 104
$silo.height                      = 20
$silo.location                    = New-Object System.Drawing.Point(100,350)
$silo.Font                        = 'Microsoft Sans Serif,10'

# Label for the silo checkbox
$siloValue                        = New-Object system.Windows.Forms.TextBox
$siloValue.multiline              = $false
$siloValue.width                  = 300
$siloValue.height                 = 20
$siloValue.location               = New-Object System.Drawing.Point(202,350)
$siloValue.Font                   = 'Microsoft Sans Serif,10'

# submitt button 
$button                           = New-Object System.Windows.Forms.Button
$button.text                      = "Submit"
$button.AutoSize                  = $true
$button.location                  = New-Object System.Drawing.Point(250,500)
$button.Font                      = 'Microsoft Sans Serif,10'



$Form.controls.AddRange(@($mainLabel, $fileId,$filePath,$idLabel,$valueLabel,$FilePathLabel,$idValue,$descriptionValue,$description, $type, $typeValue, $silo, $siloValue, $button))

#region gui events {

function SubmitForm(){


    if($fileId.checked -eq $true){
        sc -path $filePath.Text -stream $fileId.text -value $idValue.Text
    }

    if($description.checked -eq $true){
        sc -path $filePath.Text -stream $description.text -value $descriptionValue.text
    }

    if($type.checked -eq $true){
        sc -path $filePath.Text -stream $type.text -value $typeValue.text
    }

    if($silo.checked -eq $true){
        sc -path $filePath.Text -stream $silo.text -value $siloValue.text
    }


    [System.Windows.Forms.MessageBox]::Show("Successfully Added Attributes")
}
#Add Button event 
    $Button.Add_Click({SubmitForm})

#endregion events }

#endregion GUI }



# logic here


[void]$Form.ShowDialog()

上下文菜单项:

enter image description here

传递给脚本输入参数的选择目录:

enter image description here