1

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
    }
}
4
  • 2
    This is very nearly an ideal case for the use of [CmdletBinding()] and [Parameter(Mandatory=$true)] or [Parameter(ValueFromPipeline=$true)]. See About_Functions_Advanced_Parameters for more information. (You can also get this information with Get-Help about_Functions_Advanced_Parameters in a Powershell console or the ISE.) Commented Jan 31, 2017 at 13:10
  • @JeffZeitlin His script is importing the new and the oldname from a csv in case the parameters are not specified. Why should he use the mandatory or valuefrompipeline attribute? Commented Jan 31, 2017 at 13:20
  • @MartinBrandl - Point taken. I'd actually end up rewriting this in one of two ways: (1) Two parameter sets - one for when I supply a computer name, and one when I want to import them from a file, or (2) ValueFromPipeline and then call it using 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. Commented Jan 31, 2017 at 13:35
  • @JeffZeitlin Agree, that would be a better function instead of magically doing it in the script. Commented Jan 31, 2017 at 13:43

1 Answer 1

3

You are assigning $o and $n to $null in your if statement - instead of comparing it. You can check whether $o is null like this:

if($o) 
{
}

But since you are comparing a string, you probably want to check whether the string is null or empty using the static [string]::IsNullOrEmptymethod. So your refactored code could look like this:

param (
    $o = "oldname",
    $n = "newname"
)

if ([string]::IsNullOrEmpty($o) -or [string]::IsNullOrEmpty($n))
{
    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
    }
}
else
{
    Write-Host "Renaming computer from: $o to: $n"
    netdom renamecomputer $o /newName:$n /uD:domain\user /passwordD:* /force /reboot
}
Sign up to request clarification or add additional context in comments.

2 Comments

if wanted to add the password as a parameter. would i just add a $password to the top of the param list?
sure you can do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.