Any powershell scripters in the house?

The #1 community for Gun Owners in Indiana

Member Benefits:

  • Fewer Ads!
  • Discuss all aspects of firearm ownership
  • Discuss anti-gun legislation
  • Buy, sell, and trade in the classified section
  • Chat with Local gun shops, ranges, trainers & other businesses
  • Discover free outdoor shooting areas
  • View up to date on firearm-related events
  • Share photos & video with other members
  • ...and so much more!
  • Cameramonkey

    www.thechosen.tv
    Staff member
    Moderator
    Site Supporter
    Rating - 100%
    35   0   0
    May 12, 2013
    31,686
    77
    Camby area
    I'm ><THIS close to finishing a 2 part script to fix a problem, but I just cant quite get there. I'm having trouble picking up a variable I created.

    I need to start using the Dynamic DNS feature that Directnic offers. Frankly its a little lacking because they dont offer an app or router integration like others do. (But its free) They just give me a hard coded URL to visit, and when I hit the web server it updates the DNS entry. But I have to know what IP address to feed it. Now I'm trying to automate it so it can be scripted.

    Step 1: Get the IP address
    (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content | Tee-Object -Variable wanip
    That works and when I "echo $wanip" it outputs the correct IP address from step 1. So I know that part works.

    Step 2: Push the new address to Directnic
    I'm supposed to visit this URL: https://directnic.com/dns/gateway/[the secret string they provide]/?data=1.2.3.4 (with 1.2.3.4 the address I want to change the DNS entry to)
    So I send this command and it works:
    (Invoke-WebRequest -uri "https://directnic.com/dns/gateway/[the secret string they provide]/?data=1.2.3.4")

    But if I try this command, it sends the literal value $wanip not the stored string.
    (Invoke-WebRequest -uri "https://directnic.com/dns/gateway/[the secret string they provide]/?data=$wanip")

    So what am I missing? How do I get it to actually send the variable not the variable name?

    Eventually this will run regularly as a script to keep that IP updated.
     

    jkaetz

    Master
    Rating - 100%
    3   0   0
    Jan 20, 2009
    1,953
    83
    Indianapolis
    I see two potential options:
    Code:
    $uri = "https://directnic.com/dns/gateway/[the secret string they provide]/?data=$wanip"
    Invoke-WebRequest -uri $uri

    And if that works, you might be able to use the below if you want it inline

    Code:
    Invoke-WebRequest -uri ("https://directnic.com/dns/gateway/[the secret string they provide]/?data=$wanip)"

    Powershell may not be evaluating the string before sending it to Invoke-WebRequest
     

    fullmetaljesus

    Probably smoking a cigar.
    Rating - 100%
    6   0   0
    Jan 12, 2012
    5,849
    149
    Indy
    I see two potential options:
    Code:
    $uri = "https://directnic.com/dns/gateway/[the secret string they provide]/?data=$wanip"
    Invoke-WebRequest -uri $uri

    And if that works, you might be able to use the below if you want it inline

    Code:
    Invoke-WebRequest -uri ("https://directnic.com/dns/gateway/[the secret string they provide]/?data=$wanip)"

    Powershell may not be evaluating the string before sending it to Invoke-WebRequest

    Personally I'd go with ith the first option here.

    In the future when that string changes or url changes it will be easier to up date a variable be updating the actual code.
     

    Cameramonkey

    www.thechosen.tv
    Staff member
    Moderator
    Site Supporter
    Rating - 100%
    35   0   0
    May 12, 2013
    31,686
    77
    Camby area
    Second option doesn’t work. It sends the literal string of the variable name as typed, and doesn’t substitute the variable. I’ll try the first option later tonight.
     

    Cameramonkey

    www.thechosen.tv
    Staff member
    Moderator
    Site Supporter
    Rating - 100%
    35   0   0
    May 12, 2013
    31,686
    77
    Camby area
    First option worked like a charm. Thanks!

    Apparently you have to store it to get it to honor the variable.

    Somebody give Jkaets max rep for me. Board sez I gotta spread it around.
     

    jkaetz

    Master
    Rating - 100%
    3   0   0
    Jan 20, 2009
    1,953
    83
    Indianapolis
    And is there anything INGO CANT help with?
    A winning lotto number generator. :D

    I also just noticed my 2nd example has the closing paren inside the double quote, it should be outside. I'm somewhat surprised that didn't work though. I've run into more than a few instances where I had to force PS to evaluate a string before giving it to a cmdlet.
     

    Bigtanker

    Cuddles
    Emeritus
    Rating - 100%
    24   0   0
    Aug 21, 2012
    21,688
    151
    Osceola
    Found this on FB from some guy.

    You have to put the URL in as a variable and call it later.

    $uri = "https://directnic.com/dns/gateway/[the secret string they provide]/?data=$wanip"
    Invoke-WebRequest -uri $uri
     

    Cameramonkey

    www.thechosen.tv
    Staff member
    Moderator
    Site Supporter
    Rating - 100%
    35   0   0
    May 12, 2013
    31,686
    77
    Camby area
    A winning lotto number generator. :D

    I also just noticed my 2nd example has the closing paren inside the double quote, it should be outside. I'm somewhat surprised that didn't work though. I've run into more than a few instances where I had to force PS to evaluate a string before giving it to a cmdlet.

    I didnt even try the second example, because its the exact way that I tried and failed at. It apparently only parses the variables when the entire URI is sent up as a variable itself. (Mind boggling) Adding the variable to the end of the URI as a command causes it to push it to the web server exactly as written, and the variable name is not replaced with its value.

    Its times like this I am reminded why I do servers and networks, and not scripting/programming. Drives me absolutely nuts.
     

    fullmetaljesus

    Probably smoking a cigar.
    Rating - 100%
    6   0   0
    Jan 12, 2012
    5,849
    149
    Indy
    By chance, the secret string they give you, does it have special characters? Perhaps a couple $'s?

    If so PS was probably reading everything after it has a variable and dropping it.

    You could figure it out by changing your actual command to
    Write-host
    And it would spit out what it was seeing.

    You say you prefer to stick to servers and stay out of scripting. When you learn to embrace PS your job will become crazy less tedious and faster.

    PS has come a looong way in the areas of usefulness and documentation.

    Currently I'm a Linux admin so I never admit my PS skills for fear of getting drafted by the win team.
     

    Cameramonkey

    www.thechosen.tv
    Staff member
    Moderator
    Site Supporter
    Rating - 100%
    35   0   0
    May 12, 2013
    31,686
    77
    Camby area
    By chance, the secret string they give you, does it have special characters? Perhaps a couple $'s?

    If so PS was probably reading everything after it has a variable and dropping it.

    You could figure it out by changing your actual command to
    Write-host
    And it would spit out what it was seeing.

    You say you prefer to stick to servers and stay out of scripting. When you learn to embrace PS your job will become crazy less tedious and faster.

    PS has come a looong way in the areas of usefulness and documentation.

    Currently I'm a Linux admin so I never admit my PS skills for fear of getting drafted by the win team.

    Yes. I already have scripts to stand up my branch office servers to do stuff like setting time zones, installing the necessary roles and features, installing software, etc. I can now do about 40 steps in 2 scripts.

    I didnt say I didnt script, just that I dont LIKE to script. Which reminds me I still need to figure out the programmatic deletion of old backup files (>30 days old) in about a dozen directories. Trouble is one of the directories the backup mechanism exists, (backups are stored in the same folder as the puller script) so just deleting everything older than x days would delete the backup software during the first run. And it doesnt like putting the backup files somewhere else. Already tried and failed.
     
    Top Bottom