gpt4 book ai didi

powershell - 传递SwitchParameter作为参数

转载 作者:行者123 更新时间:2023-12-03 00:27:30 24 4
gpt4 key购买 nike

我想知道如何将SwitchParameter作为参数传递。

function ExecuteWindowsTaskScheduler {
param(
[Parameter(Mandatory=$true)] [String] $TaskName #e.g. "Open Notepad task"
, [Parameter(Mandatory=$true)] [String] $TimeToExecute #e.g. "3:45pm/am"
, [Parameter(Mandatory=$true)] [System.Management.Automation.SwitchParameter] $FrequencyToExecute #e.g. -Once, -Daily, -Weekly, -DaysInterval...
, [Parameter(Mandatory=$true)] [String] $DomainAndUser #e.g. "yourdomain\yourusername"
, [Parameter(Mandatory=$true)] [String] $ProgramWithPath #e.g. "C:\PowerShell\yourFile.ps1"
, [Parameter(Mandatory=$false)] [bool] ${DebugMode}
)



$Trigger= New-ScheduledTaskTrigger -At $TimeToExecute -Once # Specify the trigger settings
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument $ProgramWithPath # Specify what script to run and with its parameters
$TaskExists= Get-ScheduledTask | Where-Object {$_.TaskName -like $TaskName }

Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -User $DomainAndUser -Action $Action

}

ExecuteWindowsTaskScheduler "Open Notepad task" "11:56am" -Once "xxx\yyy" "C:\PowerShell\OpenNotepad.ps1"

我有参数 $FrequencyToExecute,当我调用该函数时,我希望能够传递以下命令之一:-Once,-Daily,-Weekly等。

可能吗?我怎样才能做到这一点?

最佳答案

您可能需要将参数类型更改为[string],然后使用[ValidateSet]属性来强制执行“Once”,“Daily”或“Weekly”值,然后使用splatting将适当的开关传递给New-ScheduledTaskTrigger:

function ExecuteWindowsTaskScheduler {
param(
[Parameter(Mandatory = $true)]
[String]$TaskName, #e.g. "Open Notepad task"

[Parameter(Mandatory = $true)]
[String]$TimeToExecute, #e.g. "3:45pm/am"

[Parameter(Mandatory = $true)]
[ValidateSet('Once','Daily','Weekly','Monthly')]
[string]$FrequencyToExecute, #e.g. -Once, -Daily, -Weekly, Monthly

[Parameter(Mandatory = $true)]
[String]$DomainAndUser, #e.g. "yourdomain\yourusername"

[Parameter(Mandatory = $true)]
[String]$ProgramWithPath, #e.g. "C:\PowerShell\yourFile.ps1"

[Parameter(Mandatory = $false)]
[bool]${DebugMode}
)

$TriggerParams = @{
At = $TimeToExecute
}
# Add the appropriate frequency value to the splatting table
if ($FrequencyToExecute -eq 'Monthly') {
$TriggerParams.Add('Weekly',$true)
$TriggerParams.Add('WeeksInterval',4)
$TriggerParams.Add('DaysOfWeek','Monday')
}
elseif ($FrequencyToExecute -eq 'Weekly') {
$TriggerParams.Add('Weekly',$true)
$TriggerParams.Add('WeeksInterval',1)
$TriggerParams.Add('DaysOfWeek','Monday')
}
else {
$TriggerParams.Add($FrequencyToExecute,$true)
}

$Trigger = New-ScheduledTaskTrigger @TriggerParams

$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument $ProgramWithPath # Specify what script to run and with its parameters
$TaskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $TaskName }

Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -User $DomainAndUser -Action $Action
}

关于powershell - 传递SwitchParameter作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52008140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com