You are using:
$Using:NewComputerName
In your ScriptBlock for Invoke-Command, correct? You dont show that code.
You are using:
$Using:NewComputerName
In your ScriptBlock for Invoke-Command, correct? You dont show that code.
Good suggestion. I have tried the following but I am getting an error message. I was trying to testing with use the the “Test” in stead of variable, but I am getting an error message. There must be something wrong with syntax, but I can not seem to figure it out.
# Elevating to administrator
param([switch]$Elevated)
Set-ExecutionPolicy Bypass -Scope CurrentUser -Force
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated) {
# tried to elevate, did not work, aborting
} else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
# This is simple test to see of computer can be renamed to "Test" using invoke-command.
$CompName= "Test"
Invoke-Command -ScriptBlock { Rename-Computer -Newname $using:CompName}
A fter a lot of trials and error I think I pinpoint the problem. I created a separate file and used only the bare essential without input boxes and I was able to get the variable for the computer name to work without having to pass any parameters. The problem is somewhere in script. I need to do breaks point and check value of the boxes to see if they have valid data. Also a strange seems to happen and I not sure if is my computer. When I change the computer name for example to “Test” and then change to it “Test1”, the computer name will still be “Test”. If I change it again to “John” it will change to “John”. If change it again to “John Doe” it still have the name “John’”…
I think that may a problem with script.
The code I tested, and that you originally posted, did not use Invoke-Command
.
Invoke-Command
may introduce problems for you because you’re using workgroup computers which are more difficult to configure for remoting.
The Rename-Computer
cmdlet does not depend on PS remoting so you would be better off, IMHO, using it directly, with the -ComputerName
parameter:
Rename-Computer -ComputerName OldComputerName -NewName $NewComputerName
You are correct about the Invoke-Command, furthermore I was not able to get it to pass the parameters so I stuck with the basic statement and there was not need to used the old computer since it was you like mentioned a local computer so the old name is not required as part of the statement. I am working on minor issues but I seem to progressing.
Rename-Computer -Newname $NewComputerName
I solve the problem. I used the department and computer description to test it out. The only thing I need to do is create a error trap when the computer name is invalid. For now here is what was happening.
I was using this code below. (Note that had $Serialnumber instead of $CompDesc). The problem with that is that the code is that it not pulling the data from the text box and is that I needed. That is why I was thinking of using parameters.
$NewComputerName = $DeptID + "-"+ $CompDesc
Rename-Computer -Newname $NewComputerName
The good thing is with text boxes pulling the data from is simple. All I had to do was to put .text at end of variable shown below.
$NewComputerName = $DeptID.text + "-"+ $CompDesc.text
Rename-Computer -Newname $NewComputerName
Thanks for all the feed back. Hope this helps someone who runs into this issue.