none
Convert Item from Input to Value from File RRS feed

  • Question

  • Hi All,

    I am working on a script to query AD for computers with specific names. What I want to do is to ask user for Location he wants to query and once he enters that, use a list of site codes, find the site code needed and use in filter to find hosts in AD.

    $Location = Read-Host "Enter Location"
    Get-ADComputer -Filter "Name -Like '$Location*'" | Select-Object "Name" | Out-File .\IR_Get_Location_Hosts_$Location.csv;

    I am struggling with coming up on how to handle this and would be grateful if you could help.

    The procedure:

    Ask for input

    Enter Location

    Check if location is valid from a list of locations in a file

    If valid, use site code that of that location to find computers in starting with that site code.

    I have to admint I cannot come up with a way to do this.

    Thanks in advance,

    WJ

    Monday, September 28, 2015 3:24 PM

Answers

  • Can I make the site codes iresulting from the switch be treated as variables so I can use them in the get-adc query as filter?

    Yes. Here is a simple example you can start with:

    $location = Read-Host 'Enter location'
    
    switch ($location) {
    
        'Loc1' { $siteCode = 'ABC' }
        'Loc2' { $siteCode = 'DEF' }
        default { $siteCode = '' }
    
    }
    
    If ($siteCode) {
    
        Write-Host "Run Get-ADComputer here with a filter of $siteCode" -ForegroundColor Green
    
    }

    In the second scenario, which is closer to what I am being asked to do, I'd need a list of locations in a file and the first task is to verify if location name entered is valid and then change it into a site code, if so, how can I do that?

    Here's something you can build from (this example requires at least v3):

    $location = Read-Host 'Enter location'
    
    $validLocationList = Get-Content .\validLocations.txt
    $siteCodeList = Get-Content .\siteCodeList.txt -Raw | ConvertFrom-StringData
    
    If ($validLocationList -contains $location) {
    
        If ($siteCode = $siteCodeList[$location]) {
    
            Write-Host "Run Get-ADComputer here with a filter of $siteCode" -ForegroundColor Green
    
        }
    
    } Else {
    
        Write-Host "$location is not valid" -ForegroundColor Red
    
    }
    
    
    validLocations.txt:
    loc1
    loc2
    
    siteCodeList.txt:
    loc1=ABC
    loc2=DEF


    • Proposed as answer by Chen VMVP Tuesday, September 29, 2015 8:03 AM
    • Marked as answer by Szadzik Tuesday, September 29, 2015 2:43 PM
    Monday, September 28, 2015 11:47 PM

All replies

  • Hi,

    I'd read in the location input file first and then check the entered code against what's in the list. If so, run Get-ADComputer. If not, exit.


    Monday, September 28, 2015 3:39 PM
  • Hi,

    I'd read in the location input file first and then check the entered code against what's in the list. If so, run Get-ADComputer. If not, exit.


    Thanks for the suggestion.

    The problem I have is that the user will type the location name and that needs to be verified as as existent in the location file and the the location's site code needs to be used as filter. The problem is that site code is different than location name, for example:

    User enters at location prompt: Warsaw

    The string to be used for filtering computer names: WAW

    or

    User enters Dubai for location

    DXB is to be used for filtering computer names.

    Any ideas? Maybe I shouldnd declare all locations (around 15 of them) as an array and have another array for site codes? Still not sure how to verify one against the other :(

    Monday, September 28, 2015 10:14 PM
  • I'd just use a switch inside the script for the site code depending on what the input value is. Alternatively, you could create a mapping file that you could read in that would link the full name with the site code.

    Monday, September 28, 2015 10:34 PM
  • I'd just use a switch inside the script for the site code depending on what the input value is. Alternatively, you could create a mapping file that you could read in that would link the full name with the site code.

    Two excellent ideas.

    Can I make the site codes iresulting from the switch be treated as variables so I can use them in the get-adc query as filter?

    In the second scenario, which is closer to what I am being asked to do, I'd need a list of locations in a file and the first task is to verify if location name entered is valid and then change it into a site code, if so, how can I do that?

    Sorry for the basic questions, but I am just beginning with PoSh.

    Monday, September 28, 2015 10:48 PM
  • Can I make the site codes iresulting from the switch be treated as variables so I can use them in the get-adc query as filter?

    Yes. Here is a simple example you can start with:

    $location = Read-Host 'Enter location'
    
    switch ($location) {
    
        'Loc1' { $siteCode = 'ABC' }
        'Loc2' { $siteCode = 'DEF' }
        default { $siteCode = '' }
    
    }
    
    If ($siteCode) {
    
        Write-Host "Run Get-ADComputer here with a filter of $siteCode" -ForegroundColor Green
    
    }

    In the second scenario, which is closer to what I am being asked to do, I'd need a list of locations in a file and the first task is to verify if location name entered is valid and then change it into a site code, if so, how can I do that?

    Here's something you can build from (this example requires at least v3):

    $location = Read-Host 'Enter location'
    
    $validLocationList = Get-Content .\validLocations.txt
    $siteCodeList = Get-Content .\siteCodeList.txt -Raw | ConvertFrom-StringData
    
    If ($validLocationList -contains $location) {
    
        If ($siteCode = $siteCodeList[$location]) {
    
            Write-Host "Run Get-ADComputer here with a filter of $siteCode" -ForegroundColor Green
    
        }
    
    } Else {
    
        Write-Host "$location is not valid" -ForegroundColor Red
    
    }
    
    
    validLocations.txt:
    loc1
    loc2
    
    siteCodeList.txt:
    loc1=ABC
    loc2=DEF


    • Proposed as answer by Chen VMVP Tuesday, September 29, 2015 8:03 AM
    • Marked as answer by Szadzik Tuesday, September 29, 2015 2:43 PM
    Monday, September 28, 2015 11:47 PM
  • Thanks a bunch, this is more than enough to get this working, much appreciated.
    Tuesday, September 29, 2015 7:11 AM
  • You're very welcome, good luck.

    Tuesday, September 29, 2015 12:04 PM
  • In case anyone needs anything like this here is hwat the final version from scenario #2 looks like:

    $location = Read-Host 'Enter location'
    $validLocationList = Get-Content .\Sites.dat
    $siteCodeList = Get-Content .\Codes.dat -Raw | ConvertFrom-StringData
    "[$ScriptName] [$Time] [Starting]" | Out-File $Log -Append 
    "[$ScriptName] [$Time] [Retrieving hosts from $location]" | Out-File $Log -Append 
    If ($validLocationList -contains $location) {
    
        If ($siteCode = $siteCodeList[$location]) {
        
            (Get-ADComputer -Filter "Name -Like '$siteCode*'" | Select-Object "Name" | Out-File .\Hosts_$SiteCode.csv)

    Tuesday, September 29, 2015 1:20 PM
  • One suggested tweak - use Export-Csv -NoTypeInformation instead of Out-File for your output results (especially since you're using the csv extension).

    Tuesday, September 29, 2015 2:41 PM
  • One suggested tweak - use Export-Csv -NoTypeInformation instead of Out-File for your output results (especially since you're using the csv extension).

    Excellent point!
    Tuesday, September 29, 2015 2:43 PM