Powershell代码为生日蛋糕

时间:2017-11-15 16:47:07

标签: powershell

我的儿子是计算机科学专业,他的生日快到了。我想为他创建一个代码主题蛋糕。我研究过PowerShell代码并创建了我认为正确和有趣的东西。但是,我不确定并被转到此网站以获得帮助。这就是我的...... JularJ是我儿子的电子游戏显示名称。

concurrent_hash_map

5 个答案:

答案 0 :(得分:5)

  

对不起,如果这太糟糕了。

可能会更糟糕; - )

两件事 - 确保使用$为变量名添加前缀,并使用-eq进行相等性比较:

if ($today -eq $day)

然后将命令与带有空格的参数分开:

echo "Happy birthday ..."

最后,施加一些缩进会使它看起来更专业:

$day = Get-Date 11/21/2017
$today = Get-Date
if ($today -eq $day) {
    echo "Happy 18th Birthday [Jular]"
}
else {
    echo "Eat Cake Anyways"
}

答案 1 :(得分:1)

绝对是你儿子的好​​主意!以下是我在PowerShell中编写代码的方法

$day = 11/21/2017

$today = Get-Date -Format MM/dd/yyyy

if ($today -eq $day){
    Write-Host "Happy 18th Birthday [Jular]"
}else{
    Write-Host "Eat Cake Anyway"
}

-Format参数对Get-Date函数很重要。否则它总会说“吃蛋糕”。

答案 2 :(得分:-1)

以下是对您当前代码的修复,以便运行:

$Birthday = [DateTime]'11/21/2017'
$Today = Get-Date

If ($Birthday.Date -eq $Today.Date)
{
    'Happy 18th Birthday, JularJ!'
}
Else
{
    'Eat Cake Anyways'
}

这是一个奖励,将其放入.bat.cmd文件中,您无需担心执行策略:

powershell -ExecutionPolicy Bypass -NoProfile -NoExit -NoLogo -EncodedCommand "JABCAGkAcgB0AGgAZABhAHkAIAA9ACAAWwBEAGEAdABlAFQAaQBtAGUAXQAnADEAMQAvADIAMQAvADIAMAAxADcAJwAKACQAVABvAGQAYQB5ACAAPQAgAEcAZQB0AC0ARABhAHQAZQAKAAoASQBmACAAKAAkAEIAaQByAHQAaABkAGEAeQAuAEQAYQB0AGUAIAAtAGUAcQAgACQAVABvAGQAYQB5AC4ARABhAHQAZQApAAoAewAKACAAIAAgACAAJwBIAGEAcABwAHkAIAAxADgAdABoACAAQgBpAHIAdABoAGQAYQB5ACwAIABKAHUAbABhAHIASgAhACcACgB9AAoARQBsAHMAZQAKAHsACgAgACAAIAAgACcARQBhAHQAIABDAGEAawBlACAAQQBuAHkAdwBhAHkAcwAnAAoAfQA="

这是您在base64中编码的脚本,并将执行所有相同的操作。

您可以通过在PowerShell中运行以下命令进行验证:

[Text.Encoding]::Unicode.GetString([Convert]::FromBase64String('JABCAGkAcgB0AGgAZABhAHkAIAA9ACAAWwBEAGEAdABlAFQAaQBtAGUAXQAnADEAMQAvADIAMQAvADIAMAAxADcAJwAKACQAVABvAGQAYQB5ACAAPQAgAEcAZQB0AC0ARABhAHQAZQAKAAoASQBmACAAKAAkAEIAaQByAHQAaABkAGEAeQAuAEQAYQB0AGUAIAAtAGUAcQAgACQAVABvAGQAYQB5AC4ARABhAHQAZQApAAoAewAKACAAIAAgACAAJwBIAGEAcABwAHkAIAAxADgAdABoACAAQgBpAHIAdABoAGQAYQB5ACwAIABKAHUAbABhAHIASgAhACcACgB9AAoARQBsAHMAZQAKAHsACgAgACAAIAAgACcARQBhAHQAIABDAGEAawBlACAAQQBuAHkAdwBhAHkAcwAnAAoAfQA='))

答案 3 :(得分:-1)

尝试以下代码。它每年都有效;)

cls

$day=Get-Date -Day 21 -Month 11 -Year 2000
$today=Get-Date

if ($today.Day -eq $day.Day -and $today.Month -eq $day.Month)
{
    $age = (New-Object DateTime -ArgumentList ($today - $day).Ticks).Year

    1..$age | %{'.' * $_}
    "Happy {0}th Birthday [Jular] ! " -f $age
    $age..1 | %{'.' * $_}
}
else
{
    "Eat Cake Anyway"
}

答案 4 :(得分:-1)

这是我的,没有变量的较短版本:

if ([datetime]::Now.ToString("MM/dd/yyyy") -eq "11/21/2017") 
{
    "Happy 18th Birthday [Jular]"
}
else 
{
    "Eat Cake Anyway"
}

生日快乐:)