Creating a filename parameter

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5868

    by russelljspicer at 2013-04-30 07:01:14

    Looking to create a parameter for naming a file that will be used as a list of computers to run a couple of functions against.

    heres the code i have so far

    Function NmCmptrLst {

    Param(
    [Parameter(Mandatory=$True)]
    [string]$FileName
    )

    $computerlist = $FileName

    }

    I think my issue is creating a variable that is a variable, just cant think of how i would change it...

    it is going to run in this portion of the script body:

    NmCmptrLst

    GetComputers >> $computerlist

    $arrComputers = get-Content -Path $computerlist
    foreach ($strComputer in $arrComputers)

    any ideas?

    by DonJ at 2013-04-30 07:54:29

    Please note that the CODE button, rather than the italics button, will format your code here in the forums.

    If you're going to use the [Parameter()] attribute, you should add [CmdletBinding()] also:


    function foo {
    [CmdletBinding()]
    Param(...)
    }

    If your goal is to read the contents of the file:


    $Computers = Get-Content $filename
    foreach ($computer in $computers) {
    }

    All your code is currently doing is putting the filename into the $computerlist variable, which isn't necessary.

    by russelljspicer at 2013-04-30 08:28:37

    Thanks for the response, I don't think i explained this properly. What my code is doing(or attempting to) is to create a filename for this computer list that is generated by querying AD. Here is more:

    So here i am trying to create a parameter that will let whoever runs the script input the filename
    Function NmCmptrLst {

    Param(
    [Parameter(Mandatory=$True)]
    [string]$FileName
    )

    $computerlist = "h:\myscripts\$FileName"

    }

    Once that file name is created i have this code that queries AD to create the actual file:

    Function GetComputers {

    Param(
    [Parameter(Mandatory=$True)]
    [string]$OrgU
    )

    $ADsPath = [ADSI]“LDAP://OU=$OrgU,OU=Workstations,DC=XXXX,DC=XXXX,DC=ORG”
    $Search = New-Object DirectoryServices.DirectorySearcher($ADsPath)
    $Search.filter = “(objectClass=computer)”
    $Search.PageSize = 1000
    $Search.SearchScope = “SubTree”
    $results = $Search.Findall()

    Foreach($result in $results){
    $computer = $result.GetDirectoryEntry()
    $computer.Name
    }

    }

    Once that file is created it is then used against the other functions:

    $arrComputers = get-Content -Path $computerlist
    foreach ($strComputer in $arrComputers)

    Sorry for not being more complete earlier. I hope this clarifies. BTW I am really new to this so I appreciate any help you give me.

    by DonJ at 2013-04-30 08:40:55

    Uh... okay. I'm still not sure exactly what the question is. What isn't working? It's a bit unusual to create a function whose sole purpose is to add a path to a filename... seems over-modularized, but if that's what you want, sure.

    by russelljspicer at 2013-04-30 09:00:57

    I like to make it that way so its easier to add the same function to another script and keep the actual script body very efficient.

    So my problem is that when i run this it does not create a file...

    also i didnt add in what the script body is:

    NmCmptrLst

    GetComputers >> $computerlist

    $arrComputers = get-Content -Path $computerlist
    foreach ($strComputer in $arrComputers)

    it finishes without error but does not create a file.

    by DonJ at 2013-04-30 09:19:05

    Ah. OK. In this function:


    Function NmCmptrLst {

    Param(
    [Parameter(Mandatory=$True)]
    [string]$FileName
    )

    $computerlist = "h:\myscripts\$FileName"

    }

    You are not producing any output.


    Function NmCmptrLst {

    Param(
    [Parameter(Mandatory=$True)]
    [string]$FileName
    )

    $computerlist = "h:\myscripts\$FileName"
    Write-Output $computerlist
    }

    Will output $computerlist as the result of the function. $computerlist remains defined only inside that function – $computerlist is not accessible outside the function. Read "about_scope" in PowerShell's help system for details. So the function has to output its result, and you have to capture that into a variable.

    Also, in your example, you're just running NmCmptrLst – you're not providing it with a filename parameter. It'll prompt for one, though.


    $myfilename = NmCmptrLst -filename whatever.txt
    GetComputers | Out-File $myfilename

    by russelljspicer at 2013-04-30 10:11:26

    Awesome!

    That worked perfectly except i removed the filename parameter because it was just naming the file "whatever.txt" when removed it prompted me to write one.

    BTW thanks for all of your patience, this forum is a wealth of knowledge!

    by DonJ at 2013-04-30 10:43:34

    Well, you'd use the -filename parameter to specify the filename YOU wanted, rather than having to be PROMPTED for it. That's kind of the point of the parameter.

You must be logged in to reply to this topic.