Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

trying to change pc names

$
0
0

Here is my testscript.ps1 file which I made, from the internet:

-------------------------------------------------------------------------------------

function renameAndReboot([string]$computer, [string]$newname)
{
        $comp = gwmi win32_computersystem  -computer $computer
        $os   = gwmi win32_operatingsystem -computer $computer

        $comp.Rename($newname)
        $os.Reboot()
}

Import-Csv mylist.csv

foreach ($entry in $list)
{
    renameAndReboot($entry.oldname,$entry.newname)

}

-------------------------------------------------------------------------------------------------------

 

mylist.csv file has 2 coloumns:

oldname newname

dt123      new123

 

When i work on a desktop machine and try to execute this, this is what I get:

-cd into the right directory,
-the csv file is in the same directory as the ps1 file 
-did this command first so i could run the script... Set-ExecutionPolicy RemoteSigned  
 - then .\testscript.ps1 to run the script


---------------------------------------------------------------------------------------
PS C:\users\psnewb\desktop> .\testscript.ps1

oldname                                                     newname
-------                                                     -------
dt123                                              new333
Get-WmiObject : Invalid namespace
At C:\users\psnewb\desktop\testscript.ps1:3 char:21
+         $comp = gwmi <<<<  win32_computersystem  -computer $computer
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : Invalid namespace
At C:\users\psnewb\desktop\testscript.ps1:4 char:21
+         $os   = gwmi <<<<  win32_operatingsystem -computer $computer
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

You cannot call a method on a null-valued expression.
At C:\users\psnewb\desktop\testscript.ps1:6 char:21
+         $comp.Rename <<<< ($newname)
    + CategoryInfo          : InvalidOperation: (Rename:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\users\psnewb\desktop\testscript.ps1:7 char:19
+         $os.Reboot <<<< ()
    + CategoryInfo          : InvalidOperation: (Reboot:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

 

 

Could anyone assist me on this? I am very new to powershell scripting.

My ultimate goal is to run a script on the domain controller to mass change pc names...but I can also just create this script and push it via GPO.

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles