对于我的实验室,我试图做一些自动化,同时试图在Powershell做得更好。目前我使用下面粘贴的两个功能:
#Set a new hostname for a local virtual machine.
function New-VmHostname ($VmHostname, $NewComputerName, $credential) {
Invoke-Command -VMName $VmHostname {
Rename-Computer -NewName $using:NewComputerName -Force} -Credential $credential
}
#Set a new hostname for a remote machine.
function New-RemoteHostname ($RemoteHostname, $NewComputerName, $credential) {
Invoke-Command -ComputerName $RemoteHostname {
Rename-Computer -NewName $using:NewComputerName -Force} -Credential $credential
}
现在我正在尝试编写一个函数,以便能够合并上述两个函数。像这样:
#Set a new hostname for a lab machine
function New-Hostname {
# Parameter help description
Param(
[Parameter(Position=0,
Mandatory=$True,
ValueFromPipeline=$True)]
[string]$VmName,
[Parameter(Position=1,
Mandatory=$True,
ValueFromPipeline=$True)]
[string]$NewComputerName,
[Parameter(Position=2,
Mandatory=$True,
ValueFromPipeline=$True)]
[ValidateSet("Remote","VM")]
[string]$RemoteSessionType
)
# Do stuff
}
我想要的是函数有一个参数$RemoteSessionType
来决定cmdlet下线是使用-VMName
还是使用-ComputerName
参数。我似乎无法弄清楚如何。