I would suggest some changes:
Set-ExecutionPolicy
is unnecessary, because if the machine has started processing the script, then the executionpolicy isn't a problem. So remove that, and specify it in thepowershell.exe
-call instead, like:powershell.exe -executionpolicy unrestricted
Use
if($oldname -ne $name) { rename-computer .... }
so you can remove the else part. Much cleaner
Try running the modified script below, and report back with the output in the scriptlog.txt
-file.
$logpath = "c:\scriptlog.txt"
$name = (Get-WmiObject win32_bios).SerialNumber.Trim()
$oldname = (Get-WmiObject win32_computersystem).Name.Trim()
"NewName is '$name'" | Out-File $logpath -Append
"OldName is '$oldname'" | Out-File $logpath -Append
IF ($oldname -ne $name){
"If-test TRUE" | Out-File $logpath -Append
Rename-computer -ComputerName $oldname -NewName $name -Force
Start-Sleep -s 5
Restart-Computer
} else { #I've added the else-part just because of logging.
"IF-test FALSE" | Out-File $logpath -Append
}