重定向表单的Powershell状态输出

v1uwarro  于 2023-01-05  发布在  Shell
关注(0)|答案(2)|浏览(129)

我在powershell中创建了一个表单,我想将伊势中显示的状态重定向到一个弹出窗口。目前,当我在ISE之外运行脚本时,我看不到状态栏,但看到它会很有帮助。
下面是函数

Function Upload-File {

    param (
        $LocalData
    )

Add-PSSnapin ShareFile

#Run the following interactively to create a login token that can be used by Get-SfClient in unattended scripts
$sfClient = Get-SfClient -Name ((Join-Path $env:USERPROFILE "Documents\Sharefile") + "\MySubdomain.sfps")

#upload directory is relative to the root of the account
#get the current user's home folder to use as the starting point
$ShareFileHomeFolder = (Send-SfRequest $sfClient -Entity Items).Url

# Create a PowerShell provider for ShareFile pointing to Personal Folsers\Send

New-PSDrive -Name sfDrive -PSProvider ShareFile -Client $sfClient -Root "\Personal Folders\Send" -RootUri $ShareFilePath

#upload all the files (recursively) in the local folder to the specified folder in ShareFile
Copy-SfItem -Path $LocalData -Destination "sfDrive:"

#Remove the PSProvider when we are done
Remove-PSDrive sfdrive
}

在伊势中运行时

有没有办法把它重定向到一个弹出窗口?
这是调用函数的命令

Start-Process (Upload-File -LocalData "$LocalData") -Wait

我看到了write-progress命令,但不知道如何在这里应用它。我应该做些不同的事情吗?

ne5o7dgx

ne5o7dgx1#

找到了这个Write-progress during Start-process -Wait,并根据您的场景进行了调整。一般的想法是用bypass替换wait,并让循环运行直到$process. hasexit:

$process =Start-Process (Upload-File -LocalData "$LocalData") -PassThru

for($i = 0; $i -le 100; $i = ($i + 1) % 100)
{
    Write-Progress -Activity "copier" -PercentComplete $i -Status "copying"
    Start-Sleep -Milliseconds 100
    if ($process.HasExited) {
        Write-Progress -Activity "copier" -Completed
        break
    }
}

然而,在这种情况下,状态只是一个计数器,但你可以用一些真实的东西来替换它,并将for循环更改为:

Do{
write-progress…
}while(-not $process.HasExited)
jvlzgdj9

jvlzgdj92#

根据我的评论,当从控制台运行时,您必须将表单/GUI资源/名称空间添加到代码的顶部,以便加载表单内容,因为它们不会像伊势中那样自动加载。

注意:您不需要所有的名称空间。我在这里列出这些名称空间只是为了让您了解PS GUI方案中最常用的名称空间。

Add-Type -AssemblyName  System.Drawing,
                        PresentationCore,
                        PresentationFramework,
                        System.Windows.Forms,
                        microsoft.VisualBasic
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form               = New-Object System.Windows.Forms.Form
$Form.width         = 400
$Form.height        = 600
$Form.Text          = 'Add Resource'
$Form.StartPosition = 'CenterScreen'

$pbrTest          = New-Object System.Windows.Forms.ProgressBar
$pbrTest.Maximum  = 100
$pbrTest.Minimum  = 0
$pbrTest.Location = new-object System.Drawing.Size(10,10)
$pbrTest.size = new-object System.Drawing.Size(100,50)

$i = 0

$Form.Controls.Add($pbrTest)

$btnConfirm          = new-object System.Windows.Forms.Button
$btnConfirm.Location = new-object System.Drawing.Size(120,10)
$btnConfirm.Size     = new-object System.Drawing.Size(100,30)
$btnConfirm.Text     = 'Start Progress'
$Form.Controls.Add($btnConfirm)

$btnConfirm.Add_Click({
    
    While ($i -le 100) 
    {
        $pbrTest.Value = $i
        Start-Sleep -m 1
        'VALUE EQ'
        $i
        $i += 1
    }
})

$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

相关问题