小编Bya*_*aku的帖子

如果工作完成,该怎么做

我正在尝试为我的PowerShell脚本实现GUI,以简化其他用户的某个过程.我有以下PowerShell脚本:

if ($checkBox1.Checked)    { 
    Try{
    Start-Job { & K:\sample\adp.cmd }
    $listBox1.Items.Add("ADP-Job started...")
    }catch [System.Exception]{
    $listBox1.Items.Add("ADP --> .cmd File not found!")}
    }

    if ($checkBox2.Checked)    { 
    Try{ 
    Start-Job { & K:\sample\kdp.cmd }
    $listBox1.Items.Add("KDP-Job started...")
    }catch [System.Exception]{
    $listBox1.Items.Add("KDP --> .cmd File not found!")}
    }
Run Code Online (Sandbox Code Playgroud)

有没有办法连续检查所有正在运行的作业,并为已完成的每个作业执行某些操作?例如,在我的列表框中打印出类似这样的内容:ADP-Files have been uploaded

由于每个Job约需5分钟--4个小时我想到了一个循环,如果一个Job完成,每5分钟检查一次,但我无法弄清楚如何区分每个Job来做一些特定的事情.

powershell jobs

7
推荐指数
2
解决办法
5871
查看次数

PowerShell Get-ChildItem如何捕获异常

我目前正在编写一个可视错误GUI,在处理过程中捕获任何异常,并为用户提供"易于理解"的错误消息.但似乎在使用Get-ChildItemcmdlet 时我无法捕获任何异常.我是否必须使用与try/catch不同的方法?

这是PowerShell脚本:

if ($checkBox1.Checked)    {
    Try{
        Get-ChildItem -path K:\adm_spm_logdb_data\ADP\DATA |Rename-Item -newname { $($_.BaseName -split '_provide')[0] + $_.Extension };
        }catch [System.Exception]{
        $listBox1.Items.Add("An error occured while trying to process ADP-Files please check the manual!")
        }
        $listBox1.Items.Add("Please check your System files to ensure the process has finished")
    }
Run Code Online (Sandbox Code Playgroud)

我试图通过使用虚假创建例外-path,其导致DriveNotFoundException.但看起来我无法通过使用try/catch来捕获它.

powershell exception try-catch

3
推荐指数
1
解决办法
6290
查看次数

在文本框中输入时 PowerShell 调用函数

我正在尝试验证在文本框中进行的用户输入。但我不想总是添加一个必须按下的按钮,而是想在用户按下“Enter”键时自动检查它。

所以我的问题是:一旦用户在特定文本框中按下 Enter 键,PowerShell 就可以调用函数吗?

powershell eventhandler

3
推荐指数
1
解决办法
1万
查看次数

如何在PowerShell中正确舍入数字

如标题所示,我在舍入数字时遇到一些问题。我的脚本当前如下所示:

[uint16]$Product1 = Read-Host "Enter the price of your product: "
...
#$TotalProducts contains the Value from all products together
Write-Host "You spent an amount of $TotalProducts for todays shopping."
Run Code Online (Sandbox Code Playgroud)

我希望程序将数字四舍五入,以使总数不是一个荒谬的长数字。它确实可以工作,但是在我手动计算之后,我看到程序计算了其他内容。

这里的问题是程序将例如122.50舍入122,而不是123

我尝试使用以下语法,但未成功:

[math]::Round($Product1)[System.Midpoint.Rounding]::AwayFromZero) = Read-Host "Enter the price of your product: "
Run Code Online (Sandbox Code Playgroud)

我是在尝试正确的方法,但只是在挑剔语法,还是这种方法完全错误?

math powershell rounding

3
推荐指数
1
解决办法
4510
查看次数

性能优势 - 多个PowerShells与后台作业

我制作了一个同时启动多个后台作业的程序.目前,该过程需要几个小时(有时3小时,有时长达6小时,具体取决于文件和大小).

重写代码是否有任何优势,因此它不会使用Background-Jobs启动同一Shell中的所有进程,而是为每个单独的进程启动PowerShell?或者两者之间没有什么大不同?

$handler_button1_Click= 
{
    if ($checkBox1.Checked)    { 
    Try{
    $job1 = start-jobhere {& C:\Users\mosermich\Desktop\Extractor\Ressources\adp_staging.bat} -Name "ADP-DATA"
    $listBox1.Items.Add("ADP-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox2.Checked)    { 
    Try{ 
    $job2 = start-jobhere {& C:\Users\mosermich\Desktop\Extractor\Ressources\kdp_staging.bat} -Name "KDP-DATA"
    $listBox1.Items.Add("KDP-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox3.Checked)    { 
    Try{ 
    $job3 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\mdp_staging.bat} -Name "MDP-DATA"
    $listBox1.Items.Add("MDP-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox4.Checked)    { 
    Try{ 
    $job4 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\zdlb_staging.bat} -Name "ZDL-B-DATA"
    $listBox1.Items.Add("ZDLB-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox5.Checked)    { 
    Try{ 
    $job5 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\zdls_staging.bat} …
Run Code Online (Sandbox Code Playgroud)

powershell performance

2
推荐指数
1
解决办法
44
查看次数

使用随机数生成X/0模式(无硬编码)

我想使用1到4之间的随机数生成某个模式.模式如下所示:

1 = X000000000
2 = X0000X0000
3 = X00X00X000
4 = X0X0X0X000
Run Code Online (Sandbox Code Playgroud)

基本上随机数表示,在模式中将使用多少个X,并且0将以规则的间隔填充.

有没有办法在不使用硬编码变量的情况下生成这些模式?

编辑

因为它看起来不容易理解我在这里写下所有的详细信息:我使用以下代码来定义程序必须为随机生成的每个数字输出的内容:

 if (i == 1){
 System.println("X000000000");
 if (i == 2){
 System.println("X0000X0000");
 if (i == 3){
 System.println("X00X00X000");
 if (i == 4){
 System.println("X0X0X0X000");
Run Code Online (Sandbox Code Playgroud)

有没有办法使用一种方法,如果问题不包括使用硬编码?

1-4的模式总是会像这样.这取决于生成的数字,输出的内容.

java

2
推荐指数
1
解决办法
76
查看次数

多维数组如何在 PowerShell 中工作?

我目前正在尝试创建一个程序来训练一门外语。为此,我有两个一维数组。在第一个中,我存储了所有外语音节或单词,在第二个中,我用我的母语存储了答案:

$QuestionArray = New-Object System.Collections.ArrayList
$QuestionArray = "Word1","Word2","Word3"

$AnswerArray = New-Object System.Collections.ArrayList
$AnswerArray = "Answer1","Answer2","Answer3"
Run Code Online (Sandbox Code Playgroud)

之后我检查输入的值是否在答案数组内。如果是我从问题数组中选择随机选择的单词的索引和索引。如果两个索引匹配,则问题已得到正确回答,否则为错误。

$RandomQuestion = $QuestionArray | Get-Random
$Answer = $InputTextbox.Text
$IndexPositionQuestion = [array]::indexof($QuestionArray, $RandomQuestion)
$IndexPositionAnswer = [array]::indexof($AnswerArray, $Answer)

If($IndexPositionAnswer -eq $IndexPositionQuestion){
    $RightTextbox.Text = $script:countercorrect++
}else{
    $WrongTextbox.Text = $script:counterwrong++
}  
Run Code Online (Sandbox Code Playgroud)

该程序按预期运行,但是当我今天向一位同事展示它时,他只是告诉我该程序的比较部分编码很丑陋,并且不是最佳实践。

我怎么能以任何其他方式去做呢?我读了一些关于多维数组的内容,但我无法理解它。我将如何从多维数组中受益?我如何从中选择我需要的值来显示、比较、检查等?

arrays powershell multidimensional-array

1
推荐指数
1
解决办法
4850
查看次数