I have a script to rename computers on my network. I am trying to change it so that I can enter a current name and new name for the machine as an argument or parameter (which ever makes sense in this case.) Also I would like the script to check for the argument and if it does not exist import a CSV file.
This is what I came up with and it is not working. The param appear to be empty from the console output but the IF statement runs as if they are not.
param (
$o = "oldname",
$n = "newname"
)
if(!($o = $null)){
if(!($n = $null)){
Write-Host "Renaming computer from: $o to: $n"
netdom renamecomputer $o /newName:$n /uD:domain\user /passwordD:* /force /reboot
}
}else{
Write-Host "Importing Computers from CSV file"
$csvfile = "C:\Sysinternals\rename.csv"
Import-Csv $csvfile | foreach {
$oldName = $_.OldName;
$newName = $_.NewName;
Write-Host "Renaming computer from: $oldName to: $newName"
netdom renamecomputer $oldName /newName:$newName /uD:domain\username /passwordD:* /force /reboot
}
}
[CmdletBinding()]and[Parameter(Mandatory=$true)]or[Parameter(ValueFromPipeline=$true)]. See About_Functions_Advanced_Parameters for more information. (You can also get this information withGet-Help about_Functions_Advanced_Parametersin a Powershell console or the ISE.)Import-CSV $csvfile | This-Script. It ends up more flexible (I'm not locked to a particular CSV file name), and the code probably ends up easier to debug - to the extent that any debugging would be necessary in the first place.