Sunday, 25 September 2011

Set-ComputerName.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script renames a computer using WMI. 
  4. .DESCRIPTION 
  5.     This script uses the Rename method from
  6.     the Win32_OperatingSystem WMI class.  
  7.     This is sample 5 from http://msdn.microsoft.com/en-us/library/aa394586 
  8. .NOTES 
  9.     File Name  : Set-ComputerName.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted tot: 
  16.         http://msdn.microsoft.com/en-us/library/aa394586%28VS.85%29.aspx 
  17. .EXAMPLE 
  18.     Left as an exercise for the Reader 
  19. #> 
  20. # This script takes two parameters, The computer to  
  21. # rename and then the newname for that computer 
  22.  
  23. param
  24. [$String] $NewName = 'NewName'
  25. [$string] $Comp = "." 
  26.  
  27. # Get computer object 
  28. $Computer = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $comp 
  29.  
  30. #Rename the Computer 
  31. $Return  = $Computer.Rename($NewName
  32.  
  33. if ($return.ReturnValue -eq 0) { 
  34.    "Computer name is now: $NewName" 
  35.    " but you need to reboot first" 
  36. } else
  37. "  RenameFailed, return code: {0}" -f $return.ReturnValue 

No comments: