For my lab i'm trying to do some automation while trying to get better at Powershell in the mean time. Currently I'm use the two functions pasted below:
#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
}
Now I'm trying to write a function to be able to incorporate both functions above. Something like this:
#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
}
What I want is for the function to have a parameter, $RemoteSessionType, to decide if the cmdlet downline uses the -VMName or uses the -ComputerName parameter. I can't seem to figure out how though.