
PC 이름을 바꾸는 PowerShell 명령이 다음과 같다는 것을 알고 있습니다.
Rename-Computer -NewName "LAPTOP-*******" -Force
"DESKTOP"을 "LAPTOP"로 바꾸고 PC 이름에 다음 8자를 남겨 두는 스크립트가 필요합니다.
어떤 제안이 있으십니까?
편집하다:
좋아, 해결책에 가까워지고 있다고 생각합니다.
$cmdOutput = $env:COMPUTERNAME -replace "DESKTOP", "LAPTOP" | Out-String
Rename-Computer -NewName "$cmdOutput" -Force
하지만 허용되지 않는 문자에 대한 오류가 발생합니다(사실이 아닙니다).
Rename-Computer : Il computer 'DESKTOP-RJL7RM8' con il nuovo nome 'LAPTOP-RJL7RM8
' verrà ignorato perché il nuovo nome non è valido. Il formato del nuovo nome di computer immesso non è corretto. I
nomi standard possono contenere lettere (a-z, A-Z), numeri (0-9) e segni meno (-), ma non sono consentiti spazi o
punti (.). Il nome non può essere costituito esclusivamente da cifre e la lunghezza non può essere maggiore di 63
caratteri.
In D:\RenamePC\RenamePC.ps1:2 car:1
+ Rename-Computer -NewName "$cmdOutput" -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (LAPTOP-RJL7RM8
:String) [Rename-Computer], InvalidOperationException
+ FullyQualifiedErrorId : InvalidNewName,Microsoft.PowerShell.Commands.RenameComputerCommand
재수정:
나는 오류가 새 이름 뒤에 새 줄이 있다고 생각합니다. -NoNewLine
첫 번째 줄 끝에 추가하려고 했지만 Powershell이 대답했습니다.
A parameter cannot be found that matches parameter name 'NoNewLine'.
답변1
공백을 "트림"하면됩니다. 코드는 다음과 같습니다.
$cmdOutput = ($env:COMPUTERNAME -replace "DESKTOP", "LAPTOP" | Out-String).Trim()