当前位置: 动力学知识库 > 问答 > 编程问答 >

parameter passing - PowerShell Function using ParamSets with a for loop

问题描述:

I put together a powershell function that schedules server reboots.

I'm stuck on adding a for loop to run the function for multiple servers using ParameterSetName.

Before adding the ParamSets, I just setup a function and called the function in the same script within a for loop which worked great.

Function ScheduleReboot{

[CmdletBinding(SupportsShouldProcess=$true,DefaultParameterSetName="ViewOnly")]

Param(

[Parameter(Mandatory=$false,ParameterSetName="KillSwitch")]

[Switch]$NukeJobs,

[Parameter(Mandatory=$true,

HelpMessage="ServerName goes here",ParameterSetName="FullRun")]

[string]$server,

[Parameter(Mandatory=$true,

HelpMessage="Enter a Date/Time 07-28-15 16:00 For July 28th, 2015 at 4:00 PM",

ParameterSetName="FullRun"

)]

[ValidatePattern('(?#Enter MM-dd-YY HH:MM 24hr clock)\d{2}-\d{2}-\d{2}\s\d{2}[:]\d{2}')]

$date,

[Parameter(Mandatory=$false,ParameterSetName="ViewOnly")]

[switch]$ViewOnly

) #"$server will reboot on $date"

Switch ($PSCmdlet.ParameterSetName){

"KillSwitch"{

if($env:PROCESSOR_ARCHITECTURE -eq "x86")

{

set-alias ps64 "$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe"

$script2 = [ScriptBlock]::Create("IF(Test-Path -Path C:\Windows\System32\Tasks\Microsoft\Windows\PowerShell\ScheduledJobs\$server)

{Remove-Item -Force -Path C:\Windows\System32\Tasks\Microsoft\Windows\PowerShell\ScheduledJobs\$server}")

"Removing previous scheduled server reboot data"

ps64 -command $script2

} else {

Get-ScheduledJob | Unregister-ScheduledJob

}

}#End killswitch block.

"FullRun" {

[array]$servers = gc D:\Scripts\servers.txt

[array]$dates = gc D:\Scripts\dates.txt

if($dates.Length -eq $servers.Length)

{

"Input count matches"

for($i=0;$i -lt $servers.Length;$i++)

{

$day = $dates[$i]

$comp = $servers[$i]

$user = Get-Credential -UserName $env:USERNAME -Message "UserName/password for scheduled Reboot"

$trigger = New-JobTrigger -once -at $day

$script = [ScriptBlock]::Create("D:\Scripts\Scheduled-Reboot-Single.ps1 -server $server | Out-File -Force \\SysLogSvr\d$\scripts\$server-Reboot.log")

Register-ScheduledJob -Name $comp -Credential $user -Trigger $trigger -ScriptBlock $script

}#end for loop

} else {

$warn = "Server list contains " + $servers.Count + " items and Date list contains " + $dates.Count + " items, Please re-check!!!"

Write-Warning $warn

}

}#end fullrun block.

"ViewOnly" {

Get-ScheduledJob | Get-JobTrigger

Get-ScheduledJob | Select Name,Command | FT -AutoSize

}#end viewonly block.

}#End param switch

}#end function.

网友答案:

Make that 3 (or more) different functions. The parameter sets the way you are using it is over-complicating it and making it hard for you to debug it.

I would have made a Get-ScheduledReboot, Clear-ScheduledReboot, Set-ScheduledReboot collection of functions.

There are a few bugs in this script as posted that are causing you issues. The first is the nuke parameter set does not contain the server parameter. The other bug is the one that is throwing you off. You define a $server parameter above but in the function, you change gears and load from a text file. You are mixing $server, $servers and $comp variables around causing it to fail.

You should change your [string]$server to [string[]]$server or better yet, [string[]$ComputerName and foreach($node in $ComputerName){...} on the collection. Keep the text file loading outside that function.

分享给朋友:
您可能感兴趣的文章:
随机阅读: