Powershell else if和文本颜色初学者

Powershell else if和文本颜色初学者,powershell,Powershell,我有一个作业要交,我的代码有一些问题。 如果该语句为真,则必须以绿色打印文本;如果为假,则必须以红色打印文本。我有麻烦,以及让它打印的是真是假 这是到目前为止我的代码 $cpr = '123456-1212' -match '\d\d\d\d\d\d-\d\d\d\d' if $cpr{ Executes when the Boolean expression is true }else { Executes when the Boolean expression is fal

我有一个作业要交,我的代码有一些问题。 如果该语句为真,则必须以绿色打印文本;如果为假,则必须以红色打印文本。我有麻烦,以及让它打印的是真是假

这是到目前为止我的代码

$cpr = '123456-1212' -match '\d\d\d\d\d\d-\d\d\d\d'

if $cpr{
    Executes when the Boolean expression is true
}else {
    Executes when the Boolean expression is false
}
如果我打印

'123456-1212' -match '\d\d\d\d\d\d-\d\d\d\d'
就其本身而言,给出一个真值或假值是没有问题的。

regex有一种方式来表示“这么多”。我会对目标模式使用类似于
'\d{6}-\d{4}'
的东西,只是因为我在计算重复次数时遇到问题。。。[咧嘴笑]
$cpr = '123456-1212' -match '\d\d\d\d\d\d-\d\d\d\d'

if($cpr)
{
    write-host -foregroundcolor yellow "Statement is true"
}
else
{
    write-host -foregroundcolor red "Statement is false"
}